I am trying to send commands directly to a Zebra TLP2844 printer. I followed the suggestion made here and my final code came to be as follows:
var
cm: String;
p: TPrinter;
i: integer;
begin
p := Printer;
p.BeginDoc;
for i := 0 to memo1.Lines.Count-2 do
begin
cm := memo1.Lines[i];
if Escape(p.Canvas.Handle,
PASSTHROUGH,
Length(cm),
PAnsiChar(cm),
nil) = 0 then
ShowMessage('Command error: ' + IntToStr(GetLastError));
end;
p.EndDoc;
end;
The content of memo1 is (first line is empty) as purposed here:
N
q609
Q203,26
B26,26,0,UA0,2,2,152,B,"603679025109"
A253,26,0,3,1,1,N,"SKU 6205518 MFG 6354"
A253,56,0,3,1,1,N,"2XIST TROPICAL BEACH"
A253,86,0,3,1,1,N,"STRIPE SQUARE CUT TRUNK"
A253,116,0,3,1,1,N,"BRICK"
A253,146,0,3,1,1,N,"X-LARGE"
P1,1
The commands don't seem to be properly received or interpreted by the printer. I checked that the printer is in Page Mode (EPL2), with the suggested code I am able to open the printer handle. But nothing is printed, only a new line of labels is feeded.
I tried to completely change the commands to something obviously wrong and the behaviour is the same.
What else should I be looking to get things printed?
Most printers that take raw commands require a prefix (starting sequence of characters) and suffix (ending sequence of chars) wrapping each command. I don't know what the prefix and suffix are for the Zebra, but the documentation should tell you.
Just add a pair of constants to define the prefix and suffix, and add them to your command before sending it.
The other issue might be that you're reading the content of your commands from a TMemo, which in Delphi 2009 and higher contains Unicode strings. You're then casting them down to PAnsiChar, which may be truncating the content. Do the conversion ahead of time by defining cm as an AnsiString, and then assigning to it first (as you are) before typecasting to pass to the Escape function. I've done this in my code to illustrate it.
var
cm: AnsiString;
p: TPrinter;
i: integer;
const
ZPrefix = AnsiString('$('); // Replace values for each of these with what
ZSuffix = AnsiString(')$'); // your documentation says you should use
begin
p := Printer;
p.BeginDoc;
for i := 0 to memo1.Lines.Count-2 do
begin
cm := ZPrefix + memo1.Lines[i] + ZSuffix;
if Escape(p.Canvas.Handle,
PASSTHROUGH,
Length(cm),
PAnsiChar(cm),
nil) = 0 then
ShowMessage('Command error: ' + IntToStr(GetLastError));
end;
p.EndDoc;
end;
I program in php which is like C
I can send things to the printer just fine
my code looks like your code the only thing is I am not sure how your programming language handles the newline in php it's \n at the end of each line
if the newline is not there the print job will not print
and if the " are not sent it will not print
your EPL looks fine and should print
there is somewhere on the zebra web site a download where you can send commands to a printer which is hooked up to your computer by USB cable
think it is called Zebra Setup Utilities
Related
I'm trying to use the SysUtils.WrapText() function with a string containing escaped single quotes characters, and I'm getting an unexpected result.
var
Lines : TStrings;
begin
Lines := TStringList.Create;
try
Lines.Text := WrapText('Can''t format message, message file not found', 15);
ShowMessage(Lines.Text);
finally
Lines.Free;
end;
end;
It seems that the function doesn't wrap the string at all, if the string contains an apostrophe character.
I've also tried using the #39 code instead of the single quote char but the problem persists. Furthermore I've checked Lines.Count and it's 1.
I've tried removing the single quote character:
var
Lines : TStrings;
begin
Lines := TStringList.Create;
try
Lines.Text := WrapText('Cant format message, message file not found', 15);
ShowMessage(Lines.Text);
finally
Lines.Free;
end;
end;
And it started wrapping the string as expected:
I'm wondering why is this happening, and how should I do for using the WrapText() function with such strings?
What you describe is intentional behavior.
In Delphi XE and earlier, the WrapText() documentation included this statement:
WrapText does not insert a break into an embedded quoted string (both single quotation marks and double quotation marks are supported).
In Delphi XE2 onwards, that statement is omitted from the documentation, but the behavior is still implemented in the RTL.
I have opened a ticket with Embarcadero about this omission:
RSP-24114: Important clause about embedded quoted strings is missing from WrapText documentation
In 10.3.1 the source includes code for handling quote characters, both double and single quotes, which looks to just ignore text between them. So one solution would be to use an apostrophe that is different from the single quote character. A second would to be to avoid using contractions. The start of the function source:
function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet;
MaxCol: Integer): string;
const
QuoteChars = ['''', '"'];
FirstIndex = Low(string);
StrAdjust = 1 - Low(string);
var
...
One option:
Lines.Text := WrapText('Can`t format message, message file not found', 15);
A second option:
Lines.Text := WrapText('Cannot format message, message file not found', 15);
Based on this question I have implemented the following code to send direct commands to my Zebra TLP2844
var
cmm: AnsiString;
i: integer;
begin
commands.saveToFile('path\to\a\file');
Printer.BeginDoc;
cmm := '';
for i := 0 to commands.Count-1 do
cmm := cmm + commands[i] + #10;
Escape(Printer.Canvas.Handle, PASSTHROUGH, Length(cmm), PAnsiChar(cmm), nil);
Printer.EndDoc;
end;
commands is a TSringList containing all the commands I want to send to the printer.
Note that I save all the commands to a text file.
Well, if I send this text file to print, via the driver preferences, using tools -> Action -> Send File, it prints perfectly.
If I use the code above, it spits some extra rows of labels after printing.
It shows me, obviously, that I am doing something wrong here, but I can't figure out what.
What I have tried
Send commands one by one and not concatenating them like in the code. Results: Nothing gets printed.
Changing #10 for a #13#10. Results: Same crazy behaviour (indeed Zebra EPL documentatins says it will ignore any #13 it finds)
What else should I try in order to send to the printer the commands the exact same way Zebra's tool does?
AFAIK you need to format the buffer as expected by the ExtEscape() API layout. I never used Escape(), but ExtEscape() - and it worked with a Zebra printer.
Here is what the MSDN doc states:
lpszInData [in] A pointer to the input structure required for the
specified escape. The first word in the buffer contains the number of
bytes of input data. The remaining bytes of the buffer contain the
data itself.
So you may code this as such:
cmm := '00'; // reserve space for the initial `word`
for i := 0 to commands.Count-1 do
cmm := cmm + commands[i] + #10;
pword(cmm)^ := length(cmm)-2; // store the length
if ExtEscape(Printer.Canvas.Handle, PASSTHROUGH, Length(cmm), pointer(cmm), 0, nil)<0 then
raise Exception.Create('Error at printing to printer');
Printer.EndDoc;
Be aware that if your command is not well formatted (e.g. missing chars), it may just create an out of memory error in the printer spooler - yes, I've seen that! In this case, you may have to kill then restart the Printer Spooler service... fix your code... and try again...
And do not forget to put the ESC character at the beginning of each of your commands[], as requested by the Zebra doc.
you can use this procedure: where the LabelFile is the full path of the label file,we are using this code and works with generic text driver printer and the printer is set as the default printer. it works with zebra printer and windows xp operating system.
https://stackoverflow.com/a/27647044/2977139
i hope this will help you.
If you want to use the Windows printer driver, you should use WritePrinter defined un the WinSpool unit. If I see this correctly, the TPrinter object from the Printers unit doesn't expose it's FPrinterHandle member, so you might need to use OpenPrinter and ClosePrinter yourself.
Having worked with MarkPoint printers at work, which are somewhat similar to Zebra printers: if the printer is connect to a serial port, I would warmly suggest to try and access the printer directly by connecting to the serial port with one of the several components available.
I have written a function that prints out ZPLII data to a Zebra printer as shown below
var
St : string;
StartDocument(DocName);
StartPage;
Try
For I:=0 to DataToPrint.Count-1 do
Begin
St:=FormatPrintLine(I);
Try
Escape(PrinterHandle,PASSTHROUGH,Length(St),PAnsiChar(St),Nil);
Except on E:Exception do
begin
GetWin32ApiErrorMessage(FErrorCode,FErrorText);
Raise EPrinter.CreateFmt('Printer Write Error %d'+#13+
'While Printing To '+PrinterName+#13+
ErrorText,[ErrorCode]);
end;
End;
end;
Finally
EndPage;
EndDocument;
I have tested the label data by using the command prompt to print it from a text file and the label prints out correctly but i cannot print it from my application. If i pause the printer i can see the job gets sent to the printer and the size of the job is 2.12Kb, roughly the size the label should be, but nothing prints out. The light on the Zebra printer for data lights up but nothing will print. I have tried this with two of the Zebra printers we own, so it is not a printer issue. My guess at this point is that maybe the program isn't sending the entire label data to the printer and the end is never received but when i trace through the send request, everthing is sent properly. the printer also shows the job has 0/0 pages for the label but i cannot understand why it is not sending the label. Is there something special that needs to go at the end of the label data besides the ^XZ termination character? I am also using Delphi XE3 if that helps.
Thanks to everyone's help I was able to successfully print my labels using the following changes:
St: AnsiString;
...
StartDocument(DocName);
StartPage;
Try
For I:=0 to DataToPrint.Count-1 do
Begin
St:=FormatPrintLine(I);
Try
WritePrinter(PrinterHandle,PChar(St),Length(St),N);
Except on E:Exception do
begin
GetWin32ApiErrorMessage(FErrorCode,FErrorText);
Raise EPrinter.CreateFmt('Printer Write Error %d'+#13+
'While Printing To '+PrinterName+#13+
ErrorText,[ErrorCode]);
end;
End;
end;
Finally
EndPage;
EndDocument;
I also had to change to writeprinter instead of escape. Escape did not print anything out after i change st to type AnsiString but writeprinter was successful.
I have an issue here, which I am trying to encode magnetic stripe data to an Fargo DTC400 printer, in the specifications it says I need to send the following string commands from example notepad, wordpad etc etc :
~1%TRACK NUMBER ONE?
~2;123456789?
~3;123456789?
this example encodes the string in track one, and the numbers 123456789 in both track 2 and 3.. this works from Notepad.exe.
EDIT:
Current delphi code I use works on another printer:
procedure SendQuote(MyCommand : AnsiString);
var
PTBlock : TPassThrough;
begin
PTBlock.nLen := Length(MyCommand);
StrPCopy(#PTBlock.SData, MyCommand);
Escape(printer.handle, PASSTHROUGH, 0, #PTBlock, nil);
end;
when I am trying to encode this string from my own application I get trouble, it seems the printer is totally ignoring my commands, when I choose print to file, I can read the binary data and see my string in the printed file, when I try to print to file from example notepad.exe I get just rubish binary data and cannot find my strings at all...
so I wonder what does notepad do to send this string command which I dont ?
hope someone can shed light on this because I have been eager to implement fargo support in my application for a longer period of time .
thanks
Update.
the following code is ancient but it does the job, however is there another way I can use this with the Passthrough code above?
var
POutput: TextFile;
k: Integer;
begin
with TPrintDialog.Create(self) do
try
if Execute then
begin
AssignPrn(POutput);
Rewrite(POutput);
Writeln(POutput,'~1%TESTENCODER?');
Writeln(POutput,'~2;123456789?');
Writeln(POutput,'~2;987654321?');
CloseFile(POutput);
end;
finally
free;
end
end;
TPassThrough should be declared like this :
type
TPassThrough = packed record
nLen : SmallInt;
SData : Array[0..255] of AnsiChar;
end;
You might be using a modern Delphi (2009 or newer) or forgotten the packed directive.
See also this SO question for a correct-way-to-send-commands-directly-to-printer.
At Torry's there is an example snippet (written by Fatih Ölçer):
Remark : Modified for use with Unicode Delphi versions as well.
{
By using the Windows API Escape() function,
your application can pass data directly to the printer.
If the printer driver supports the PASSTHROUGH printer escape,
you can use the Escape() function and the PASSTHROUGH printer escape
to send native printer language codes to the printer driver.
If the printer driver does not support the PASSTHROUGH printer escape,
you must use the DeviceCapabilities() and ExtDevMode() functions instead.
Mit der Windows API Funktion Escape() kann man Daten direkt zum Drucker schicken.
Wenn der Drucker Treiber dies nicht unterstützt, müssen die DeviceCapabilities()
und ExtDevMode() Funktionen verwendet werden.
}
// DOS like printing using Passthrough command
// you should use "printer.begindoc" and "printer.enddoc"
type
TPrnBuffRec = packed record
bufflength: Word;
Buff_1: array[0..255] of AnsiChar;
end;
function DirectToPrinter(S: AnsiString; NextLine: Boolean): Boolean;
var
Buff: TPrnBuffRec;
TestInt: Integer;
begin
TestInt := PassThrough;
if Escape(Printer.Handle, QUERYESCSUPPORT, SizeOf(TESTINT), #testint, nil) > 0 then
begin
if NextLine then S := S + #13 + #10;
StrPCopy(Buff.Buff_1, S);
Buff.bufflength := StrLen(Buff.Buff_1);
Escape(Printer.Canvas.Handle, Passthrough, 0, #buff, nil);
Result := True;
end
else
Result := False;
end;
// this code works if the printer supports escape commands
// you can get special esc codes from printer's manual
// example:
printer.BeginDoc;
try
DirectToPrinter('This text ');
finally
printer.EndDoc;
end;
How do I check if another application is busy?
I have a program that sends text to a console. The text that I will send contains #13 char (e.g. ls#13cd documents#13dir). In other words I want to send many commands at one time and the console will process them one by one. I am sending the text character by character. Sometimes the console only executes ls and cd documents. I think maybe this is because my program continuously sends character even if the console is busy, in which case the console does not receive incoming characters.
This is my code:
procedure TForm1.SendTextToAppO(Str: String; AHandle: Integer);
var
iWindow, iPoint, i: Integer;
SPass: PChar;
sList: TStringList;
begin
sList := TStringList.Create;
ExtractStrings([#13],[' '],PChar(Str),sList);
iWindow := AHandle;// AHandle is the handle of the console
iPoint := ChildWindowFromPoint(iWindow, Point(50,50));
for i:=0 to sList.Count-1 do begin
SPass := PChar(sList[i]);
try
while(SPass^ <> #$00) do begin
SendMessage(iPoint,WM_CHAR,Ord(SPass^),0);
Inc(SPass);
end;
SendMessage(iPoint,WM_KEYDOWN,VK_RETURN,0);
except
// do nothing;
end;
end;
end;
I am using Delphi 7.
If I interpret you question correctly you are sending the text to some sort of shell/command line interpreter and you want it to execute your commands.
Usually command line interpreters output a certain prompt (like $ on a Linux system or C:\ for DOS) that indicate that they can accept new commands. You need to read the output to wait for the appropriate prompt before you send another command. If you don't your sent text will be consumed as input by the currently running command (like you experienced).
lothar is on the right track; what you want to do is, instead of using ShellExecute, use CreateProcess. Look around Stack Overflow and Google for "Console Redirection" - that'll get you what you're looking for.
I think I understand what's going on, not that I have a fix for it:
You send a command to the console. While the command is running that program will receive the keys you send.