javascriptdot-matrixqz-tray

Convert a string to hexadecimal control code for a Dot Matrix printer


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 = "\x1B\x28\x42\x10\x00\x06\x02\x00\x7D\x00\x01\x41\x32\x33\x40\x41\x21\x43\x44\x5B\x5D";

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 = "\x1B\x28\x42\x10\x00\x06\x02\x00\x7D\x00\x01" + "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) + "\x28\x42\x10\x00\x06\x02\x00\x7D\x00\x01\x41\x32\x33\x40\x41\x21\x43\x44\x5B\x5D";

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) + "\x28\x42\x10\x00\x06\x02\x00\x0A\x00\x01" + "A" + "1234567890";


Solution

  • The hex value of:

    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

    You can verify this in JavaScript:

    console.log("\x41\x32\x33\x40\x41\x21\x43\x44\x5B\x5D");
    // prints: "A23@A!CD[]"
    

    So you can change your example to this:

    var barcode = "\x1B\x28\x42\x10\x00\x06\x02\x00\x7D\x00\x01" + "A123456789";
    

    ... or make it even more readable:

    var barcode = 
        "\x1B\x28\x42\x10\x00" + // 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
        "\x7D\x00" +             // Bar length v1,v2 = 125 / 180 inch
        "\x01" +                 // Control flags c
        "A123456789";            // Barcode Data
    

    ... or using QZ Tray's array notation:

    var data = [
        "\x1B\x28\x42\x10\x00",  // 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
        "\x7D\x00",              // 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