尝试通过Python CAN接收数据

时间:2019-05-14 14:48:55

标签: python-3.x can-bus

我正在尝试通过CAN总线接收数据。我正在将数据从Windows笔记本电脑发送到树莓派。由于某种原因,树莓派上的python脚本只是无限地挂起,并且不显示消息。我已经从canard文档中获取了示例代码,但是我没有运气。

接收代码:

"""Connect to CANable; print and echo any received frames"""
from canard import can
from canard.hw import cantact

dev = cantact.CantactDev("/dev/ttyACM0") # Connect to CANable that enumerated as ttyACM0
dev.set_bitrate(1000000) # Set the bitrate to a 1Mbaud
dev.start() # Go on the bus
count = 0

while True:
    count += 1
    frame = dev.recv() # Receive a CAN frame
    dev.send(frame) # Echo the CAN frame back out on the bus
    print(str(count) + ": " + str(frame)) # Print out the received frame

要发送的代码:

from __future__ import print_function
import can
def send_one():

    bus = can.interface.Bus(bustype='serial', channel='COM4', bitrate=1000000)

    msg = can.Message(arbitration_id=0xc0ffee,
                      data=[0, 25, 0, 1, 3, 1, 4, 1],
                      is_extended_id=True)

    try:
        bus.send(msg)
        print("Message sent on {}".format(bus.channel_info))
    except can.CanError:
        print("Message NOT sent")

if __name__ == '__main__':
    send_one()

我不确定自己在做什么错。

这是我的实现方式:

笔记本电脑(Windows,使用代码发送)-> USB线-> CANable适配器-> CAN Line-> CANable适配器-> USB Wire-> RaspberryPi(Linux,使用代码接收)

感谢所有提前答复的人。

0 个答案:

没有答案