0

Unfortunately, again I am experiencing some problems with communication via USB with PyUSB under Windows. In my question about 2-3 years ago, the problem was that the vendor provided wrong documentation and wrong SCPI commands.

Now, I would like to communicate with a stepper-motor controller (TMCM-6110) from Trinamic. Everything works fine with the IDE from the vendor, but now I want to address the device via pyusb, starting again from this pyusb-tutorial.

The device is found, so I assume:

  • libusb is installed correctly and found
  • idVendor and idProduct are correct

The commands I have to send are fixed 9 bytes messages. For example: "move to position absolute motor0 12800 steps". The byte sequence is: b'\x01\x04\x01\x00\x00\x00\x32\x01\x39' which, however, Python prints as b'\x01\x04\x01\x00\x00\x002\x019' which confused me and took me a while to figure out that this is the same.

From the output from print(dev), I assume that the address is 3. However, I get error messages:

Script:

import usb.core
import usb.util

dev = usb.core.find(idVendor=0x2a3c, idProduct=0x0100)
if dev is None:
    raise ValueError('Device is not found')
# device is found :-)
print(dev)

cmd = b'\x01\x04\x01\x00\x00\x002\x019'
# cmd = b'\x01\x04\x01\x00\x00\x00\x32\x01\x39'

dev.write(3,cmd)

Output:

DEVICE ID 2a3c:0100 on Bus 001 Address 006 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x110 USB 1.1
 bDeviceClass           :    0x2 Communications Device
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :   0x40 (64 bytes)
 idVendor               : 0x2a3c
 idProduct              : 0x0100
 bcdDevice              : 0x2e01 Device 46.01
 iManufacturer          :    0x1 Error Accessing String
 iProduct               :    0x2 Error Accessing String
 iSerialNumber          :    0x3 Error Accessing String
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x43 (67 bytes)
   bNumInterfaces       :    0x2
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0
   bmAttributes         :   0xc0 Self Powered
   bMaxPower            :   0x32 (100 mA)
    INTERFACE 0: CDC Communication =========================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x1
     bInterfaceClass    :    0x2 CDC Communication
     bInterfaceSubClass :    0x2
     bInterfaceProtocol :    0x1
     iInterface         :    0x0
      ENDPOINT 0x82: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x82 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x8 (8 bytes)
       bInterval        :   0xff
    INTERFACE 1: CDC Data ==================================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x2
     bInterfaceClass    :    0xa CDC Data
     bInterfaceSubClass :    0x0
     bInterfaceProtocol :    0x0
     iInterface         :    0x0
      ENDPOINT 0x3: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x3 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x0
      ENDPOINT 0x81: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x0

Error 1:

Traceback (most recent call last):
  File "C:\Tilman\Programs\Python37\lib\site-packages\usb\core.py", line 223, in get_interface_and_endpoint
    return self._ep_info[endpoint_address]
KeyError: 3

Error 2:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Trinamic.py", line 13, in <module>
    dev.write(3,cmd)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 940, in write
    intf, ep = self._ctx.setup_request(self, endpoint)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 215, in setup_request
    intf, ep = self.get_interface_and_endpoint(device, endpoint_address)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 225, in get_interface_and_endpoint
    for intf in self.get_active_configuration(device):
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 236, in get_active_configuration
    self.managed_open()
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 102, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\core.py", line 120, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\backend\libusb1.py", line 786, in open_device
    return _DeviceHandle(dev)
  File "C:\Test\Programs\Python37\lib\site-packages\usb\backend\libusb1.py", line 643, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "C:\Test\Programs\Python37\lib\site-packages\usb\backend\libusb1.py", line 593, in _check
    raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform

Questions:

  • Error 1: What does KeyError: 3 mean? Is 3 the wrong address?

  • Error 2: What does "NotImplementedError: Operation not supported or unimplemented on this platform" mean? How to fix?

  • What else to check and try in order to find out what's going wrong here?

And why can't these communication examples, no matter which interface (RS232, RS458, USB, LAN, ... ) be complete and working right away?

theozh
  • 22,244
  • 5
  • 28
  • 72

1 Answers1

1

This is a USB CDC ACM device, so the best way to talk to it is to make sure it is creating a COM port entry in the "Ports (COM & LPT)" list in your Device Manager and then open that COM port using pySerial.

You could use libusb to talk to it if you write and sign an INF file that tells Windows to load a different driver for your device (i.e. WinUSB), but it is a lot of trouble. Hardcoding endpoint numbers like that means your code will only work for this particular device.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • thank you for this hint. I didn't know about "special" devices, I thought this is simply USB. And on another application I am successfully talking to another device via pysusb. Actually, in the meantime I found a module on github (pyTrinamic), probably from the manufacturer himself. This seems to work, although, again documentation seems to be partly wrong or not updated. As a back-backup... I could try pySerial. Thanks for the input. Nevertheless, I would be interested in learning why the above USB connection does not work. – theozh Jul 29 '22 at 11:25
  • Because your device is probably managed by the usbser.sys driver in Windows. That driver is in charge of all the I/O done to the device. libusb doesn't know how to talk to that driver and that driver doesn't support anything like the libusb API, it is more limited. – David Grayson Jul 29 '22 at 15:55