CAN BUS python code issue

Hi I am using following script and python code to receive data. I am getting data from micro controller and also connect it with a CAN to USB converter to monitor data. At converter software I am getting correct data but at Orin NX I am getting some garbage data. See screen shorts. When I send the data from laptop (CAN to USB converter) to Orin then it work properly but not from microcontroller. Please help to solve this…
########################################################
#!/bin/bash
sudo busybox devmem 0xc303000 32 0x0000c400
sudo busybox devmem 0xc303008 32 0x0000c450
sudo modprobe can
sudo modprobe can-raw
sudo modprobe mttcan
sudo ip link set can0 up type can bitrate 500000
#cangen can0 -v
#candump can0
exit 0
#########################################################
python code

import the library

import can
import time
import sys

create a bus instance

many other interfaces are supported as well (see below)

#bus = can.Bus(interface=‘socketcan’,channel=‘vcan0’,receive_own_messages=True)

bus = can.interface.Bus(bustype=‘socketcan’, channel=‘can0’, bitrate=125000)

send a message

message = can.Message(arbitration_id=0x02, is_extended_id=True,
data=[0x11, 0x22, 0x33])

“”"
i=0

while i<100:
bus.send(message, timeout=0.5)
print(“\nMessage send count=”,i)
i=i+1
time.sleep(500/1000)
“”"

iterate over received messages

for msg in bus:
print(“{:X}: {}”.format(msg.arbitration_id, msg.data))
# or use an asynchronous notifier
#notifier = can.Notifier(bus, [can.Logger(“recorded.log”), can.Printer()])
#print(notifier)

#bus.shutdown()
sys.exit()
##########################################################
Link for Python code…

###########################################################

Data at Orin NX

<can.notifier.Notifier object at 0xffffa42e2400>
Timestamp: 1722515254.085720 ID: 088 S Rx E DL: 8 00 00 04 00 00 00 00 00 Channel: can0
Timestamp: 1722515254.091433 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0
Timestamp: 1722515254.097101 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0
Timestamp: 1722515254.102829 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0
Timestamp: 1722515254.108559 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0
Timestamp: 1722515254.114303 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0
Timestamp: 1722515254.119971 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0
Timestamp: 1722515254.125702 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0
Timestamp: 1722515254.131362 ID: 088 S Rx E DL: 8 00 00 00 18 00 00 00 00 Channel: can0

Data at laptop (This is correct data)

Hi skhanna.esys,

Are you using the devkit or custom board for Orin NX?
What’s your Jetpack version in use?

It seems you used 125kbps as bitrate from your host PC, but 500kbps on Jetson.
Please use the following command instead.

$ sudo ip link set can0 up type can bitrate 125000

Jetpack version is 5.1 and sorry, at both side the bitrate is 125kbps. As I told that on laptop I am able to receive data from Orin and microcontroller both but at orin side I am not able to receive data of microcontroller. When I transmit data frame from laptop it is received correctly. All of then are connected by CAN bus with termination resistance of 120ohm at microcontroller and Orin transceiver.

Hi, I think my problem is CAN bit rate. So how I can set the CAN bus bit rate using shall or python lib ‘can.py’?

In microcontroller CAN clock is 75MHz and baud rate is 125000, Bitrate-125000, Tseg1 --83 and Tseg2 --13.

So how can I set matching value in orin nx.

I calculate the bit rate from this site for microcontroller—CAN Bus Bit Timing Calculator - Kvaser

I am getting data on USB to CAN converter software but not on orin. But I am sure this is related with bitrate.

It may depend on your CAN application.
Could you just use ip link command to setup CAN and confirm if the issue is caused from bitrate?

Thanks I will check with command and as you ask I try with different value of Tseg1 and Tseg2 register at 351, 41 it work also but suppose there are 10 frames transmitted then 6 come shows as error frame rest 4 received correctly. On this basis I think it is bitrate problem.

Hi, try with IP link command and I am getting this–

