Search code examples
c#thermal-printerescpos

How i can generete qrcode with ESC POS?


I am developing QRCODE printing with the help of ESC / POS commands. However, I can't generate a qrcode with more than 127 characters.

Follows the code in C # :

   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;

   string initp = ESC + (char)64; //initialize printer

   string buffer = ""; //store all the data that want to be printed
   string QrData = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"; //data to be print in QR code

   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 });
   buffer += cut + initp;

in order to generate the qrcode string, I write the string to the file and have it printed.


Solution

  • That's because it uses string variables.

    Even if you specify the encoding, it is not always possible to convert character data with a value of 0x80 or higher to the correct byte value.

    If you use only a byte array, you can use long data.


    Not all can be treated as text encoded in a single code page.

    This is because commands that include control codes such as barcode printing are very likely to contain data that cannot be handled as character string text.

    However, text and barcode printing can be mixed. Text data can be written to a file as encoded binary data (not a string).

    If there is another program that reads data from the file and writes it to the printer, that program must open the file as a binary data file.