我一直在使用 python-can 和 cantools 来传输和接收 CAN 总线消息,没有任何问题。但是,当我开始发送定期消息,同时尝试接收消息时,似乎出现了一些错误。我不确定我的操作方式是否存在问题,或者是否根本无法执行同时发送和接收。当我尝试这样做时,
receive()
返回 NoneType,我知道这是不正确的,因为我有另一个设备记录并报告我正在寻找的信号确实存在于总线上。
是否可以使用python-can在同一总线上同时发送和传输?
追溯:
line 51, in tc_1
if sig_min <= sig <= sig_max:
TypeError: '<=' not supported between instances of 'float' and 'NoneType'
代码:
tc_1()
调用 receive()
在总线上查找特定消息,并调用 periodic_send()
在总线上定期发送消息。在我拨打 periodic_send()
之后,当我尝试在公交车上查找消息时,错误发生。似乎一旦我开始发送消息,receive()
就不会返回 None。我知道这不是超时问题,因为错误会在几秒钟内发生,而我的超时是 15 秒。如果我注释掉,我就不会明白这个问题periodic_send()
。
def tc_1(bus, num):
message = can.Message(arbitration_id=0x300)
sig = receive(bus, message, 'sig')
if sig is not None:
sig = float(sig)
logging.info('Test Case #{}.0 - PASS'.format(num))
if sig_min <= sig <= sig_max:
logging.info('Test Case #{}.1 - PASS'.format(num))
close_relay = can.Message(arbitration_id=0x303, data=[1])
periodic_send(bus, close_relay, 0.01)
then = time.time()
print('Entering while loop for sig to go low...')
while type(sig) == float and sig > sig_max:
# Refresh sig message instance
### THIS IS WHERE THE PROBLEM OCCURS ###
sig = receive(bus, message, 'sig')
if sig is not None:
sig = float(sig)
print('while loop, pwr_en = {:.3f}'.format(sig))
now = time.time()
# Timeout if sig does not transition low
if now > (then + relay_switch_time):
break
# Verify the active voltage state of power enable
if sig_min <= sig <= sig_max:
print('PASS')
else:
print('FAIL')
else:
print('FAIL')
else:
print('FAIL')
# Final clean-up and restore
bus.stop_all_periodic_tasks()
def receive(bus, message, signal):
# include dbc file
db = cantools.db.load_file(dbc_path)
msg = bus.recv(timeout=15)
if msg is None:
logging.warning("Failed to detect bus activity. Timeout occurred")
if msg is not None and msg.arbitration_id == message.arbitration_id:
message_data = db.decode_message(msg.arbitration_id, msg.data)
signal_data = message_data.get(signal)
return signal_data
def periodic_send(bus, message, period):
try:
bus.send_periodic(message, period)
except can.CanError:
print("Message NOT sent")
finally:
return
研究
在 docs 中,他们提到了
can.ThreadSafeBus()
类,并且它 “假设底层总线实例的 send() 和 _recv_internal() 可以同时调用” 这让我认为我必须使用此类总线实例以便同时发送/接收消息。但我看到“线程”这个词,有点害怕。
更新1:
经过一些测试,看起来 can.Bus 实例可以同时发送和接收消息。它实际上并不是“同时”的,但如果您尝试同时发送和接收,则没有问题。
我遇到的问题似乎在于我如何实现发送和接收。有时
receive()
返回 None
(参见下面的代码),当在比较语句中使用该返回值时,这会出现问题,结果为: TypeError: '<=' not supported between instances of 'float' and 'NoneType'
1593644420.403561 - sig: 4.7067000000000005
1593644420.4893048 - sig: 4.752400000000001
1593644420.5660996 - sig: None
1593644420.7276666 - sig: 4.752400000000001
1593644420.8034878 - sig: 4.752400000000001
1593644420.8912296 - sig: 4.7067000000000005
1593644420.9899697 - sig: 4.752400000000001
1593644421.0677896 - sig: None
1593644421.2223675 - sig: 1.0053
1593644421.3011339 - sig: 0.31980000000000003
1593644421.3919268 - sig: 0.0913
这里的核心问题是我如何实现我的
receive()
。内置的 recv()
是我最初用来从总线轮询数据的:
def receive(bus, message, signal):
# include dbc file
db = cantools.db.load_file(dbc_path)
msg = bus.recv(timeout=15)
if msg is None:
logging.warning("Failed to detect bus activity. Timeout occurred")
if msg is not None and msg.arbitration_id == message.arbitration_id:
message_data = db.decode_message(msg.arbitration_id, msg.data)
signal_data = message_data.get(signal)
return signal_data
这种方法的问题是,如果不满足条件,
recv()
将返回None
。因此,我没有使用 recv()
,而是轮询总线,直到找到消息。这目前有效,但我还需要添加一些异常和超时处理,以防总线出现问题或消息永远不会到达。
def receive(bus, message, in_signal):
# include dbc file
db = cantools.db.load_file(dbc_path)
for msg in bus:
if msg.arbitration_id == message.arbitration_id:
message_data = db.decode_message(msg.arbitration_id, msg.data)
signal_data = message_data.get(in_signal)
return signal_data
我很抱歉没有将其添加为评论,我的代表还不够高,甚至无法对您的帖子进行投票。我只是想说谢谢你回来并发布你的解决方案,我在开始使用这个库时遇到了类似的麻烦,你基本上遇到了,并解决了我面临的每个问题。我很感激你的努力,谢谢!