python-can icon indicating copy to clipboard operation
python-can copied to clipboard

CANalyst-II can't reopen device

Open MoscowskyAnton opened this issue 2 years ago • 6 comments

Hello! I use your library with CANalyst-II device and it works pretty well, thank you! But I have one problem, first run of program is fully successful, but when I quit it and start again it throws error like:

    msg = self.bus.recv(1)
  File "/home/anton/.local/lib/python3.8/site-packages/can/bus.py", line 94, in recv
    msg, already_filtered = self._recv_internal(timeout=time_left)
  File "/home/anton/.local/lib/python3.8/site-packages/can/interfaces/canalystii.py", line 177, in _recv_internal
    self.poll_received_messages()
  File "/home/anton/.local/lib/python3.8/site-packages/can/interfaces/canalystii.py", line 157, in poll_received_messages
    (channel, raw_msg) for raw_msg in self.device.receive(channel)
  File "/home/anton/.local/lib/python3.8/site-packages/canalystii/device.py", line 259, in receive
    assert len(rx_data) % self.MESSAGE_BUF_LEN == 0
AssertionError
free(): double free detected in tcache 2

If I replug device everything is Ok again.
In code it is looks like:

self.bus = can.interface.Bus(bustype="canalystii", channel="0", bitrate=125000)
...
while True:
   msg = self.bus.recv(1)
...
self.bus.shutdown()

OS and version: Kubuntu 20.04 Python version: 3.8.10 python-can version: 4.0.0 python-can interface/s: canalystii==0.1

Any ideas?

MoscowskyAnton avatar Apr 15 '22 14:04 MoscowskyAnton

That's a bit weird, the assertion is failing because USB returned less bytes than expected, a "short read".

Can you try printing the actual number of bytes before the assert, please? i.e.

canalystii/device.py:
@@ -256,6 +256,7 @@ class CanalystDevice(object):
         message_ep = CHANNEL_TO_MESSAGE_EP[channel]
         rx_data = self._dev.read(message_ep | 0x80, rx_buffer_size)
 
+        print(len(rx_data), rx_buffer_size))
         assert len(rx_data) % self.MESSAGE_BUF_LEN == 0
         num_buffers = len(rx_data) // self.MESSAGE_BUF_LEN
 

Maybe the canalystii backend needs to reset the device somehow when it reopens it, or flush any pending reads.

projectgus avatar Apr 27 '22 11:04 projectgus

Thanks for response!

It printed two lines before crush

0 128  
48 128

And if I keep launching in increases in byte numbers

0 10752  
48 10752

I tried to add flush_tx_buffer after bus init, but this doesn't help There is also in canalystii function clear_rx_buffer but how can I reach it from can class? Because bus.clear_rx_buffer(0) doesn't works and it says

'CANalystIIBus' object has no attribute 'clear_rx_buffer'

MoscowskyAnton avatar Apr 28 '22 07:04 MoscowskyAnton

MESSAGE_BUF_LEN is 64 (each CAN message is 64 bytes long in the device's USB protocol). For some reason your device says it has messages available and then does a USB "short read" and only sends 48 bytes (less than a full message.)

i.e. the rx_buffer_size 128 is for 2 messages, 10752 is for 168 messages because the RX buffer is filling up.

Try bus.device.clear_rx_buffer(0) and see if you can start reading properly after this.

Otherwise you can try commenting the assert line. In your case, it will read 0 messages this time and maybe the next read will be correct? Although I don't know where the missing 16 bytes has gone...

projectgus avatar May 01 '22 23:05 projectgus

Clearing buffer doesn't help but commenting assert line almost does Now it reconnects successfully several times, until throws

    msg = self.bus.recv(1)
  File "/home/anton/.local/lib/python3.8/site-packages/can/bus.py", line 94, in recv
    msg, already_filtered = self._recv_internal(timeout=time_left)
  File "/home/anton/.local/lib/python3.8/site-packages/can/interfaces/canalystii.py", line 177, in _recv_internal
    self.poll_received_messages()
  File "/home/anton/.local/lib/python3.8/site-packages/can/interfaces/canalystii.py", line 157, in poll_received_messages
    (channel, raw_msg) for raw_msg in self.device.receive(channel)
  File "/home/anton/.local/lib/python3.8/site-packages/canalystii/device.py", line 257, in receive
    rx_data = self._dev.read(message_ep | 0x80, rx_buffer_size)
  File "/home/anton/.local/lib/python3.8/site-packages/usb/core.py", line 1029, in read
    ret = fn(
  File "/home/anton/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 846, in bulk_read
    return self.__read(self.lib.libusb_bulk_transfer,
  File "/home/anton/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 954, in __read
    _check(retval)
  File "/home/anton/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 602, in _check
    raise USBTimeoutError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBTimeoutError: [Errno 110] Operation timed out
free(): double free detected in tcache 2

But seems it is hardware problem, as soon it is strongly correlates with blinking on a device I use.

MoscowskyAnton avatar May 04 '22 08:05 MoscowskyAnton

Hi, I am using catalystii-II device to control my BLDC motor, While running python_catalystii.py file , i am getting the following error bus = can.interface.Bus(bustype='canalystii', channel=1, bitrate=500000)

File ~\Anaconda3\lib\site-packages\can\util.py:369 in wrapper return f(*args, **kwargs)

File ~\Anaconda3\lib\site-packages\can\interface.py:138 in new bus = cls(channel, **kwargs)

File ~\Anaconda3\lib\site-packages\can\interfaces\canalystii.py:116 in init channel = msg.channel

AttributeError: 'NoneType' object has no attribute 'VCI_OpenDevice',

Can anyone give solution for this problem ?

Nareshkumar-g avatar Feb 07 '23 08:02 Nareshkumar-g

@MoscowskyAnton 您好,请问python-can 4.2.0 怎么调用canalystii can.interface.detect_available_configs()函数无法得到具体的设备类型,但是比如Pcan 和Vector的设备都可以读取到

jxwdgd5201314 avatar May 16 '23 12:05 jxwdgd5201314