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
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
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)A23@A!CD[]
You can verify this in JavaScript:
So you can change your example to this:
… or make it even more readable:
… or using QZ Tray’s array notation:
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
The correct barcode 128C per font encoding for
1234567890
in some BarCode TrueTypeFonts could be69 0c 22 38 4e 5a 55 6a
ori"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 then01 42
followed by Human data
31323334353637383930
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 then01 43
followed by Human data
31323334353637383930