简体   繁体   English

Zpl 打印机从 Java 应用程序接收碎片数据

[英]Zpl printer receive fragmented data from Java application

Background背景

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

Issue问题

  1. After setting the emulator up and add it as zpl printer via mac Printer&Scanner,设置模拟器并通过 mac Printer&Scanner 将其添加为 zpl 打印机后, 在此处输入图像描述

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"

在此处输入图像描述

  1. However, my use case is send the command to a printer remotely on the.network, Therefore I need to create a java application to send the Zpl command to the printer.但是,我的用例是将命令远程发送到 .network 上的打印机,因此我需要创建一个 java 应用程序来将 Zpl 命令发送到打印机。 It succeeds initially, then it randomly get splitted:它最初成功,然后随机分裂:
    java code: java 代码:
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?有人可以帮忙吗?

Update:更新:

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.

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