简体   繁体   English

通过usb4java从树莓派打印

[英]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 我正在使用Java应用程序使用esc / pos命令通过usb接口将字节发送到EPSON TM-T88V收据打印机

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 我读到有关java8的内容,在繁重的处理过程中,pi可能会给USB设备断电,并且我已将电源升级到pi,以便它接收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) 我尝试比较从我的开发机和pi发送到打印机的字节,它们是相同的(发送的字节和发送的字节数相同)

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! 我可以提供有关发送给打印机的字节的更多信息,尽管从笔记本电脑发送的esc / pos命令可以按预期工作,所以我不认为这可能是原因-但可能是错误的!

Thanks again for all your help! 再次感谢你的帮助!

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: 拆分阵列并一次发送8个字节使打印机/ pi可以成功通信,而打印机不会随机停止:

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 :) 但是对于其他面临相同问题的人来说,这似乎已经解决了:)

Try sending more bytes (one, two thousands?!) to the printer after you sent the ones that you want. 发送所需的字节后,尝试向打印机发送更多的字节(一,两千个?!)。 Try with 0x00 or 0xFF . 尝试使用0x000xFF

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

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