简体   繁体   English

从Java应用程序打印到TSC打印机

[英]Printing to TSC printer from java application

I am developing a java application (which will run on linux desktop) to print shipping labels using TSC TTP-244 Pro printer. 我正在开发一个Java应用程序(将在Linux桌面上运行),以使用TSC TTP-244 Pro打印机打印运输标签。 This printer supports TSPL2 commands. 本打印机支持TSPL2命令。

I am using USB connection and started writing some simple tests using usb4java high-level API in-order to communicate to this printer. 我正在使用USB连接,并开始使用usb4java高级API编写一些简单的测试,以便与此打印机进行通信。 I am able to successfully query for the printer status/state <ESC>?! 我能够成功查询打印机状态/状态<ESC>?! or <ESC>?S ( <ESC> here is ASCII 27) with out any issues but unable to issue PRINT command. <ESC>?S<ESC>这里是ASCII 27)没有任何问题,但无法发出PRINT命令。

Below is my code. 下面是我的代码。

@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;
}

Is my usb communication correct ? 我的USB通讯正确吗?

Does this USB communication with the TSC printer works without installing any printer driver (on linux) ? 无需安装任何打印机驱动程序(在Linux上),即可与TSC打印机进行USB通信吗?

Yes, your communication is correct. 是的,您的沟通是正确的。

Yes, USB communication on linux can work directly, without drivers. 是的,Linux上的USB通信无需驱动程序即可直接工作。

If printer is not accepting some command, double check that command, maybe there should be some control sum, or something else you have missed? 如果打印机不接受某些命令,请仔细检查该命令,也许应该有一些控制总和,或者您错过了其他东西? Study how exactly this command should be structured. 研究该命令应如何精确组织。

I have done a project using serial port to communicate with printer, the details of a command are very important. 我已经完成了一个使用串行端口与打印机通信的项目,命令的细节非常重要。 The status command could have no control sum and is very simple, that's why it work out-of-the-box, but with more sophisticated command you need to read the documentation and debug. status命令可能没有控制和,并且非常简单,这就是为什么它可以直接使用,但是要使用更复杂的命令,您需要阅读文档并进行调试。

There is also possibility, that the printer is using differnt USB endpoint for other than "status" communication. 打印机还有可能使用其他USB端点进行“状态”通信以外的通信。

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

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