2

A relatively simple task is turning into hours of frustration so here goes:

I have various Thermal printers and we print to the using the RawPrinterHelper that Microsoft posted. Usually use a String builder build the string and call the SendStringToPrinter and we have a printed piece of paper.

I'm trying to print a simple barcode via the ESC/POS commands that are supported. WE use these for other functions (cutting, change font sizes) that all works, the barcode refuses to print.

ESC POS Command is : GS k m n d1 d2 … dn m:barcode type e.g. n:barcode length -indicates the number of bar code data bytes d1 the barcode

The question I have is, How do I send the length of the barcode? I believe this where my problem lies.

The code snippet:

StringBuilder print = new StringBuilder();
barcode = "1234567890";
char commandGS = '\x1D';
char linefeed = '\x0A';
char esc = '\x1B';
char commandFontSize = '\x21';
char commandk = '\x6B';
char code128 = '\x69';
print.Append(commandGS);
print.Append(commandk);
print.Append(code128);
print.Append(barcode.Length);
print.Append(barcode);
string printJob = print.ToString();
RawPrinterHelper.SendStringToPrinter(printerName, printJob);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
user3725237
  • 131
  • 2
  • 10

1 Answers1

0

You probably need a byte to represent the length of the barcode rather than the text representation of the length of the barcode.

So instead of

print.Append(barcode.Length);

use

print.Append((char)barcode.Length);

I assume the length does not exceed 255.

Edited to add: You can examine the bytes which you are sending with something like

var bb = Encoding.UTF8.GetBytes(printJob);
bb.ToList().ForEach(x => Console.Write(x.ToString("X2") + " "));

and check that they conform to what you intend to send. You may also want to compare what is sent to something which does work in your current code.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84