Hi, I have a character string of serial data that i want to converts to bytes for transmission over the CAN bus. Now to convert from a character to bytes i have the code:
data = Serial.write(string);
I'm not sure if this works.
Also at the receiving node i need to convert it back to a character string. I have searched the internet but can't find any information and have also searched the forums with no success. Please can you suggest how i can do this?
A string is essentially an expensive array of chars ( 8 bit bytes ), so you will probably find it easier just implementing your own array where you can access each element directly.
Thanks for the reply. I did read the documentation but still made the mistake anyway.
I have got some code working now to convert a string to bytes, then transmit these bytes and then convert them back to a string the other side. However at the other side it just outputs the first character. Here is my code:
Sorry, I don't have any knowledge on the CAN library. Make sure all the info you pass to the read function is correct.
CAN.readDATA_ff_0(&length,frame_data,&frame_id,&ext,&filter);
P.S. I just checked the value of data.length() and it has a value of 20 which is correct in this case.
You do a 'length = data.length();' on transmit side, does the length need to be known on the receiver side?
if frame_data is an array of chars, frame_data[0] will only output one character.
Hard to say as the code you left out is quite relevant it seems.
The function CAN.readDATA_ff_0 is definitely correct.
The length does need to be known as far as i know, but even if not it will be useful for me to know at the receiver side.
frame_data is a byte array.
Thanks for the help and suggestions, I hope this clears up your questions.
I tried the code but it doesn't work. To be honest i'm now a little confused myself. The output was T, , , , , , , , and the character after T, changed through random characters like the alphabet (in order). I was looking through the code and as the data string is stored only in frame_data[0] i wouldn't expect cycling through the whole byte array to display the correct data. However i would expect byte 1 of the array to count up from 1 as it's the packet number byte which displays correctly when i use my output code...