[英]Zpl printer receive fragmented data from Java application
I am trying to create a java application to send ZPL command to a ZPL printer and get a label printed.我正在尝试创建一个 java 应用程序以将 ZPL 命令发送到 ZPL 打印机并打印 label。 I set up a ZPL emulator( https://chrome.google.com/webstore/detail/zpl-printer/phoidlklenidapnijkabnfdgmadlcmjo ) by following this post: Emulate ZPL printer
我按照这篇文章设置了一个 ZPL 模拟器( https://chrome.google.com/webstore/detail/zpl-printer/phoidlklenidapnijkabnfdgmadlcmjo ): Emulate ZPL printer
I try to use lp command to print it.我尝试使用 lp 命令打印它。 it is stable the reliable:
它稳定可靠:
lp -o "raw" -q1 -d zpl <<< "^XA\n^FO50,60^A0,40^FDWorld Best Griddle^FS\n^FO60,120^BY3^BCN,60,,,,A^FD1234ABC^FS\n^FO25,25^GB380,200,2^FS\n^XZ"
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main (String argv[]) throws Exception
{
Socket clientSocket=new Socket("127.0.0.1",9100);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream() );
try {
outToServer.writeBytes("^XA\n^FO50,60^A0,40^FDWorld Best Griddle^FS\n^FO60,120^BY3^BCN,60,,,,A^FD1234ABC^FS\n^FO25,25^GB380,200,2^FS\n^XZ");
} catch (Throwable e) {
System.out.println(e);
}
outToServer.flush();
outToServer.close();
clientSocket.close();
// outToServer.flush();
}
}
Result:结果:
So quite often, it succeeds once and then following 2 or 3 printing get fragmented or failed.所以很多时候,它成功一次,然后在 2 或 3 次打印后变得支离破碎或失败。 I suspect it might be the socket TCP fragmentation issue.
我怀疑这可能是套接字 TCP 碎片问题。 or there any socket option I can configure?
或者我可以配置任何套接字选项? But not sure how to solve it.
但不确定如何解决它。 Can anyone help here?
有人可以帮忙吗?
I try to use wireshark.我尝试使用 wireshark。 The issue resides in the TCP package size:
问题在于 TCP package 大小:
For lp command, everything is sent as one single pack:对于 lp 命令,所有内容都作为一个单独的包发送:
As for java app, it is broken down into smaller packs for some reason.至于 java 应用程序,由于某种原因被分解成更小的包。
But I still don't know how to configure the pack size.但我仍然不知道如何配置包大小。 Also it confuses me a lot why Java app will break the data packet to like 1 byte or two byte sized?
同样让我很困惑的是,为什么 Java 应用程序会将数据包分解为1 个字节或两个字节大小?
I think you are using a wrong method to send printing job.我认为您使用错误的方法发送打印作业。 I'm not sure
lp
command uses Socket
connection.我不确定
lp
命令使用Socket
连接。 Try calling lp
from Java using Runtime.getRuntime().exec
:尝试使用
Runtime.getRuntime().exec
从 Java 调用lp
:
String cmd = "lp -o \"raw\" -q1 -d zpl <<< \"^XA\n^FO50,60^A0,40^FDWorld Best Griddle^FS\n^FO60,120^BY3^BCN,60,,,,A^FD1234ABC^FS\n^FO25,25^GB380,200,2^FS\n^XZ\"";
Runtime.getRuntime().exec(String.format(cmd));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.