javaprintingraspberry-piusb4javaescpos

Printing from raspberry pi via usb4java


Thank you in advance for reading this!

I am using a java app to send bytes through the usb interface to an EPSON TM-T88V receipt printer using esc/pos commands

The behaviour is different when executed on the raspberry pi compared to when executed on my dev laptop (which is super strange!)

When the bytes are printed from the raspberry pi - it stops before completing

The code is as below:

public void sendToPrinter(byte[] message) throws UsbException {
   UsbDevice device = getPrinterDevice(); //find the usb using usb4java

   UsbConfiguration configuration = device.getActiveUsbConfiguration();
   UsbInterface iface = configuration.getUsbInterfaces().get(0); //There was only 1

   if (!iface.isClaimed()) {
      iface.claim(usbInterface -> true);
   }

   UsbEndpoint endpoint = (UsbEndpoint) iface.getUsbEndpoints().get(0);
   UsbPipe pipe = endpoint.getUsbPipe();

   pipe.open();

   try {
      LOG.info(Arrays.toString(message));
      int sent = pipe.syncSubmit(message);
      LOG.info("Bytes Sent: " + sent);
   } finally {
      pipe.close();
   }

   iface.release();
}

Things I have investigated:

  1. I read on about java8 that during heavy processing, the pi might lose power to usb devices and I have upgraded the power source to the pi so that it is receiving 2A
  2. I have tried comparing the bytes sent to the printer from my dev machine and the pi and they are identical (the bytes sent and the number of bytes sent are identical)

I can provide more info on the bytes I sent to the printer, although the esc/pos commands sent from my laptop work as expected so I don't believe that could be the cause - but may be wrong!

Thanks again for all your help!


Solution

  • I seem to have found a solution

    Splitting the array and sending 8 bytes at a time has allowed the printer/pi to communicate successfully without the printer randomly stopping:

    public static void sendAsBatch(int batchSize, byte[] payload, UsbPipe pipe) throws UsbException {
    
       int offset = 0;
       for (int multiplier = 1; offset < payload.length; multiplier++) {
    
          byte[] batch = offset + batchSize < payload.length ?
                Arrays.copyOfRange(payload, offset, offset + batchSize) :
                Arrays.copyOfRange(payload, offset, payload.length);
    
          pipe.syncSubmit(batch);
          offset = multiplier * batchSize;
       }
    }
    

    I still don't know what the actual problem is though and if anyone can shed any light would be amazing

    But for anyone else facing the same issue this seems to have solved it :)