简体   繁体   English

从嵌入式android打印到USB或预选的网络打印机

[英]Print to USB or pre-selected network printer from embedded android

My product is an industrial measurement instrument that uses embedded Android. 我的产品是使用嵌入式Android的工业测量仪器。 The instrument needs to print results to a pre-selected network printer or to a USB printer. 仪器需要将结果打印到预选的网络打印机或USB打印机。 The instrument operator cannot be burdened with the standard Android printer interface, and Cloud printing is not acceptable. 仪器操作员不必负担标准的Android打印机界面的负担,并且不接受云打印。 I would think this situation is fairly common in products with embedded Android (eg, POS thermal printers) 我认为这种情况在带有嵌入式Android的产品(例如POS热敏打印机)中相当普遍

I have code which can find the available printers on the network and return the IP address and port numbers, and I can write plain ANSI text to the printer. 我有可以在网络上找到可用打印机并返回IP地址和端口号的代码,并且可以将纯ANSI文本写入打印机。 However, Unicode characters do not print correctly. 但是,Unicode字符不能正确打印。 A few other non-ANSI characters also print (some European letter variants). 其他一些非ANSI字符也会打印出来(某些欧洲字母变体)。 I believe this is because of the default "symbol set". 我相信这是由于默认的“符号集”。

My expectation is that I will use PCL or IPP to control the printer. 我的期望是,我将使用PCL或IPP来控制打印机。 All text starting with "@PCL" is printed as plain text. 以“ @PCL”开头的所有文本均以纯文本形式打印。 All text started with ESC is not printed, but I don't have any reason to believe that such commands are being processed. 以ESC开头的所有文本均未打印,但我没有任何理由相信此类命令正在被处理。

Searching the 'web, I see this question has been asked a few times, but not well answered. 搜索“网络”后,我看到此问题已被问过几次,但回答得不好。

I am wondering whether there is something wrong with my Socket/InputStream/BufferedReader usage. 我想知道我的Socket / InputStream / BufferedReader用法是否有问题。

Has anyone designed a solution for this usage? 有没有人为此设计解决方案?

final char ESC = 0x1b;
final String UEL = ESC + "%-12345X";
final String CRLF = "\r\n";
Socket socket = new Socket(printer.getIpAddr(), 9100);
InputStream inputStream = socket.getInputStream();
DataOutputStream oStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
write(oStream, UEL + "@PJL" + "\r\n");
write(oStream, "@PJL COMMENT some comments"  + CRLF);
write(oStream, "@PJL ECHO RRE" + CRLF);
write(oStream, UEL + "\r\n");
oStream.flush();

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
int count = bufferedInputStream.available();

The local 'write' method creates a byte array in UTF-8 from the Java String and writes the bytes to the stream. 本地“写入”方法从Java字符串中以UTF-8创建字节数组,并将字节写入流中。 Note that for these characters, UTF-8 is ANSI 请注意,对于这些字符,UTF-8是ANSI

Firstly, are you sure the printer speaks PJL and PCL ? 首先,您确定打印机会说PJL和PCL吗? Some special printers have their own languages. 一些特殊的打印机有自己的语言。

Secondly, I think your PJL has an extra newline. 其次,我认为您的PJL还有多余的换行符。 It seem that the First PJL command has no newline from the PJL escape. 似乎First PJL命令从PJL转义符起没有换行符。

Sample PJL from the HP PJL reference manual. HP PJL参考手册中的PJL示例。

%–12345X@PJL COMMENT *Start Job* 
@PJL JOB NAME = "Sample Job #1" 
@PJL SET COPIES = 3 
@PJL SET RET = OFF 
@PJL ENTER LANGUAGE = PCL 
E. . . . PCL job . . . .E
~
%–12345X@PJL 
@PJL EOJ
%–12345X

PJL reference PJL参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM