Go Down

Topic: Serial.print (Read 565 times) previous topic - next topic

chesskid

Hey everybody,

I want to encode three different characters into Serial.print(ABC); and send this information over xbee. The receiving xbee calls int incomingByte = Serial.read(); . My question is how can i convert this int into a string so that i can access each individual character.

PaulS

The Serial.print() function sends string representations of the input to the serial port.

The Serial.read() function reads that string one character at a time. It is up to you to collect the data in an array or String object.

This topic only comes up about once a day. A few seconds with the search button would be time well spent.

chesskid


This topic only comes up about once a day. A few seconds with the search button would be time well spent.

I did use the search button and found nothing that answered my question,maybe you can provide me with a link thanks again.

I dont quite understand you reply, Serial.read() returns an integer my question is how can I retrieve the original ABC from this integer representation. If you have some coding that would be helpful
Cheers



PaulS

To send data:
Code: [Select]
int x = 12;
Serial.print(x);

This results in "12" arriving at the serial port, and being stored in the serial receive buffer.

To read the data:
Code: [Select]
while(Serial.available() > 0)
{
   int inByte = Serial.read();
}

This will result in inByte being assigned the value '1' at one point, and the value '2' at another point.

If you set up an array, and store each value read into that array, you could recreate the value 12.

Code: [Select]
char inData[10];
int index = 0;

while(Serial.available() > 0)
{
   int inByte = Serial.read();
   if(index < 10)
   {
     inData[index++] = inByte;
     inData[index] = '\0'; // Terminating NULL
   }
}
// At this point, inData MIGHT contain a whole packet...


To ensure that inData contains a whole packet, and end-of-packet marker needs to be sent. The carriage return/line feed added by Serial.println() are sufficient. Then, read data in the while loop, until the end-of-packet marker arrives. Then, you can parse it and reset stuff.

tkbyd

Forgive me if I'm missing something in the context here, but if there's nothing stopping you using the NewSoftSerial library, then do. If only for the avail() function.

More on these matters at...

http://sheepdogguides.com/arduino/art4SeIntro.htm

Go Up