Read int from CANBUS using Teensy 4.1

Hi,
First of all i'm new to the forum and not sure if i'm in the right category so sorry for my mistakes.
I am trying to communicate an ESP32 with a Teensy 4.1 using CANBUS (FlexCAN_T4 library on Teensy and CAN library on ESP32). ESP32 reads some DS18B20 and sends it to Teensy over CANBUS for Teensy to store it and use it to display on a screen and set off an alarm when its too high etc.

I can send and receive data without a problem but the data is decimal.

CANBUS message sent from ESP32:

CAN.beginExtendedPacket(0xabcdef);
CAN.write('w');
CAN.write('o');
CAN.write('r');
CAN.write('l');
CAN.write('d');
CAN.endPacket();

CANBUS reading from Teensy:

MB: 4  ID: 0xABCDEF DATA: 119 111 114 108 100 0 0 0   TS: 58453

So "119 111 114 108 100 0 0 0" is equal to "world" and i can confirm this by using some online converters:

But how can i convert this in arduino to make it possible to store and process my sensor readings in Teensy?

Thanks, Γ–mer.

in C++ when you write 'w' with the single quotes, you are using a character literal and that is represented in memory with the ASCII code of the letter ➜ here for 'w' it's 119 in decimal, so that's what you get on the other end.

if you were to write

CAN.beginExtendedPacket(0xabcdef);
CAN.write(10);
CAN.write(20);
CAN.write(30);
CAN.write(40);
CAN.endPacket();

you would receive 4 bytes on the other side holding the values (in decimal) 10, 20, 30 and 40.


sending your sensor data could be done in binary, just sending the bytes payload over, or as text representation (a string). It's a design choice.

in binary that would look like this

int16_t temperature = 326; // whatever the raw reading of the sensor is
CAN.beginExtendedPacket(0xabcdef);
CAN.write(& temperature, sizeof temperature); // send the bytes
CAN.endPacket();

for this to work easily you would need to ensure the receiving platform uses the same endianness


regarding the question on how to "store and process the sensor readings" , it's basically the same as handling Serial communication ➜ I would suggest to study Serial Input Basics to handle this

1 Like

Oops! That makes sense. Sorry and thanks. I tried sending hex but never thought of directly sending integers.
Thanks for your efforts.
Best regards.

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