skip to Main Content

I need to send a string to a Dot Matrix printer which uses Epson FX emulation that would print a simple barcode. I am able to print it by manually typing out the control codes into a string.

This is an example from manual: https://files.support.epson.com/pdf/general/escp2ref.pdf#page=327
enter image description here

I typed the data manually into a string:

var barcode = "x1Bx28x42x10x00x06x02x00x7Dx00x01x41x32x33x40x41x21x43x44x5Bx5D";

Then sent it using "qz tray" module.

This works well and the printer responds by printing the illustrated code properly. Now my struggle is actually giving it my own data and changing the last 10 string codes, I have tried many ways to encode a 10 character string properly, so far nothing has worked. Is there a way in JavaScript to do this?

EDIT:

Doing:

var barcode = "x1Bx28x42x10x00x06x02x00x7Dx00x01" + "1234567890";

was one of the first things I’ve tried, and results in the printer sitting idle and not reacting to the command at all. In fact I actually found that the first command 1B does not work and I have to send it using String.fromCharCode(27). The full working command that manages to print a barcode then becomes:

var barcode = String.fromCharCode(27) + "x28x42x10x00x06x02x00x7Dx00x01x41x32x33x40x41x21x43x44x5Bx5D";

and it prints the data just like in manual. Substituting the escaped hex data for "1234567890" somehow just breaks it and printer does nothing at all.

EDIT2 , The solution:

It turns out that when sending the data to the printer, I inadvertently skipped the control code that was responsible for setting the barcode data type, in this case it would be A. Without it the printer will sit idle. Working code:

String.fromCharCode(27) + "x28x42x10x00x06x02x00x0Ax00x01" + "A" + "1234567890";

2

Answers


  1. The hex value of:

    • x41x32x33x40x41x21x43x44x5Bx5D

    is simply the following text, encoded (or more properly, escaped) in hexadecimal format:

    Pay special note to the first character A, per wikipedia: (Thanks to @Thomas in the comments)

    • 128A (Code Set A) – ASCII characters 00 to 95 (0–9, A–Z and control codes), special characters, and FNC 1–4
    • 128B (Code Set B) – ASCII characters 32 to 127 (0–9, A–Z, a–z), special characters, and FNC 1–4
    • 128C (Code Set C) – 00–99 (encodes two digits with a single code point) and FNC1
    • A23@A!CD[]

    You can verify this in JavaScript:

    console.log("x41x32x33x40x41x21x43x44x5Bx5D");
    // prints: "A23@A!CD[]"
    

    So you can change your example to this:

    var barcode = "x1Bx28x42x10x00x06x02x00x7Dx00x01" + "A123456789";
    

    … or make it even more readable:

    var barcode = 
        "x1Bx28x42x10x00" + // Barcode command and data length
        "x06" +                 // Barcode type k = Code 128
        "x02" +                 // Module width m = 2 dots / 180 inch
        "x00" +                 // Space adj value s = +0 dots / 360 inch
        "x7Dx00" +             // Bar length v1,v2 = 125 / 180 inch
        "x01" +                 // Control flags c
        "A123456789";            // Barcode Data
    

    … or using QZ Tray’s array notation:

    var data = [
        "x1Bx28x42x10x00",  // Barcode command and data length
        "x06",                  // Barcode type k = Code 128
        "x02",                  // Module width m = 2 dots / 180 inch
        "x00",                  // Space adj value s = +0 dots / 360 inch
        "x7Dx00",              // Bar length v1,v2 = 125 / 180 inch
        "x01",                  // Control flags c
        "A123456789"             // Barcode Data
    ];
    
    qz.print(config, data);
    

    Edit: Thanks to @Thomas in the comments for mentioning that the A should be kept.

    I believe the data is formatted using the Epson ESC/P2 barcode format, defined here, page C-195: https://files.support.epson.com/pdf/general/escp2ref.pdf

    Login or Signup to reply.
  2. The correct barcode 128C per font encoding for 1234567890 in some BarCode TrueTypeFonts could be 69 0c 22 38 4e 5a 55 6a or i"8NZUj. The pairs of numbers would be such that "12" = x0c.

    One problem is there are other TTF encoding methods with different shifts so we may need D2 2C 42 58 6E 7A 75 D3 to get the same output.

    However those software TTF may not work in printer encoding as Epson have used their own different firmware encoding system!

    In Epson Esc Codes the C(ompact) ligature feature may not be used for the inputs, so all range C characters are used as if plain numerals.

    Thus rather than, 12 encoded as 0C using TTF, in Epson Esc/POS it is simply "12" = x3132. For that reason 128C may become academic for input, it may as well be 128A or 128B.

    So for Epson Esc/Pos the simplest range to use is the Data Character Set B range as it include all Upper, Lower and Numerals. To ensure a Check Code is added along with the Start and Stop codes use the following

    From example start with 1B 28 42 10 00 06 02 00 7D 00 as per example then 01 42

    followed by Human data 31323334353637383930
    enter image description here

    To test if the compact version is used in the C variant printout try:-

    From example start with 1B 28 42 10 00 06 02 00 7D 00 as per example then 01 43

    followed by Human data 31323334353637383930
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search