Pages: [1]   Go Down
Author Topic: Serial.print  (Read 515 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 612
Posts: 49216
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 612
Posts: 49216
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To send data:
Code:
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:
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:
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.
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: