ruby 通过 ESC/POS 热敏打印机打印二维码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23577702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printing QR codes through an ESC/POS thermal printer?
提问by Giorgio Robino
I'm printing some QR codes (from a Ruby script) writing ESC/POS commands to a Epson TM-T20 thermal printer.
我正在打印一些二维码(来自 Ruby 脚本),将 ESC/POS 命令写入 Epson TM-T20 热敏打印机。
BTW, I'm writing a simple ESC/POS commands printer "driver". The printer I'm using an Epson TM-T20 (USB interface) I'm doing some tests from a Windows 7 host, using serialport gem.
顺便说一句,我正在编写一个简单的 ESC/POS 命令打印机“驱动程序”。我使用的是 Epson TM-T20(USB 接口)的打印机我正在使用串行端口 gem 从 Windows 7 主机进行一些测试。
All fine about writing ESC/POS commands for print formatted texts and also linear barcodes, but I have problems uinderstanding the command protocol to print QR CODES, using the only available documentation supplyied by Epson (as far as I know), see: http://www.novopos.ch/client/EPSON/TM-T20/TM-T20_eng_qr.pdf
关于为打印格式化文本和线性条码编写 ESC/POS 命令一切都很好,但是我在使用 Epson 提供的唯一可用文档(据我所知)来理解打印 QR 码的命令协议时遇到问题,请参阅:http: //www.novopos.ch/client/EPSON/TM-T20/TM-T20_eng_qr.pdf
Now, he section concerning QRCodes commands is for me pretty obscure and I was unable to interpreter requested byte sequences; instead I found very helpfull the Nicolas' example I found here: https://code.google.com/p/python-escpos/wiki/Usage
现在,关于 QRCodes 命令的部分对我来说非常晦涩,我无法解释请求的字节序列;相反,我发现我在这里找到的 Nicolas 示例非常有帮助:https: //code.google.com/p/python-escpos/wiki/Usage
Hacking that useful bytecodes example, I am able to successuffly print QR codes, see:
I https://twitter.com/solyarisoftware/status/464740233008132096
破解那个有用的字节码示例,我能够成功打印 QR 码,请参阅:
我https://twitter.com/solyarisoftware/status/464740233008132096
Nevertheless, in general, I'm confused on the ESC/POS message format, especially in case I would insert a long text message (> 400 chars) inside a QR code... It seem that printer reject (do not print) QR codes containing more than 400 chars using this code:
尽管如此,总的来说,我对 ESC/POS 消息格式感到困惑,尤其是在我将在二维码中插入长文本消息(> 400 个字符)的情况下......似乎打印机拒绝(不打印)二维码使用此代码包含超过 400 个字符的代码:
def test_qrcode (printer, text, print_also_text=false, qr_size=6.chr)
s = text.size + 3
lsb = (s % 256).chr
msb = (s / 256).chr
# https://code.google.com/p/python-escpos/wiki/Usage
escpos = ""
escpos << "\x1D\x28\x6B\x03\x00\x31\x43#{qr_size}"
escpos << "\x1D\x28\x6B\x03\x00\x31\x45\x33"
escpos << "\x1D\x28\x6B#{lsb}#{msb}\x31\x50\x30"
escpos << text #
escpos << "\x1D\x28\x6B\x03\x00\x31\x51\x30"
# writing byte streams directly to the serial port
printer.write escpos
end
Does someone can suggest a CLEAR ESC/POS DOCUMENTATION concerning the ESC/POS commands (=bytecodes sequences) to print QRCodes (two-dimensional code ESC/POS commands) ?
有人可以建议有关 ESC/POS 命令(=字节码序列)的 CLEAR ESC/POS DOCUMENTATION 来打印 QRCodes(二维码 ESC/POS 命令)吗?
回答by Josue Alexander Ibarra
The most complete documentation I've found for the ESC/POS command set is this one: http://content.epson.de/fileadmin/content/files/RSD/downloads/escpos.pdf
我为 ESC/POS 命令集找到的最完整的文档是这个:http: //content.epson.de/fileadmin/content/files/RSD/downloads/escpos.pdf
Recently, I added the QR code feature to a POS client. I've found it very useful to have a print out of this Code page 437 reference, especially for debugging a sequence that was printed.
最近,我为 POS 客户端添加了二维码功能。我发现打印出这个代码页 437 参考非常有用,特别是对于调试打印的序列。
My example is in Java, but you can get the idea:
我的例子是在 Java 中,但你可以理解:
public void print_qr_code(String qrdata)
{
int store_len = qrdata.length() + 3;
byte store_pL = (byte) (store_len % 256);
byte store_pH = (byte) (store_len / 256);
// QR Code: Select the model
// Hex 1D 28 6B 04 00 31 41 n1(x32) n2(x00) - size of model
// set n1 [49 x31, model 1] [50 x32, model 2] [51 x33, micro qr code]
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=140
byte[] modelQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x04, (byte)0x00, (byte)0x31, (byte)0x41, (byte)0x32, (byte)0x00};
// QR Code: Set the size of module
// Hex 1D 28 6B 03 00 31 43 n
// n depends on the printer
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=141
byte[] sizeQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x43, (byte)0x03};
// Hex 1D 28 6B 03 00 31 45 n
// Set n for error correction [48 x30 -> 7%] [49 x31-> 15%] [50 x32 -> 25%] [51 x33 -> 30%]
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=142
byte[] errorQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x45, (byte)0x31};
// QR Code: Store the data in the symbol storage area
// Hex 1D 28 6B pL pH 31 50 30 d1...dk
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143
// 1D 28 6B pL pH cn(49->x31) fn(80->x50) m(48->x30) d1…dk
byte[] storeQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, store_pL, store_pH, (byte)0x31, (byte)0x50, (byte)0x30};
// QR Code: Print the symbol data in the symbol storage area
// Hex 1D 28 6B 03 00 31 51 m
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=144
byte[] printQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x51, (byte)0x30};
// flush() runs the print job and clears out the print buffer
flush();
// write() simply appends the data to the buffer
write(modelQR);
write(sizeQR);
write(errorQR);
write(storeQR);
write(qrdata.getBytes());
write(printQR);
flush();
}
回答by ssokolow
I'm not familiar with ESC/POS but I do have some experience with QR codes.
我不熟悉 ESC/POS,但我对 QR 码有一些经验。
If you're hitting an upper limit on the number of characters you can put in a QR code, there are four things inherent in the QR code design which might be causing it:
如果您达到了可以放入 QR 码的字符数上限,则可能会导致 QR 码设计中固有的四件事:
You can control the amount of error correction data. More error correction means a bigger QR code but a more reliable scan.
QR codes must be square so you might be bumping up against your printer firmware's "maximum width" and "minimum QR code pixel size" limits.
QR codes are defined in "versions"with each version representing a range of sizes (the higher the version, the bigger the QR code). Your printer's firmware may just not support versions above a certain number.
QR codes support four different encodings with different data limits on each (Numeric, Alphanumeric, Binary, and Kanji).
您可以控制纠错数据的数量。更多的纠错意味着更大的二维码,但更可靠的扫描。
QR 码必须是方形的,因此您可能会遇到打印机固件的“最大宽度”和“最小 QR 码像素大小”限制。
二维码在“版本”中定义,每个版本代表一个大小范围(版本越高,二维码越大)。您的打印机固件可能不支持高于特定数量的版本。
QR 码支持四种不同的编码,每种编码具有不同的数据限制(数字、字母数字、二进制和汉字)。
This means that you should:
这意味着您应该:
Check how much error correction you're putting in. (From most to least, the levels are
H,Q,M, andL). You might find that a lower level of error correction still gives you enough reliability while allowing you to squeeze in more data.Check the spec to see if you can ask for smaller QR code pixels so a wider code will fit on the paper.
Check what the highest supported QR code version for your printer is.
Check which data encoding you're using.
检查多少纠错你安排了。(从多到少,水平是
H,Q,M,和L)。您可能会发现较低级别的纠错仍然可以为您提供足够的可靠性,同时允许您输入更多数据。检查规格,看看您是否可以要求更小的二维码像素,以便更宽的代码适合纸张。
检查您的打印机支持的最高二维码版本是什么。
检查您使用的数据编码。
If you're hitting a limit around 400 characters, the maximum version should be somewhere in the 8-13range for numeric, 11-17for alphanumeric, and 13-21for binary, depending on the level of error correction used. (See the reference tableI linked before)
如果您达到 400 个字符左右的限制,则最大版本应该在8-13数字、11-17字母数字和13-21二进制范围内的某个位置,具体取决于所使用的纠错级别。(见我之前链接的参考表)
回答by PaulW
This is in ESC/POS:
这是在 ESC/POS 中:
GS "(k" 4 0 49 65 50 0
GS "(k" 3 0 49 67 5
GS "(k" 3 0 49 69 48
GS "(k" 28 0 49 80 48 "https://stackoverflow.com/"
GS "(k" 3 0 49 81 48

