Serial & strings

Hi!!

Im making some tests using LabView, its really easy to interface with it, but a have some problems:

  • If i send from labview and a dial (0-255) valors, the problem is that it will be send one character at the time.... how can i receive a string of more than 2 characters in the serial port?

i try this:

a = Serial.read();
b = Serial.read();
c = Serial.read();
Serial.print("Se recibio: ");
Serial.print(a);
Serial.print(b);
Serial.println(c);

For receiving 3 characters, but i only get this on the monitor:

Se recibio: 1ÿÿ
Se recibio: 23ÿ

and o would like to receive:

123

jejejeje

can it be done?

how could i do it?

10x!!

cheers!!

you must check to see if characters are available for reading (with Serial.available()). Otherwise, Serial.read() will return garbage (which is exactly what you are seeing).

e.g.

while (Serial.available() < 1 )
  {;}
a=Serial.read();
while (Serial.available() < 1 )
  {;}
b=Serial.read();
...

-j

Thanks!!!

It works!!

my code finish like this:

void loop()
{

while (Serial.available() < 1 )
{;}
arr[0]=Serial.read();
while (Serial.available() < 1 )
{;}
arr[1]=Serial.read();
while (Serial.available() < 1 )
{;}
arr[2]=Serial.read();

/*
Serial.print("Se recibio: ");
Serial.print(arr[0],BYTE);
Serial.print(arr[1],BYTE);
Serial.println(arr[2],BYTE);
*/
arr[0]=arr[0]-48;
arr[1]=arr[1]-48;
arr[2]=arr[2]-48;

cad=(arr[0]*100)+(arr[1]*10)+arr[2];

//Serial.println(cad);

if(cad<256)
if(cad>0)
convertir(cad);
else
convertir(0);
else
convertir(0);

Serial.flush();
}

10x!!!

Cheers!!!

Of course you could do:-

while (Serial.available() < 3 )
{;}
arr[0]=Serial.read();
arr[1]=Serial.read();
arr[2]=Serial.read();

Right, which is what I proposed in the other incarnation of this thread. Grr.. :slight_smile:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1226197859/1#1