5 years, 1 month ago.

Facing issue in can.read(CANMessage msg)

I am trying to establish CAN communication between two nodes. I was successful in sending data but, not able to read the data in the CAN bus. I can sense that can.read() is not functioning in my code. Please suggest me a solution for this.

//#define TARGET_STM32F103C8T6    1       // uncomment this line to use STM32F103C8T6 boards

//#define BOARD1  1                       // comment out this line when compiling for board #2

#if defined(TARGET_STM32F103C8T6)
#define LED_PIN PC_13
const int           OFF = 1;
const int           ON = 0;
#else
#define LED_PIN LED1
const int           OFF = 0;
const int           ON = 1;
#endif
#if defined(BOARD1)
const unsigned int  RX_ID = 0x100;
const unsigned int  TX_ID = 0x101;
#else
const unsigned int  RX_ID = 0x101;
const unsigned int  TX_ID = 0x100;
#endif
#include "mbed.h"
#include "CANMsg.h"
 
Serial              pc(USBTX, USBRX);
CAN                 can(PB_8, PB_9);    // CAN Rx pin name, CAN Tx pin name
CANMsg              rxMsg;
CANMsg              txMsg;
DigitalOut          led(LED_PIN);
Timer               timer;
uint8_t             counter = 0;
 

void printMsg(CANMessage& msg)
{
    pc.printf("  ID      = 0x%.3x\r\n", msg.id);
    pc.printf("  Type    = %d\r\n", msg.type);
    pc.printf("  Format  = %d\r\n", msg.format);
    pc.printf("  Length  = %d\r\n", msg.len);
    pc.printf("  Data    =");
    for (int i = 0; i < msg.len; i++)
        pc.printf(" 0x%.2X", msg.data[i]);
    pc.printf("\r\n");
}
 

int main(void)
{
    pc.baud(9600);                              // set serial speed
#if defined(BOARD1)
    led = ON;                                   // turn the LED on
    timer.start();                              // start timer
    pc.printf("CAN_Hello board #1\r\n");
#else
    led = OFF;                                  // turn LED off
    pc.printf("CAN_Hello board #2\r\n");
#endif
    can.frequency(1000000);                     // set CAN bit rate to 1Mbps
    can.filter(RX_ID, 0xFFF, CANStandard, 0);   // set filter #0 to accept only standard messages with ID == RX_ID
    while (1) {
        if (timer.read_ms() >= 2000) {
            // check for timeout
            timer.stop();                       // stop timer
            timer.reset();                      // reset timer
            counter++;                          // increment counter
            txMsg.clear();                      // clear Tx message storage
            txMsg.id = TX_ID;                   // set ID
            // append data (total data length must not exceed 8 bytes!)
            txMsg << counter;                   // one byte
            if (can.write(txMsg)) {
                // transmit message
                led = OFF;                      // turn the LED off
                pc.printf("-------------------------------------\r\n");
                pc.printf("-------------------------------------\r\n");
                pc.printf("CAN message sent\r\n");
                printMsg(txMsg);
                pc.printf("  counter = %d\r\n", counter);
            }
            else
                pc.printf("Transmission error\r\n");
        }
 
        if (can.read(rxMsg)) {
            pc.printf("-------------------------------------\r\n");
            pc.printf("CAN message received\r\n");
            printMsg(rxMsg);
            led = ON;                           // turn the LED on
            if (rxMsg.id == RX_ID) {
                rxMsg >> counter;
                pc.printf("  counter = %d\r\n", counter);
            }
 
            timer.start();                      // to transmit next message
        }
    }
}

1 Answer

5 years, 1 month ago.

Hi there,

I rewrote your code for one board with two nodes and your code works well. So no problems with can.read(rxMsg). Tested on Nucleo F767ZI with DIY dual Can shild (2xMCP2551) and MbedOS5 bare metal profile.

Maybe check your connection.

BR, Jan

Hi Jan Kamidra, I was successful in sending data. Can you please tell me some common connection errors? I am using texas CAN transceiver SN65HVD230DR and NUCLEOF103RB(this has one inbuilt can controller). One more thing I observed was both CANH and CANL are at the same voltages(i.e 2.6V). I am using serial monitor to observe the code at a baud rate of 9600 and my CAN bus is operating at 1Mbps. Does this affect my observation?

posted by Dheeraj Etta 29 Oct 2019

Do you have terminators (2* 120ohm rezistors)?

When you have, for example, one message per second, the baud rate 9600 is enough.

posted by Jan Kamidra 29 Oct 2019

Hi Jan Kamidra, I am still not able to run the code in NUCLEO-F103RB. I have checked connections properly and there are no errors in it. The code is also right as it was running on your board. I feel that there is some problem with MBED OS. My board supports only MBED OS 2 and MBED OS 5.4. Do these MBED OS versions matter much? Please tell me a solution to this.

posted by Dheeraj Etta 31 Oct 2019

Hi, I do not have a F103 board but when I tried to add it to online compiler, it was able to compile latest MbedOS5. So, please try to update to MbedOS5.14 or MbedOS5.14 with bare metal profile. We will see what happens.

posted by Jan Kamidra 31 Oct 2019

Hi, I have also tried with NUCLEO-L4R5ZI, I am facing the same issue as I was facing the board NUCLEO-F103RB. I also tried by using different transceivers then also it didn't work. I am not understanding what to now. Can you please share the output you were getting when you compiled the above code on NUCLEO-F767ZI?

posted by Dheeraj Etta 05 Nov 2019

Hi, the output in the serial monitor is…

Init
-------------------------------------
-------------------------------------
CAN1 message sent
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x01
  counter = 1
-------------------------------------
CAN2 message received
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x01
-------------------------------------
-------------------------------------
CAN1 message sent
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x02
  counter = 2
-------------------------------------
CAN2 message received
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x02
-------------------------------------
-------------------------------------
CAN1 message sent
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x03
  counter = 3
-------------------------------------
CAN2 message received
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x03
-------------------------------------
-------------------------------------
CAN1 message sent
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x04
  counter = 4
-------------------------------------
CAN2 message received
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x04
-------------------------------------
-------------------------------------
CAN1 message sent
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x05
  counter = 5
-------------------------------------
CAN2 message received
  ID      = 0x100
  Type    = 0
  Format  = 0
  Length  = 1
  Data    = 0x05
-------------------------------------
posted by Jan Kamidra 05 Nov 2019

Hi again,

today I have little more time and for sure I tried this again on two boards (F767ZI and F446RE) connected together via MCP2551 transceivers and can-bus. It works great.

Still it looks like wiring issue.

BR, Jan

posted by Jan Kamidra 06 Nov 2019