如何将包含 ESC/POS 命令的 .text 文件从 java 代码发送到打印机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13754668/
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
how to send a .text file containing ESC/POS commands to a printer from java code
提问by ALMEK
What i have so far is the following code:
到目前为止我所拥有的是以下代码:
FileInputStream fin =new FileInputStream(filename);
DocFlavor df = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc d = new SimpleDoc(fin, df, null);
PrintService P = PrintServiceLookup.lookupDefaultPrintService();
if (P != null) {
DocPrintJob job = P.createPrintJob();
job.print(d, null);
}
fin.close;
the code is working fine but the printer does't interpret the commands if the file containing commands, it keep printing the exact string content of the file. so how to send command to Epson receipt printer ?
代码工作正常,但如果文件包含命令,打印机不会解释命令,它会继续打印文件的确切字符串内容。那么如何向爱普生收据打印机发送命令?
回答by ALMEK
As have been figured out that commands may have to be send directly not in ESC/POS format but you need to interpret the code to hexadecimal in you java code and send to printer as the way i post, whether from a file or string. as example instead of initializing the Epson receipt printer by:
正如已经发现的那样,命令可能必须直接发送,而不是以 ESC/POS 格式发送,但您需要将 Java 代码中的代码解释为十六进制,并按照我发布的方式发送到打印机,无论是从文件还是字符串。作为示例,而不是通过以下方式初始化 Epson 收据打印机:
PRINT #1, CHR$(&H1B);"@";
and to cut the paper in receipt printer the code may be:
并在收据打印机中剪纸,代码可能是:
PRINT #1, CHR$(&H1D);"V";CHR$(1);
so this is how it works for me.
所以这就是它对我的工作方式。
char[] initEP = new char[]{0x1b, '@'};
char[] cutP = new char[]{0x1d,'V',1};
String Ptxt= new String(initEP)+ " text data \n \n \n"+ new String(cutP);
instead of
代替
Doc d = new SimpleDoc(new FileInputStream(filename), df, null);
use
利用
InputStream pis = new ByteArrayInputStream(Ptxt.getBytes());
Doc d = new SimpleDoc(pis, df, null);
however it maybe a way to send code as its command format but desperately could't do that so far. and not sure if it can done from java.
然而,它可能是一种将代码作为其命令格式发送的方法,但到目前为止还无法做到这一点。并且不确定它是否可以从 java 完成。
回答by David Duncan
The last step is inserting the printer commands using their true ASCII values in your input file--e.g., the escape character is ASCII value 0x1B. This can be done either by using an editor that allows inserting any ASCII value, such as a hex editor or Notepad++'s Character Panel (under the Edit menu), or by programmatically modifying the data sent to the printer after it is read from the file.
最后一步是使用输入文件中的真实 ASCII 值插入打印机命令——例如,转义字符是 ASCII 值 0x1B。这可以通过使用允许插入任何 ASCII 值的编辑器来完成,例如十六进制编辑器或 Notepad++ 的字符面板(在“编辑”菜单下),或者通过在从文件中读取数据后以编程方式修改发送到打印机的数据来完成。
回答by karnbo
When running linux (Ubuntu 12), this problem happens when the printer driver is generic + text. Selecting generic + raw queue, makes the printer behave more like a ESC POS device.
在运行 linux (Ubuntu 12) 时,当打印机驱动程序为 generic + text 时会出现此问题。选择通用 + 原始队列,使打印机的行为更像 ESC POS 设备。