0

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 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 I get the same value. What should I do to get the values from arduino to raspberry pi Thanks!

4
  • Try adding a bitrate parameter to can.interface.Bus in RPi script and set it to 115200. Also, you can remove the time.sleep(1) since bus.recv() is blocking.
    – A6EE
    Dec 7 '20 at 16:06
  • 1
    The Pi code is incomplete.
    – joan
    Dec 7 '20 at 16:42
  • @A6EE I tried with bitrate as well sir! Dec 8 '20 at 1:57
  • @joan what shld I do to complete sir? Dec 8 '20 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.