Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 2432

General discussion • CAN MCP2515 and rpi Zero data read issue

$
0
0
I am using the Raspberry pi zero and MCP2515 can interface. I have a device that sends the data over querry. But i am unable to receive any data from it.
I have connected the Spi and the interrupt on GPIO 25.
I run the following command after rebooting:

Code:

sudo /sbin/ip link set can0 up type can bitrate 500000

My config.txt file:

Code:

dtparam=spi=ondtoverlay=mcp2515-can0,oscillator=8000000dtparam=interrupt=25,spimaxfrequency=500000dtoverlay=spi-bcm2835-overlay

my code to receive the message:

Code:

import timeimport canclass CAN_MSG:    def __init__(self):        self.id = 0        self.rtr = 0        self.len = 0        self.buf = [0] * 8def test():    rx_id = 0    rx_data = [0] * 8    rx_len = 0    query_0x100 = CAN_MSG()  # New instance for query with ID 0x100    query_0x100.id = 0x100    query_0x100.rtr = 0    query_0x100.len = 8    query_0x100.buf[0] = 0x00    query_0x100.buf[1] = 0x00    query_0x100.buf[2] = 0x00    query_0x100.buf[3] = 0x00    query_0x100.buf[4] = 0x00    query_0x100.buf[5] = 0x00    query_0x100.buf[6] = 0x00    query_0x100.buf[7] = 0x00    can_interface = 'can0'    bus = can.interface.Bus(channel=can_interface, bustype='socketcan')    try:                while True:            # Send query message            message_0x100 = can.Message(                arbitration_id=query_0x100.id,                data=query_0x100.buf,                is_extended_id=bool(query_0x100.ext),            )            bus.send(message_0x100)                        # Receive response            rx_msg = bus.recv(timeout=0.1)            if rx_msg is not None:                rx_id = rx_msg.arbitration_id                rx_data = rx_msg.data                rx_len = rx_msg.dlc                                print(f"Received message: ID=0x{rx_id:X}, DLC={rx_len}, Data={rx_data}")                # Process received data if needed                        # Publish received data to MQTT            data = {"received_id": rx_id, "received_data": rx_data}                           time.sleep(1)                except Exception as ex:        print(ex)if __name__ == "__main__":    test()

I don,t receive any message from the device. I am not able the figure out the issue.

Statistics: Posted by Axon_11 — Mon Apr 22, 2024 11:25 am



Viewing all articles
Browse latest Browse all 2432

Trending Articles