Trying to receive data over Python CAN

2019-08-17 11:33发布

问题:

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.