Trying to receive data over Python CAN

北城余情 提交于 2019-12-13 04:26:29

问题


I am trying to receive data over a CAN bus. I am sending data from my windows laptop over to a raspberry pi. For some reason, the python script on the raspberry pi just hangs endlessly and it does not display the message. I have taken the sample code from the canard docs, but I have no luck.

Code to receive:

"""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

Code to send:

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()

I'm not sure what I am doing wrong.

Here is my implementation:

Laptop (Windows, using the code to send) -> USB Wire -> CANable Adapter -> CAN Line -> CANable Adapter -> USB Wire -> RaspberryPi (Linux, using the code to receive)

Thanks to all of those who reply in advance.

来源:https://stackoverflow.com/questions/56133228/trying-to-receive-data-over-python-can

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!