ESC/POS: Print QR Code on Receipt
on Thursday, September 13, 2018
QR code has been used widely in the past few years; either for ticketing, purchasing and etc. Since last year, it's usage become trending technology when e-wallet industry emerge. Some of us might have different ways on how to print it on receipt/ticket; send qr code as image and print, or send the data to be convert to qr code and let the printer do the work. This entry will guide you on how to print qr code using receipt printer (Model: Pioneer STEP-5e Receipt Printer) by using ESC/POS command in C#/.NET.
COMMON ESC/POS COMMANDS
ESC a 0 = align left
ESC a 1 = align center
ESC a 2 = align right
ESC E 1 = turn on bold/emphasize mode
ESC E 0 = turn off bold/emphasize mode
ESC d n = feed/extra nth line
GS V 66 = partial cut for receipt
ESC @ = initialize printer
QRCODE ESC/POS COMMANDS
1. Selects QR Code model
Function 165: GS ( k pL pH cn fn n1 n2
2. Sets the module size of QR Code
Function 167: GS ( k pL pH cn fn n
3. Selects the error correction level for QR Code
Function 169: GS ( k pL pH cn fn n
4. Stores symbol data in the symbol storage area
Function 180: GS ( k pL pH cn fn m d1,d2...dk
5. Prints QR Code symbol data stored in the system saving region
Function 181: GS ( k pL pH cn fn m
Let's get into the coding part. Hooray! Please note that different receipt printer might use different ESC/POS command as some might use custom ESC/POS commands; such as Star TSP100 Cutter (TSP143). Please refer to the receipt printer manual accordingly. :D
//Convert ASCII/Decimal
string ESC = Convert.ToString((char)27);
string GS = Convert.ToString((char)29);
string center = ESC + "a" + (char)1; //align center
string left = ESC + "a" + (char)0; //align left
string bold_on = ESC + "E" + (char)1; //turn on bold mode
string bold_off = ESC + "E" + (char)0; //turn off bold mode
string cut = ESC + "d" + (char)1 + GS + "V" + (char)66; //add 1 extra line before partial cut
string initp = ESC + (char)64; //initialize printer
string buffer = ""; //store all the data that want to be printed
string QrData = "LianaAliBlogspotMy"; //data to be print in QR code
//Print Text
buffer += center;
buffer += "This text is align center!\n"; //to enter or add newline: \n
buffer += left;
buffer += "This text is align left!\n"; //align left is already set as default if not specified
//Print QRCode
Encoding m_encoding = Encoding.GetEncoding("iso-8859-1"); //set encoding for QRCode
int store_len = (QrData).Length + 3;
byte store_pL = (byte)(store_len % 256);
byte store_pH = (byte)(store_len / 256);
buffer += initp; //initialize printer
buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 4, 0, 49, 65, 50, 0 });
buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 67, 8 });
buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 69, 48 });
buffer += m_encoding.GetString(new byte[] { 29, 40, 107, store_pL, store_pH, 49, 80, 48 });
buffer += QrData;
buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 81, 48 });
//Cut Receipt
buffer += cut + initp;
//Send to Printer
RawPrinterHelper.SendStringToPrinter(ConfigurationManager.AppSettings["STEP-5e"], buffer);
沒有留言:
張貼留言