ESP32 + CAN Comm
Posted: Sat Nov 30, 2019 2:53 am
Hello, i have an device that transmit message using CAN protocol. I am trying to comunicate with the ESP32, but i am having troubles to recieve the message.
The way that the device send the data is using an data package of 64 bits see the attachment please!
In this 64 bits, the first 8 bits are a CODE number, the next 16 bits are a real number but i can only recieve the right message for the package that come with 8 bits.
Instead of recieveing 1.3 , i am receiving 1 0, i dont know how to take the 16 bits messages together, or how the real number 1.3 is sent using CAN protocol.
The message that i receive : 102 1 0 39 69 0 0 0
The message that i think that i should receive : 102 1 3 39 69 0 0 0
The code that i am using:
#include <ESP32CAN.h>
#include <CAN_config.h>
#include <driver/can.h>
/* the variable name CAN_cfg is fixed, do not change */
CAN_device_t CAN_cfg;
void setup() {
Serial.begin(115200);
Serial.println("iotsharing.com CAN demo");
/* set CAN pins and baudrate */
CAN_cfg.speed=CAN_SPEED_250KBPS;
CAN_cfg.tx_pin_id = GPIO_NUM_5;
CAN_cfg.rx_pin_id = GPIO_NUM_4;
/* create a queue for CAN receiving */
CAN_cfg.rx_queue = xQueueCreate(10,sizeof(CAN_frame_t));
//initialize CAN Module
ESP32Can.CANInit();
}
void loop() {
CAN_frame_t rx_frame;
can_message_t message;
//receive next CAN frame from queue
if(xQueueReceive(CAN_cfg.rx_queue,&rx_frame, 3*portTICK_PERIOD_MS)==pdTRUE){
//do stuff!
if(rx_frame.FIR.B.FF==CAN_frame_std)
printf("New standard frame");
else
printf("New extended frame");
if(rx_frame.FIR.B.RTR==CAN_RTR)
printf(" RTR from 0x%08x, DLC %d\r\n",rx_frame.MsgID, rx_frame.FIR.B.DLC);
else{
printf(" from 0x%08x, DLC %d\n",rx_frame.MsgID, rx_frame.FIR.B.DLC);
for(int i = 0; i < 8; i++){
printf("%d\t", rx_frame.data.u8);
}
}
}
}
The way that the device send the data is using an data package of 64 bits see the attachment please!
In this 64 bits, the first 8 bits are a CODE number, the next 16 bits are a real number but i can only recieve the right message for the package that come with 8 bits.
Instead of recieveing 1.3 , i am receiving 1 0, i dont know how to take the 16 bits messages together, or how the real number 1.3 is sent using CAN protocol.
The message that i receive : 102 1 0 39 69 0 0 0
The message that i think that i should receive : 102 1 3 39 69 0 0 0
The code that i am using:
#include <ESP32CAN.h>
#include <CAN_config.h>
#include <driver/can.h>
/* the variable name CAN_cfg is fixed, do not change */
CAN_device_t CAN_cfg;
void setup() {
Serial.begin(115200);
Serial.println("iotsharing.com CAN demo");
/* set CAN pins and baudrate */
CAN_cfg.speed=CAN_SPEED_250KBPS;
CAN_cfg.tx_pin_id = GPIO_NUM_5;
CAN_cfg.rx_pin_id = GPIO_NUM_4;
/* create a queue for CAN receiving */
CAN_cfg.rx_queue = xQueueCreate(10,sizeof(CAN_frame_t));
//initialize CAN Module
ESP32Can.CANInit();
}
void loop() {
CAN_frame_t rx_frame;
can_message_t message;
//receive next CAN frame from queue
if(xQueueReceive(CAN_cfg.rx_queue,&rx_frame, 3*portTICK_PERIOD_MS)==pdTRUE){
//do stuff!
if(rx_frame.FIR.B.FF==CAN_frame_std)
printf("New standard frame");
else
printf("New extended frame");
if(rx_frame.FIR.B.RTR==CAN_RTR)
printf(" RTR from 0x%08x, DLC %d\r\n",rx_frame.MsgID, rx_frame.FIR.B.DLC);
else{
printf(" from 0x%08x, DLC %d\n",rx_frame.MsgID, rx_frame.FIR.B.DLC);
for(int i = 0; i < 8; i++){
printf("%d\t", rx_frame.data.u8);
}
}
}
}