Search code examples
python-2.7can-buspython-can

Sending messages with different ID on a pcan can bus, using python can


My program sends almost 50 messages, all with different ID's, on a pcan can-bus. And then loops again continuously, starting with a new data for 1st ID.

I have been able to initialize and send the single ID message, but I'm not able to send any other ID on the bus. I am analyzing the bus signal using an oscilloscope, and therefore I can see what messages are on the bus.

This is a part of code, showing how I'm trying to send 2 consecutive messages on the bus, but it only sends the id=100 message and not the next ones. I'm only importing the python-can library, for this.

        for i in range(self.n_param):
            if self.headers[i] == 'StoreNo':  # ID 100 byte size = 3
                to_can_msg = []
                byte_size = 3
                hex_data = '0x{0:0{1}X}'.format(int(self.row_data[i], 10), byte_size * 2)
                to_can_msg = [int(hex_data[2:4], 16), int(hex_data[5:6], 16), int(hex_data[7:8], 16)]
                bus_send.send(Message(arbitration_id=100, data=to_can_msg))

            elif self.headers[i] == 'Date':  # ID 101 byte size = 4
                to_can_msg = []
                byte_size = 4
                date_play = int(self.row_data[i].replace("/", ""), 10)
                hex_data = '0x{0:0{1}X}'.format(date_play, byte_size * 2)
                to_can_msg = message_array(hex_data)
                bus_send.send(Message(arbitration_id=101, data=to_can_msg))

And I'm closing each loop with bus_send.reset() to clear any outstanding message in the queue and begin afresh in the next loop.

Much thanks!


Solution

  • Turns out I missed an important detail in CAN communication,the ACK bit, which needs to be set to recessive by the receiver node. And since I'm only trying to read the CAN bus using one node,that node keeps on transmitting the first message forever in hope to receive the ACK bit. Loopback could've worked but appears like pcan doesn't support loopback functionality for linux. So would have to use a second CAN node to receive messages.