In my Ubuntu22.04, I can not use bus0.recv API to acquire CANFD msg.
at first, I set up two virtual CAN device named "CAN0" and "CAN1". In Terminal 'A',I run the Python program named can_bridge.py to forward CAN msg between CAN0 and CAN1. In Terminal 'B', I run "cansend can0 cansend can0 213##4112233445566778899AA", I find that the can_bridge.py can not forward this msg and have no printf info. But I run "cansend can0 123#DEADBEEF", the can_bridge.py can forward this msg and have printf info as below:
Received on can0: Timestamp: 1742197953.015760 ID: 111 S Tx DL: 8 00 00 00 00 00 00 00 00 Channel: can0 Forwarding classic CAN message from can0 to can1
if I use cansend and candump to send or receive CANFD msg in my Ubuntu22.04, It' OK, so Can rule out hardware and config issues.
can_bridge.py's code:
#!/usr/bin/env python3
import can
# CAN interface names
CAN0 = 'can0'
CAN1 = 'can1'
def main():
# Setup CAN interfaces using python-can
bus0 = can.interface.Bus(channel=CAN0, interface='socketcan')
bus1 = can.interface.Bus(channel=CAN1, interface='socketcan')
while True:
# Receive a message from can0
msg = bus0.recv(timeout=1.0)
if msg is not None:
print(f"Received on {CAN0}: {msg}")
# Forward the message to can1
if msg.is_fd:
print(f"Forwarding CAN FD message from {CAN0} to {CAN1}")
else:
print(f"Forwarding classic CAN message from {CAN0} to {CAN1}")
bus1.send(msg)
# Receive a message from can1
msg = bus1.recv(timeout=1.0)
if msg is not None:
print(f"Received on {CAN1}: {msg}")
# Forward the message to can0
if msg.is_fd:
print(f"Forwarding CAN FD message from {CAN1} to {CAN0}")
else:
print(f"Forwarding classic CAN message from {CAN1} to {CAN0}")
bus0.send(msg)
if __name__ == "__main__":
main()
I want to use python program to implement logic as below:
In the Ubuntu host, write a python script that can implement the following pseudo-code logic. In the while infinite loop, use the poll interface to listen to the POLLIN events of the virtual nodes can0 and can1. After receiving the POLLIN event, use the read interface to obtain the message information of the node, and then use the write interface to send the frame message information to another node.
In my Ubuntu22.04, I can not use bus0.recv API to acquire CANFD msg.
at first, I set up two virtual CAN device named "CAN0" and "CAN1". In Terminal 'A',I run the Python program named can_bridge.py to forward CAN msg between CAN0 and CAN1. In Terminal 'B', I run "cansend can0 cansend can0 213##4112233445566778899AA", I find that the can_bridge.py can not forward this msg and have no printf info. But I run "cansend can0 123#DEADBEEF", the can_bridge.py can forward this msg and have printf info as below:
Received on can0: Timestamp: 1742197953.015760 ID: 111 S Tx DL: 8 00 00 00 00 00 00 00 00 Channel: can0 Forwarding classic CAN message from can0 to can1
if I use cansend and candump to send or receive CANFD msg in my Ubuntu22.04, It' OK, so Can rule out hardware and config issues.
can_bridge.py's code:
#!/usr/bin/env python3
import can
# CAN interface names
CAN0 = 'can0'
CAN1 = 'can1'
def main():
# Setup CAN interfaces using python-can
bus0 = can.interface.Bus(channel=CAN0, interface='socketcan')
bus1 = can.interface.Bus(channel=CAN1, interface='socketcan')
while True:
# Receive a message from can0
msg = bus0.recv(timeout=1.0)
if msg is not None:
print(f"Received on {CAN0}: {msg}")
# Forward the message to can1
if msg.is_fd:
print(f"Forwarding CAN FD message from {CAN0} to {CAN1}")
else:
print(f"Forwarding classic CAN message from {CAN0} to {CAN1}")
bus1.send(msg)
# Receive a message from can1
msg = bus1.recv(timeout=1.0)
if msg is not None:
print(f"Received on {CAN1}: {msg}")
# Forward the message to can0
if msg.is_fd:
print(f"Forwarding CAN FD message from {CAN1} to {CAN0}")
else:
print(f"Forwarding classic CAN message from {CAN1} to {CAN0}")
bus0.send(msg)
if __name__ == "__main__":
main()
I want to use python program to implement logic as below:
In the Ubuntu host, write a python script that can implement the following pseudo-code logic. In the while infinite loop, use the poll interface to listen to the POLLIN events of the virtual nodes can0 and can1. After receiving the POLLIN event, use the read interface to obtain the message information of the node, and then use the write interface to send the frame message information to another node.
Share Improve this question asked Mar 18 at 7:06 叶问天叶问天 11 Answer
Reset to default 0I assume you are using python-can
package (https://python-can.readthedocs.io/en/stable/). Try to change bus configs:
bus0 = can.interface.Bus(channel=CAN0, interface='socketcan', fd=True)
bus1 = can.interface.Bus(channel=CAN1, interface='socketcan', fd=True)