1
\$\begingroup\$

I am trying to communicate between Raspberry Pi and Arduino. In arduino I am using this program to transmit

#include <SPI.h>
#include <mcp_can.h>

const int spiCSPin = 10;

MCP_CAN CAN(spiCSPin);

void setup()
{
    Serial.begin(115200);

    while (CAN_OK != CAN.begin(CAN_125KBPS))
    {
        Serial.println("CAN BUS init Failed");
        delay(100);
    }
    Serial.println("CAN BUS Shield Init OK!");
}

unsigned char stmp[8] = {1, 2, 3, 4, 5, 6, 7, 8};
   
void loop()
{  
  Serial.println("In loop");
  CAN.sendMsgBuf(0x43, 0, 8, stmp);
  delay(1000);
}

In Raspberry pi I am using this program to recieve

import can
import time

can_interface = 'can0'
bus = can.interface.Bus(can_interface,bustype='socketcan'
while True:
 message = bus.recv()
 print(message)
 time.sleep(1)

All I get is

Timestamp: 1607187085.129426        ID: 0020    S                DLC:  8    00 19 00 01 03 01 04 01     Channel: can0

everytime What should I do to recieve values I sent from arduino to Raspberry pi Thank you

\$\endgroup\$
4
  • \$\begingroup\$ Looks like you are only printing trash from memory, so I would suspect that the problem is in the receiver code. \$\endgroup\$
    – Lundin
    Commented Dec 8, 2020 at 7:29
  • \$\begingroup\$ Exactly sir , the problem was the transmitter side clock! And so the reciever couldn’t get it correctly \$\endgroup\$ Commented Dec 8, 2020 at 18:06
  • \$\begingroup\$ In that case you should be receiving error frames, not random garbage. \$\endgroup\$
    – Lundin
    Commented Dec 9, 2020 at 7:40
  • \$\begingroup\$ True sir , I synchronized the clock and got the values \$\endgroup\$ Commented Dec 10, 2020 at 8:35

1 Answer 1

1
\$\begingroup\$

Change

while (CAN_OK != CAN.begin(CAN_125KBPS))

to

while (CAN_OK != CAN.begin(CAN_500KBPS,MCP_8MHz))

And

bus = can.interface.Bus(can_interface,bustype='socketcan’,bitrate=500000)

And

In /boot/config.txt (in RPi)

Set oscillator=8000000

Finally ensure bitrate and clock are same in both RPi and Arduino

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.