问题
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);
回答1:
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.
来源:https://stackoverflow.com/questions/33132105/rawprinterhelper-unable-to-print-barcode-using-esc-pos-commands