C# - send KPL commands to zebra ttp 2010 printer via serial port

1.3k views Asked by At

This is my first time posting to stackoverflow.

im working a windows forms (C#) application that needs to connect to a serial port(in this case COM1) to which a zebra ttp 2010 printer is connected to and send some KPL commands that the printer can understand and print what i want.

At the moment i'm doing this very simple code:

serialPort1.PortName = "COM1";
serialPort1.Open();

//command that prints a EAN barcode
//i have tested the command using the zebra toolbox.exe and works as intended
string str = "<ESC>BS<0><0><h 48><0><0><00><0><h  40><0><2><02><ESC>BW<00>123456789<00><LF><RS>";

serialPort1.Write(str);
serialPort1.Close();

when i execute this code, nothing is printed but if i press the physical "feed" button on the side of the printer the ejected paper has my string written in it instead of the result of the command.

I have also tried to convert the string to a byte array but the end result is the same.

byte[] toBytes = Encoding.ASCII.GetBytes(str);
serialPort1.Write(toBytes, 0, toBytes.Length);
serialPort1.Close();

So not only i am not being able to make the printer print the data that has receive but also the printer is not treating the data as KPL commands.

Anyone knows what i am doing wrong and how can i fix it?

****EDIT***** i figured out what i was doing wrong. i can't simply convert my text to a byte array because that won't convert the KPL commands (e.g. "<ESC>") to its hexadecimal representation.

By sniffing the com port i have discovered that for example a line feed command is represented by "0A" so when i do this:

byte[] toSend = { 0x1B, 0x74, 0x00, 0x0A, 0x00, 0x14, 0x00, 0x50, 0x61, 0x72, 0x6B, 0x4E, 0x61, 0x6D, 0x65, 0x00, 0x0A, 0x0A, 0x0D, 0x1E };
sp.Write(toSend, 0, toSend.Length);

the printer automatically starts printing as it should

1

There are 1 answers

2
cory.todd On

It sounds like there is a minimum ticket length requirement. Thermal printers typically have either a minimum number of lines to be printed or a timeout before a present and/or cut is executed. ESC/POS (the protocol you are using) has a bunch of commands that allow your to control when and how to present the ticket. There are some variations from vendor to vendor but your printer more than likely supports at least some of these commands.

<ESC> |#fP - Feed # of lines and cut paper
<ESC> |#lF - Feed # of line and do not cut

See page 29 of this manual https://www.zebra.com/content/dam/zebra/manuals/en-us/printer/kiosk-opos-driverguide-en.pdf.