Convert string to byte and back again

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?

Thanks

No that won't work.
Read the serial documentation before assuming its functionality.
Especially the return values.

http://arduino.cc/en/Serial/read

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:

Transmit side:

String data = "Transmit Test String";
byte candata[90];
byte packet_number= 0;

byte length,rx_status,i;
uint32_t frame_id;
byte frame_data[8]; 
byte filter,ext;

data.getBytes(candata, data.length()); 
packet_number++;
			
frame_data[0] = *candata;
frame_data[1] = packet_number;
frame_id = *candata << 3;  
length = data.length();
	  
CAN.load_ff_0(length,&frame_id,frame_data,false);
			 
Serial.print(",\n");
delay(250);

Receive side:

rx_status = CAN.readStatus();

if (CAN.buffer0DataWaiting()) {
CAN.readDATA_ff_0(&length,frame_data,&frame_id,&ext,&filter);
Serial.print("Packet Number: ");
Serial.print(frame_data[1]);
Serial.print("\n");
Serial.print("Packet Data: ");
Serial.write(frame_data[0]);
Serial.print("\n");

Just to let you know i haven't included all the code here, only what is relevant, i hope you are still able to help me though.
Thanks,
James

P.S. I just checked the value of data.length() and it has a value of 20 which is correct in this case.

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 think the output code is a little off, I may be wrong but, you use packet data as the number then only output one byte.

This uses the id for the number and loops through the 8 data bytes.

Serial.print("Packet Number: ");
Serial.println(frame_id);
Serial.print("Packet Data: ");

for( int i_Index = 0 ; i_Index < 8 ; ++i_Index ){

  Serial.write(frame_data[ i_Index ]);
  if( i_Index < 7 ) Serial.write(", ");
}
Serial.print("\n");

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...

I was looking through the code and as the data string is stored only in frame_data[0]

frame_data[0] is a single element ( 1 byte long ),a string is a 'string' of elements.

Okay, thanks for your help! I have it working fine now so long as i only specify one character per frame_data[] cell!

I really appreciate your help :slight_smile:

No worries :slight_smile:
You should have a read through [this] when you have some spare time, the arrays section would clear up a few things.

Thanks, I will do. I have done C++ but only for one semester/12 weeks.