0

I'm trying to connect a POS receipt printer to python and have been using python-escpos to do so. I'm running windows 10.

My code in my file 'print.py' is:

from escpos.printer import Usb

p = Usb(0x0456,0x0808)

p.text('Hello World\n')

but when i run it i get the following error log:

Traceback (most recent call last):
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 223, in get_interface_and_endpoint
    return self._ep_info[endpoint_address]
KeyError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "print.py", line 5, in <module>
    p.text('Hello World\n')
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\escpos\escpos.py", line 437, in text
    self._raw(txt.encode())
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\escpos\printer.py", line 73, in _raw
    self.device.write(self.out_ep, msg, self.timeout)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 940, in write
    intf, ep = self._ctx.setup_request(self, endpoint)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 215, in setup_request
    intf, ep = self.get_interface_and_endpoint(device, endpoint_address)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 225, in get_interface_and_endpoint
    for intf in self.get_active_configuration(device):
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 239, in get_active_configuration
    bConfigurationValue=self.backend.get_configuration(self.handle)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\backend\libusb0.py", line 510, in get_configuration
    100)
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\backend\libusb0.py", line 593, in ctrl_transfer
    timeout
  File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\backend\libusb0.py", line 431, in _check
    raise USBError(errmsg, ret)
usb.core.USBError: [Errno None] b'libusb0-dll:err [control_msg] sending control message failed, win error: A device which does not exist was specified.\r\n\n'

Does anyone know what i need to do to make this work. I have installed libusb into my System32 and SysWOW64 folders, aswell as installing a libusb-32 driver for my printer with zadig.

The printer i'm using is a 'Hoin 58mm POS Printer'.

I would be grateful if someone could explain to me why i'm having this error and how to solve it!

Marc Balmer
  • 1,780
  • 1
  • 11
  • 18
hskerr4
  • 11
  • 1

3 Answers3

0

Firstly, you can check this link for the complete thread.

As suggested by KM4YRI,

I ran into the same No backend available exception when trying to follow the tutorial on a Windows 7 machine. The following worked for me, per one of the StackOverflow replies:

1. Download the latest Windows binary: https://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.21/libusb-1.0.21.7z/download

2. Unzip using 7zip into a temp dir

3. If on 64-bit Windows, copy `MS64\dll\libusb-1.0.dll` into `C:\windows\system32`.  If on 32-bit windows, copy `MS32\dll\libusb-1.0.dll` into `C:\windows\SysWOW64`.

I've verified the above answer on Windows-10 64-bit version.

HawkEye
  • 1
  • 2
0

I had a tight experience with that error using at least 3 different USB Storage Devices. After hours of trials I ended up to a feeling that USB transmission errors are the cause of this error.

The USB bus will return it in a random pattern for a low quality connection as if a device remove has presumably happened.

In my scenario I was bulk writing sectors to a \\.\PhysicalDriveN device and I noticed that after a short period the connection reestablished and the file handle was still valid and going.

To overcome the error I've ended up with the following steps:

  • Seek To File Position;
  • Write Sector;
  • For any 433 error:
    • 250 ms delay;
    • Seek To file Position, ignoring ERROR_NO_SUCH_DEVICE and ERROR_NOT_READY errors and trying for at least 1000 ms before giving up;
    • Read Sector, ignoring ERROR_NO_SUCH_DEVICE and ERROR_NOT_READY errors and retrying for up to 1000 ms before giving up;
    • Specially for my scenario: I've compared input buffer and read buffer and stop if matching;
    • Repeat the whole procedure up to 5 times.

So the idea here is the following: The device driver returns ERROR_NO_SUCH_DEVICE assuming a device remove scenario, but still in a pending state. As the problem is simply quality of physical connection, the link continues and the bus driver starts a renegotiation, in which case a ERROR_NOT_READY is temporarily returned. When the link is stable and negotiated, communication continues without the need to reopen the device handle.

mgruber4
  • 744
  • 7
  • 11
0

I would recommend adding a sleep statement of a second or two in between the instantiation of the USB printer, and the attempt to print to it. e.g:

from escpos.printer import Usb
from time import sleep

p = Usb(0x0456,0x0808)

sleep(1)

p.text('Hello World\n')
HB67
  • 1