0

Is there an equivalent in Python Socketcan / python-can that allows CAN messages to be sent to a specific destination device ID like how it's done on Arduino devices? The Arduino CAN bus boards use a MCP_CAN Library which allows for defining ID filter mode of the protocol controller in the command sendMsgBuf(ID, EXT, DLC, DATA). I have multiple devices connected which I can send specific CAN messages with ID field set to... CAN.sendMsgBuf(0x01...), CAN.sendMsgBuf(0x02...), CAN.sendMsgBuf(0x03...). However, when on a Linux board running socketcan, there's no equivalent Python-can with the cansend command.

Python-Can / Linux

  import can
  def send_one()
      bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=500000)
      msg = can.Message(arbitration_id=0xc0ffee,
                    data=[0, 25, 0, 1, 3, 1, 4, 1],
                    is_extended_id=False)
     try:
         bus.send(msg)
         print("Message sent on {}".format(bus.channel_info))
     except can.CanError:
        print("Message NOT sent")

 if __name__ == '__main__':
     send_one()

MCP_CAN Library for Arduino

//sendMsgBuf(ID, DLC, DATA)
...sendMsgBuf(0x01, DLC, DATA) //Send to Device ID 0x01
...sendMsgBuf(0x02, DLC, DATA) //Send to Device ID 0x02
...sendMsgBuf(0x02, DLC, DATA) //Send to Device ID 0x03

In a standard socketcan example, the can.Message() command sets the arbitration_id for the transmitting device, but it doesn't define the can_id of the device that I'm trying to send it to when I have multiple devices connected to the canbus which are waiting for messages. How would I send CAN messages to let's say can device 0x01, device 0x02, and device 0x03?

Stigma
  • 331
  • 1
  • 4
  • 15
  • Extended id = 29 bits. You set this to false, so you supposedly have 11 bits. Now how many bits are `0xc0ffee`... – Lundin Mar 25 '21 at 11:14
  • @Lundin I'm not sure I understand how that is relevant to my question. 0xc0ffee and the rest of the script is directly from the python-can documentation examples (https://python-can.readthedocs.io/en/master/). I can easily set arbitration ID to 0x08 or something. My question is regarding MCP_CAN allowing destination IDs but python's bus.send() and can.Message(...) doesn't as the arbitation ID is the ID of the transmitting device and not the destination ones. – Stigma Mar 25 '21 at 16:51
  • It's not related to your question, I was just being nice pointing out an obvious bug to you. The example you linked sets extended id to true, so it can use 29 bit identifier. You set it to false, so you can't. – Lundin Mar 26 '21 at 07:41
  • As for this Arduino stuff, how does "device id" make sense? Is that part of some higher layer protocol? If you send `0x01` with sendMsgBuf then what appears on the bus? Isn't it just setting the CAN message id to 1? Also the manual for sendMsgBuf says there are 4 parameters. – Lundin Mar 26 '21 at 07:47

0 Answers0