Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

delphi - Sending commands directly to Zebra EPL

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...