tel_orin3@tel-orin3-desktop:~/Desktop/can_test$ ip link set can0 up type can bitrate 125000 dbitrate 125000 berr-reporting on
RTNETLINK answers: Operation not permitted

I try almost every thing which is given in this link…

Controller Area Network (CAN) — Jetson Linux Developer Guide documentation?

Hi,
Please tell how to remove this…
tel_orin3@tel-orin3-desktop:~/Desktop/can_test$ ip link set can0 up type can bitrate 125000 dbitrate 125000 berr-reporting on
RTNETLINK answers: Operation not permitted

Hi, Anyone please answer?

Please run sudo su before running this command.

It is possible to check the CAN frame directly on terminal? I don’t to apply any filter or rejection of frame due to error. Just want to check the frames

Please see the attached images. The device ID 64 is received on the orin nx without error but ID-304 is not receiving and giving error. So please help me to solve this

Here as you can see in third image the frame error is come but after that it is again transmitted (second image)successfully but not received on orin. I put logic analyzer on CAN-rx pin of orin nx. There it show some garbage data.
In first image the frame is ok and received correctly.

Please run candump -x any & on the your board to dump the CAN data.

Could you share the block diagram of your connections?
It seems you have 3 CAN devices as following:

  1. mttcan from Jetson Orin NX
  2. CAN to USB converter to Laptop
  3. Microcontroller

Can you share the result of the following command on each sides of CAN device?

$ ip -s -d link show can0

This is I am getting after running the command…

tel_orin3@tel-orin3-desktop:~/Desktop/can_test$ ip -s -d link show can0
9: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10000
link/can promiscuity 0 minmtu 0 maxmtu 0
can state ERROR-ACTIVE (berr-counter tx 0 rx 40) restart-ms 0
bitrate 125000 sample-point 0.875
tq 40 prop-seg 87 phase-seg1 87 phase-seg2 25 sjw 1
mttcan: tseg1 2…255 tseg2 0…127 sjw 1…127 brp 1…511 brp-inc 1
mttcan: dtseg1 1…31 dtseg2 0…15 dsjw 1…15 dbrp 1…15 dbrp-inc 1
clock 50000000
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 2 59 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
RX: bytes packets errors dropped overrun mcast
4400 550 0 0 0 0
TX: bytes packets errors dropped carrier collsns
800 100 0 0 0 0

And when I run python code…

tel_orin3@tel-orin3-desktop:~/Desktop/can_test$ python3 testcanbus.py
testcanbus.py:12: DeprecationWarning: The ‘bustype’ argument is deprecated since python-can v4.2.0, and scheduled for removal in python-can v5.0.0. Use ‘interface’ instead.
bus = can.interface.Bus(bustype=‘socketcan’, channel=‘can0’, bitrate=125000)

40: bytearray(b’\x00\x01\x02\x03\x04\x05\x06\x07’)
<can.notifier.Notifier object at 0xffffb554d4c0>
4: bytearray(b’\x00\x10\x00\x00\x00\x00\x00\x7f’)
<can.notifier.Notifier object at 0xffffb54ef100>
Timestamp: 1724069829.096500 ID: 040 S Rx DL: 8 00 01 02 03 04 05 06 07 Channel: can0
Timestamp: 1724069829.464517 ID: 004 S Rx E DL: 8 00 10 00 00 00 00 00 7f Channel: can0
40: bytearray(b’\x00\x01\x02\x03\x04\x05\x06\x07’)
<can.notifier.Notifier object at 0xffffb5563820>
Timestamp: 1724069829.861631 ID: 004 S Rx E DL: 8 00 10 00 00 00 00 00 7f Channel: can0
Timestamp: 1724069829.905609 ID: 040 S Rx DL: 8 00 01 02 03 04 05 06 07 Channel: can0
Timestamp: 1724069830.258881 ID: 004 S Rx E DL: 8 00 10 00 00 00 00 00 7f Channel: can0
Timestamp: 1724069830.324557 ID: 040 S Rx DL: 8 00 01 02 03 04 05 06 07 Channel: can0
Timestamp: 1724069830.655953 ID: 004 S Rx E DL: 8 00 10 00 00 00 00 00 7f Channel: can0
Timestamp: 1724069830.719877 ID: 040 S Rx DL: 8 00 01 02 03 04 05 06 07 Channel: can0
Timestamp: 1724069831.052962 ID: 004 S Rx E DL: 8 00 10 00 00 00 00 00 7f Channel: can0
Timestamp: 1724069831.129350 ID: 040 S Rx DL: 8 00 01 02 03 04 05 06 07 Channel: can0
4: bytearray(b’\x00\x10\x00\x00\x00\x00\x00\x7f’)
<can.notifier.Notifier object at 0xffffb54e6f10>

