11 years, 7 months ago.

CAN Bus Transmit

Hi,

I'm trying to send sensor values from one mbed controller to another mbed controller through CAN bus , i'm able to receive the message from the other side but the message data is garbled .

The read function returns the float value from 0.0 to 1.0 and i have done a type cast from an float pointer to an char pointer i guess that's giving some problems. Please give suggestions on the same and i have put up the hyperterminal screenshots and the code for the same .

Please let me know what changes have to be made in the code .

Transmitter Code

<<code main.cpp>>

  1. include "mbed.h" CAN can1(p30, p29); AnalogIn pin(p18);

void send(char *y) {

if(can1.write(CANMessage(20, y,1))) { printf("\n Message sent: %s", *y); } }

int main() { volatile float x; while(1) { x=pin.read(); printf("\n The value read by the sensor is = %f",x);

send((char *)&x); wait(0.3); } }

<</ code>>

Receiver Code

<< code main.cpp>>

  1. include "mbed.h"

CAN can(p30, p29);

int main() {

CANMessage msg;

while(1) {

if(can.read(msg)) {

printf("\n Message id :%d", msg.id);

printf("\n Message received: %f", msg.data);

wait(0.1);

} }

} <</ code>>

Receiver Hyperterminal

/media/uploads/sanpai/rxnode1.png

Transmitter Hyperterminal

/media/uploads/sanpai/can_sensor.png

2 Answers

11 years, 7 months ago.

You can not send Float or numbers with decimal points through CAN bus. The data send through a CAN Bus should be in a character format and integers, eg '1', '2', '3' .... . Your problem is that the data waiting to be send is converted into an integer in char form thus rounded, and because is always 0.00something is always rounded down to 0.

I had the same problem using a sensor in a project. The way that I did it was to take the sensor reading, multiply with 100 and then send it. On the receiver I was dividing the data with 100, after converting it to decimal and I had my reading. In your case because your number is very small try to multiply with 1000 I would suggest.

The way to do it on the receiver is take the data as a char and divide by the constant multiplier you are using and then a single addition of 48, if I remember correctly, will give you the correct reading. The addition of 48 is due to the conversion of char to decimal.

Hope it helps, Georgios

Accepted Answer
11 years, 7 months ago.

Hello Georgious,

Even i did the same , i multiplied the sensor value with 100 and transmitted . It seems to be working fine now .

I am sorry i came across the same issue and browsed this thread. One further clarification is if a message is some standard message (example J1939) and has some offset and multiplication factor which i cannot edit and I am only a sender then what should i do? For example i want to send 12.35 multiplication factor is 0.25 and offset is 0. Then while sending I think i have to multiply with 4 (1/0.25) and send it. I cannot multiply with 100 in such cases what i have to do? Please help.

posted by Vihaan Reddy 19 Jan 2016