Issue with reading CAN data using UART serial CAN Bus device

Hi,
I hope this email finds you well. I am currently facing an issue with reading CAN data using a UART serial CAN Bus device, and I would greatly appreciate any assistance or insights you can provide.

In my code, I am sending the data using the system("cansend can0 7df#01020304") command. I have verified that the data is being successfully received when using the candump can0 command with the Canable Pro device. However, when I attempt to read the same data using the UART serial CAN Bus device, I am not seeing any data.

Here is the code I have used for receiving the CAN frame:

#include <SPI.h>
#include "mcp_can.h"
#define SPI_CS_PIN 9

MCP_CAN CAN(SPI_CS_PIN); // Set CS pin

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

while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS FAIL!");
delay(100);
}
Serial.println("CAN BUS OK!");
}

void loop()
{
unsigned char len = 0;
unsigned char buf[8];

if(CAN_MSGAVAIL == CAN.checkReceive())            // check if data coming
{
    CAN.readMsgBuf(&len, buf);  //read data,len: data length, buf: data buf

    unsigned long canId = CAN.getCanId();
    
    Serial.println("-----------------------------");
    Serial.print("Get data from ID: ");
    Serial.println(canId, HEX);

    for(int i = 0; i<len; i++)    // print the data
    {
        Serial.print(buf[i], HEX);
        Serial.print("\t");
    }
    Serial.println();
}

}

I would be grateful if anyone could provide some insights or suggestions on what might be causing this issue and how to resolve it. Additionally, if there are any specific settings or configurations that need to be considered when using a UART serial CAN Bus device, please let me know.

Thank you in advance for your help.

Thanks,
Vani

Moved to Project Guidance from Uncategorized. Please don't post in Uncategorized again.

can you give a link to the " UART serial CAN Bus device"?
also details of how you have connected the device?
how have you terminated the bus?
what microcontroller are you using?

Hi, @myname_vani
Welcome back to the forum.

Thanks... Tom.. :smiley: :+1: :coffee: :australia:

Hi,
Please find the link to Serial CAN BUS Module

#include "mcp_can.h"
#define SPI_CS_PIN 9

MCP_CAN CAN(SPI_CS_PIN); // Set CS pin

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

while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS FAIL!");
delay(100);
}
Serial.println("CAN BUS OK!");
}

void loop()
{
unsigned char len = 0;
unsigned char buf[8];

if(CAN_MSGAVAIL == CAN.checkReceive())            // check if data coming
{
    CAN.readMsgBuf(&len, buf);  //read data,len: data length, buf: data buf

    unsigned long canId = CAN.getCanId();
    
    Serial.println("-----------------------------");
    Serial.print("Get data from ID: ");
    Serial.println(canId, HEX);

    for(int i = 0; i<len; i++)    // print the data
    {
        Serial.print(buf[i], HEX);
        Serial.print("\t");
    }
    Serial.println();
}

}```
To establish communication, I have connected the CAN end of the Serial CAN Bus Module to my device. Through this connection, I am able to send CAN data using the command "System("cansend can0 7df#01020304")" within my application code. This command allows me to transmit the CAN data effectively.
On the other end of the Serial CAN Bus Module, I have connected a USB UART interface, which enables me to read the sent CAN data using a program such as PuTTY.
Thank you for your attention and support.

Regards,
Vani

the labrary MCP_CAN_lib you are using in the code of post 5 is for a Canbus shield with an SPI interface, e.g. mcp2515-can-module-arduino-tutorial
the Canbus module you are using has a serial interface and is supported by the Serial_CAN_Arduino library

what Arduino are you using? how have you connected it to the Canbus module?
what is the Canbus connected too? upload a schematic of your wiring?
I find a USB-CAN adapter useful for monitoring and debugging Canbus networks

Hi horace,

There was a mistake in the circuit connection . Thank you for your support.

However, I am currently facing another challenge and would greatly appreciate your assistance once again. I am trying to establish two-way communication using a Serial CAN Bus Module, but I seem to be encountering difficulties. The issue lies within my current code, as I can only either send or receive data, but not both simultaneously.

I would be grateful if anyone could provide some insights or suggestions on what might be causing this issue and how to resolve it. Additionally, if there are any specific settings or configurations that need to be considered when using a Serial CAN Bus device, please let me know.

Thank you in advance for your help.

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

#define SPI_CS_PIN 9

MCP_CAN CAN(SPI_CS_PIN); // Set CS pin

#define UART_DATA_SIZE 10

byte inputData[UART_DATA_SIZE] = {0};
bool dataComplete = false;
int recvLen = 0;

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

while (CAN_OK != CAN.begin(CAN_500KBPS))    // init can bus : baudrate = 500k
{
    Serial.println("CAN BUS FAIL!");
    delay(100);
}
Serial.println("CAN BUS OK!");

}

void loop()
{
byte d[11] = {0};
unsigned char len = 0;
unsigned char buf[8];
int l=toBytes(d);
if(l<=0) return;

Serial.println(l);

for (int i = 0; i < l+2; i++)
{
Serial.print(" ");
Serial.print(d[i], HEX);
}

if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf

    unsigned long canId = CAN.getCanId();
    
          
    Serial.println(canId, HEX);

    for(int i = 0; i<len; i++)    // print the data
    {
        Serial.print(buf[i], HEX);
        Serial.print("\t");
    }
    Serial.println();
}

}

int toBytes(byte *d) {
char data[10] = {0};
while (!Serial.available()) ; // wait
// get tag and length
Serial.readBytesUntil('\n', data, 4);
Serial.println(data);
int tag=0, len1=0;
if(sscanf(data, "%02x%02x", &tag, &len1) != 2)return 0;
d[0]=tag;
d[1]=len1;
// read values
for (int i = 0; i < len1; i++) {
char data[10] = {0};
Serial.readBytesUntil('\n', data, 2);
Serial.println(data);
int value=0;
sscanf(data, "%02x", &value);
d[2+i]=value;
}
return d[1];
} `

use code tages </> to format you code it will make ir easier read

in the toBytes() function you wait for serial input which will stop the program until you enter Serial data
try

if (!Serial.available()) return;

which, f no serial data is available, will return to loop() i and check for canbus data

could you give a schematic of your system idenifing the components and how they are connected?
also a description of how you expect the system to operate

Hi,

Why do you want to simultaneously Rx and TX?

What is your project?

What is the application?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.