javalinuxprintingusb4java

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. This printer supports TSPL2 commands.

I am using USB connection and started writing some simple tests using usb4java high-level API in-order to communicate to this printer. I am able to successfully query for the printer status/state <ESC>?! or <ESC>?S (<ESC> here is ASCII 27) with out any issues but unable to issue PRINT command.

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 ?

Does this USB communication with the TSC printer works without installing any printer driver (on linux) ?


Solution

  • Yes, your communication is correct.

    Yes, USB communication on linux can work directly, without drivers.

    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.

    There is also possibility, that the printer is using differnt USB endpoint for other than "status" communication.