chemicstry / candle_driver

Python wrapper for candle (gs_usb) adapters.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cangaroo works, but the sample code doesn't work!

openedhardware opened this issue · comments

Here is my screenshot of my cangaroo:
image
So it received a packet from my device.
And my setup:
image

But following python3.8 code doesn't work though:

import candle_driver

devices = candle_driver.list_devices()

print('Found {} candle devices.'.format(len(devices)))
device = devices[0]
print('Device path: {}'.format(device.path()))
print('Device name: {}'.format(device.name()))
print('Device channels: {}'.format(device.channel_count()))
device.open()

ch = device.channel(0)
ch.set_bitrate(1000000)
ch.start()
ch.write(0x141, bytes([0x92, 0, 0, 0, 0, 0, 0, 0]))
try:
    frame_type, can_id, can_data, extended, ts = ch.read(1000)
    print('Received {} from ID {} at {}'.format(can_data, can_id, ts))
except TimeoutError:
    print('CAN read timeout')

# close everything
ch.stop()
device.close()

Its result:

Found 1 candle devices.
Device path: \\?\usb#vid_1d50&pid_606f&mi_00#6&2d528fd&0&0000#{c15b4308-04d3-11e6-b3ea-6057189e6443}
Device name: candle_2d528fd
Device channels: 1
test_candle.py:15: DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats
  ch.write(0x141, bytes([0x92, 0, 0, 0, 0, 0, 0, 0]))
CAN read timeout

Please help!

I am using CANable Pro after updating its firmware to candle_2d528fd

Thanks!

Hey!

Did you test if ch.write succeeds and actually sends something on the bus? If yes, then it's only reading that is broken?

@chemicstry

Yes, it returns True, but now I am getting the sent packet back...

test.py:17: DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats
  val = ch.write(0x141, bytes(send_packet))
test.py:20: DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats
  frame_type, can_id, can_data, extended, ts = ch.read(1000)
Found 1 candle devices.
Device path: \\?\usb#vid_1d50&pid_606f&mi_00#6&2d528fd&0&0000#{c15b4308-04d3-11e6-b3ea-6057189e6443}
Device name: candle_2d528fd
Device channels: 1
Sending packet: [48, 0, 0, 0, 0, 0, 0, 0]
Sent result: True
Received b'0\x00\x00\x00\x00\x00\x00\x00' from ID 321 at 2253538656

Received packet is actually what I sent...

But please check this screenshot:
image

Cangaroo receives 30 00 64 0A FA 32 14 14

Any idea?

Well, I can receive a reply from my device!

But at first, ch.read() function returns the command that was sent... lol
And then it receives a packet from my motor.

So I have to call ch.read() command twice.

Any idea why this happens?

I added the termination resistor to my motor and also set TERM jumper on CANable Pro board as connected.

Thanks!

Glad you got it working!

Receiving sent (echo) frames is intended and can be used to track when exactly (if at all) frame was sent. The returned timestamp is precise down to microseconds (?) and generated by the device itself.

You can check if it's an echo frame from the returned frame_type variable by comparing it with CANDLE_FRAMETYPE_ECHO constant.