1

我一直在使用 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()返回无。我知道这不是超时问题,因为错误会在几秒钟内发生,而我的超时时间是 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

研究

文档中,他们提到了该类can.ThreadSafeBus(),并且它“假设可以同时调用底层总线实例的 send() 和 _recv_internal()”这使我认为我必须使用此类总线实例才能 tx/rx同时发消息。但我看到线程这个词有点害怕。

更新 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
4

1 回答 1

0

这里的核心问题是我如何实现我的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
于 2020-07-01T23:58:29.470 回答