【问题标题】:Printing to TSC printer from java application从 Java 应用程序打印到 TSC 打印机
【发布时间】:2017-02-06 17:24:17
【问题描述】:

我正在开发一个 java 应用程序(将在 Linux 桌面上运行)使用 TSC TTP-244 Pro 打印机打印运输标签。本打印机支持 TSPL2 命令。

我正在使用 USB 连接并开始使用 usb4java 高级 API 编写一些简单的测试,以便与这台打印机通信。我能够成功查询打印机状态/状态 <ESC>?!<ESC>?S<ESC> 这里是 ASCII 27)没有任何问题,但无法发出 PRINT 命令。

下面是我的代码。

@Test
public void printTest() throws UsbException 
{
    final UsbServices services = UsbHostManager.getUsbServices();
    UsbDevice printerUsbDevice = findDevice(services.getRootUsbHub(), 0x1234, 0x1734);
    UsbConfiguration configuration = device.getActiveUsbConfiguration();
    UsbInterface iface = configuration.getUsbInterface((byte) 1);
    iface.claim();
    try
    {
        UsbEndpoint inEndpoint = iface.getUsbEndpoint(0x01);
        UsbPipe pipe = inEndpoint.getUsbPipe();


        UsbEndpoint outEndpoint = iface.getUsbEndpoint(0x82);
        UsbPipe pipe2 = outEndpoint.getUsbPipe();
        pipe2.open();

            pipe.open();
            pipe.syncSubmit(27 + "!?".getBytes("US-ASCII")); 
            pipe.close();

            pipe2.open();
            byte[] statusResponse = pipe2.syncSubmit(new byte[1]);
            pipe2.close();
            System.out.println(new String(statusResponse, "US-ASCII")); // Here status got is "00" if ok otherwise getting error code

            pipe.open();
            pipe.syncSubmit("SIZE 57 mm,37 mm");
            pipe.syncSubmit("GAP 3 mm,0 mm");
            pipe.syncSubmit("DIRECTION 1");
            pipe.syncSubmit("CLS");
            pipe.syncSubmit("TEXT 10,10 "3",0,1,1, "Test printing");
            pipe.syncSubmit("PRINT 1"); 
            pipe.close();

            // at this pint of time, printer is not printing anything instead just idle
    }
    finally        
    {
        iface.release();
    }
}

private UsbDevice findDevice(UsbHub hub, short vendorId, short productId)
{
    for (UsbDevice device : (List<UsbDevice>) hub.getAttachedUsbDevices())
    {
        UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
        if (desc.idVendor() == vendorId && desc.idProduct() == productId) return device;
        if (device.isUsbHub())
        {
            device = findDevice((UsbHub) device, vendorId, productId);
            if (device != null) return device;
        }
    }
    return null;
}

我的 USB 通讯是否正确?

这种与 TSC 打印机的 USB 通信是否可以在不安装任何打印机驱动程序的情况下工作(在 linux 上)?

【问题讨论】:

    标签: java linux printing usb4java


    【解决方案1】:

    是的,你的沟通是正确的。

    是的,Linux上的USB通讯可以直接工作,无需驱动。

    如果打印机不接受某些命令,请仔细检查该命令,也许应该有一些控制和,或者你错过了什么?研究这个命令的结构应该如何。

    我做过一个使用串口与打印机通信的项目,一个命令的细节很重要。 status 命令可以没有控制和并且非常简单,这就是它开箱即用的原因,但是对于更复杂的命令,您需要阅读文档和调试。

    还有一种可能是,打印机正在使用不同的 USB 端点进行“状态”通信以外的通信。

    【讨论】:

    • 非常感谢。将研究控制总和或不同的端点方面。
    • 虽然我仍然没有成功打印,但早先用PRINT 命令确定了问题所在。我必须在PRINT 命令结束时发出&lt;CR&gt;&lt;LF&gt;,以便打印机开始打印(可能是刷新其内部缓冲区的指示)。修改我的代码以发出&lt;CR&gt;&lt;LF&gt; 打印机响应但由于我的设置问题(与色带放置和标签位置有关),打印机在打印时给我错误。无论如何,感谢您的投入。
    • 是的,命令末尾的 CR LF 是必不可少的,如果对您有帮助,请接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多