5

How do I send text commands to a printer connected in the USB port using Delphi?

I have a Zebra TLP2844 printer and want to program a direct communication with it.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Ricardo Acras
  • 35,784
  • 16
  • 71
  • 112

1 Answers1

7

You use the WinAPI function Escape, passing it the Printer.Canvas.Handle as the first parameter and PASSTHROUGH as the nEscape parameter.

var
  YourCommand: String;
begin
  YourComamnd := 'Your command here';

  if Escape(Printer.Canvas.Handle, 
                PASSTHROUGH, 
                Length(YourCommand), 
                PChar(YourCommand), 
                nil) <> 0 then
    // Handle return value (listed in docs link above)
  else
    // send next command 

Escape is defined in the Windows unit. Note you have to call Printer.StartPage before using this function in order to prepare the printer driver to receive content.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • In Delphi XE3 Printer.Canvas.Handle would be Printer.Canvas.BufferHandle? – Ricardo Acras Jun 06 '13 at 15:05
  • In VCL.Printers, it's `Printer.Canvas.Handle`, which is the printer's `HDC`. I don't see `Canvas.BufferHandle` in the documentation anywhere. If you're not using the VCL, you should say so and add the appropriate FireMonkey tag to your question. :-) Just found `BufferHandle`, and no; that's a `THandle`, and `Escape` is looking for a handle to a device context (`HDC`). – Ken White Jun 06 '13 at 15:50
  • Looking at the source for `FMX.Printer.Win.pas`, there's a `Canvas` defined for the `TPrinter`. I don't know if it has a Windows-compatible HDC available (I doubt it), but you might be able to use the `Printer.Handle`, which it says is an HDC. I don't know if it will work with `Escape` or not, because I haven't tried it. – Ken White Jun 06 '13 at 15:59
  • I was confused by the help that pointed me to FMX when I tried to see wich unit defined the TPrinter class. I need the VCL indeed. Thank you. – Ricardo Acras Jun 06 '13 at 16:33
  • I have some more problems here. Couldn't fit it in a comment nor could I see how to edit the question to add that info. So I posted a new question on the same subject: http://stackoverflow.com/questions/16969323/sending-commands-directly-to-zebra-epl – Ricardo Acras Jun 06 '13 at 18:21
  • A new question was exactly the right thing to do, as it's a totally new question. :-) – Ken White Jun 06 '13 at 18:35