When I run can dump. I am getting data from USB to CAN but not from microcntroller …
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07
can0 RX - - 040 [8] 00 01 02 03 04 05 06 07


After this I again run “p -s -d link show can0”
tel_orin3@tel-orin3-desktop:~/Desktop/can_test$ ip -s -d link show can0
9: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10000
link/can promiscuity 0 minmtu 0 maxmtu 0
can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
bitrate 125000 sample-point 0.875
tq 40 prop-seg 87 phase-seg1 87 phase-seg2 25 sjw 1
mttcan: tseg1 2…255 tseg2 0…127 sjw 1…127 brp 1…511 brp-inc 1
mttcan: dtseg1 1…31 dtseg2 0…15 dsjw 1…15 dbrp 1…15 dbrp-inc 1
clock 50000000
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 3 133 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
RX: bytes packets errors dropped overrun mcast
13048 1631 0 0 0 0
TX: bytes packets errors dropped carrier collsns
800 100 0 0 0 0


After this I send frame only from microcontroller and run the command “candump -x any &”
I am getting this…
tel_orin3@tel-orin3-desktop:~/Desktop/can_test$ candump -x any &
[2] 4926

Could you also share this information about your setup?

I would also like to check the status on your microcontroller and CAN to USB converter.
It seems there’s no error packets in the result from Jetson.

Is your current issue about the CAN packet sending out from your microcontroller can not be received on Jetson?

can_dig1

1)As you ask for the block diagram.
2) There is no error packet from microcontroller to “USB CAN converter” all packet are received correctly.
3)Yes presently issue is with sending packet from microcontroller are not received correctly on orin
What ever send on can bus is received on USB to CAN converter (from microcntroller and orin). But problem is between Orin and Mirocontroller.

PLease see this for more clarity–All device are connected as shown in figure.

  1. “USB to CAN controller” to Orin --Working
  2. Orin to “USB to CAN controller” --Working
  3. Micorconteoller to “USB to CAN converter”–Working
  4. Orin to microcontroller–Not Working
  5. Micrcontroller to Orin–Not working

Thanks for your information and sharing the test result.
How about the result of “USB to CAN converter” to Micorconteoller?

Could you share the result of ip -s -d link show can0 on your STM32 microcontroller?
I would like to check its bitrate, sample-point and configuration of other CAN parameters.

Hi, This is the output on command and nothing is received on microcontroller.

an0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10
link/can promiscuity 0 minmtu 0 maxmtu 0
can state ERROR-PASSIVE (berr-counter tx 0 rx 127) restart-ms 0
bitrate 125000 sample-point 0.875
tq 40 prop-seg 87 phase-seg1 87 phase-seg2 25 sjw 1
mttcan: tseg1 2…255 tseg2 0…127 sjw 1…127 brp 1…511 brp-inc 1
mttcan: dtseg1 1…31 dtseg2 0…15 dsjw 1…15 dbrp 1…15 dbrp-inc 1
clock 50000000
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 1 15 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
RX: bytes packets errors dropped overrun mcast
432 54 0 0 0 0
TX: bytes packets errors dropped carrier collsns
400 50 0 0 0 0

HI, Any update

The result seems expected to me.

Could you get a scope to check the waveform when you are sending CAN packet from Orin to Microcontrolleer?