Communication through CAN-bus interface not working

1.7k Views Asked by At

I have python code running on a linux based device which has the following OS specifications:

NAME=Buildroot
VERSION=2020.11.1

This device is connected to a PLC using a three wire interface (CAN_H,CAN_L,GND).

I have written a code that sends a message to the PLC using the CAN-bus protocol. This is being done with the python-can package.

I have the following code:

import can

def send():
    #USB interface
    #bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)
    #on linux
    bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=500000)
    #on windows
    #bus = can.interface.Bus(bustype='serial', channel='COM1', bitrate=500000)
    msg = can.Message(arbitration_id=0x68005,data=[0x10,0x11,0x12],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()

The problem faced is that the device is not sending any data to the PLC. This is probably due to incorrect options set for bustype and channel.

Can anyone advise on what to provide for bustype and channel for this physical interface with three CAN wires?

1

There are 1 best solutions below

0
On

WARNING: I'm not familiar with the python-can library, but I am with socketcan.

vcan0 is a virtual can channel that you can setup in order to enable virtual communication between applications (e.g. testing).

An actual CAN device (e.g. PCAN-USB) is listed as can0, can1, canX. Beware that on some embedded systems this may not hold true (e.g. it can start with can1).

EDIT: I have forgotten to mention that you can get a list of all network interfaces like so, thus including CAN interfaces:

$ ifconfig -a

Reference documentation: https://elinux.org/Bringing_CAN_interface_up