将字符串转换为点阵打印机的十六进制控制代码

Fro*_*xor 3 javascript dot-matrix qz-tray

我需要将字符串发送到点阵打印机,该打印机使用 Epson FX 仿真来打印简单的条形码。我可以通过手动将控制代码键入字符串来打印它。

这是手册中的示例:https ://files.support.epson.com/pdf/general/escp2ref.pdf#page=327 在此输入图像描述

我手动将数据输入到字符串中:

var barcode = "\x1B\x28\x42\x10\x00\x06\x02\x00\x7D\x00\x01\x41\x32\x33\x40\x41\x21\x43\x44\x5B\x5D";

然后使用“qz托盘”模块发送它。

效果很好,打印机会通过正确打印所示代码来做出响应。现在我的斗争实际上是给它我自己的数据并更改最后 10 个字符串代码,我尝试了很多方法来正确编码 10 个字符的字符串,但到目前为止没有任何效果。JavaScript 有没有办法做到这一点?

编辑:

正在做:

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

这是我首先尝试的事情之一,结果导致打印机闲置并且根本不对命令做出反应。事实上,我实际上发现第一个命令 1B 不起作用,我必须使用 发送它String.fromCharCode(27)。打印条形码的完整工作命令变为:

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";

它会像手册中一样打印数据。以某种方式替换转义的十六进制数据"1234567890"只会破坏它,并且打印机什么也不做。

编辑2,解决方案:

事实证明,当我将数据发送到打印机时,我无意中跳过了负责设置条形码数据类型的控制代码,在这种情况下它将是A。没有它,打印机将闲置。工作代码:

String.fromCharCode(27) + "\x28\x42\x10\x00\x06\x02\x00\x0A\x00\x01" + "A" + "1234567890";

tre*_*esf 5

十六进制值:

\n
    \n
  • \\x41\\x32\\x33\\x40\\x41\\x21\\x43\\x44\\x5B\\x5D
  • \n
\n

只是以下文本,以十六进制格式编码(或更准确地说,转义):

\n

A根据维基百科,请特别注意第一个字符:(感谢评论中的@Thomas)

\n
\n
    \n
  • 128A(代码集 A) \xe2\x80\x93 ASCII 字符 00 至 95(0\xe2\x80\x939、A\xe2\x80\x93Z 和控制代码)、特殊字符和 FNC 1\xe2\x80\x934
  • \n
  • 128B(代码集 B) \xe2\x80\x93 ASCII 字符 32 至 127 (0\xe2\x80\x939、A\xe2\x80\x93Z、a\xe2\x80\x93z)、特殊字符和 FNC 1\xe2 \x80\x934
  • \n
  • 128C(代码集 C) \xe2\x80\x93 00\xe2\x80\x9399(用单个代码点编码两个数字)和 FNC1
  • \n
\n
\n
    \n
  • A23@A!CD[]
  • \n
\n

您可以在 JavaScript 中验证这一点:

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

所以你可以将你的示例更改为:

\n
var barcode = "\\x1B\\x28\\x42\\x10\\x00\\x06\\x02\\x00\\x7D\\x00\\x01" + "A123456789";\n
Run Code Online (Sandbox Code Playgroud)\n

...或者使其更具可读性:

\n
var barcode = \n    "\\x1B\\x28\\x42\\x10\\x00" + // Barcode command and data length\n    "\\x06" +                 // Barcode type k = Code 128\n    "\\x02" +                 // Module width m = 2 dots / 180 inch\n    "\\x00" +                 // Space adj value s = +0 dots / 360 inch\n    "\\x7D\\x00" +             // Bar length v1,v2 = 125 / 180 inch\n    "\\x01" +                 // Control flags c\n    "A123456789";            // Barcode Data\n
Run Code Online (Sandbox Code Playgroud)\n

...或使用 QZ Tray 的数组表示法:

\n
var data = [\n    "\\x1B\\x28\\x42\\x10\\x00",  // Barcode command and data length\n    "\\x06",                  // Barcode type k = Code 128\n    "\\x02",                  // Module width m = 2 dots / 180 inch\n    "\\x00",                  // Space adj value s = +0 dots / 360 inch\n    "\\x7D\\x00",              // Bar length v1,v2 = 125 / 180 inch\n    "\\x01",                  // Control flags c\n    "A123456789"             // Barcode Data\n];\n\nqz.print(config, data);\n
Run Code Online (Sandbox Code Playgroud)\n

编辑:感谢@Thomas 在评论中提到A应该保留。

\n

我相信数据是使用 Epson ESC/P2 条形码格式进行格式化的,在此处定义,第 C-195 页: https: //files.support.epson.com/pdf/general/escp2ref.pdf

\n

  • “条形码数据”的第一个字符“A|B|C”似乎表示使用的“数据字符集”,所以我们不应该改变它。 (2认同)