0

If I send the following string to the rpi

echo "#q10"  > /dev/ttyUSB0

Why does the whole string not get read each time its sent? This is the app output

See code below

The code is as follows. I am using the serial wiring library from Gordons project

#include <stdio.h>
#include <wiringSerial.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

int fd;

struct obj_Properties
{
char *one;
char *two;
char *three;
char *four;
};

int main()
{
if ((fd = serialOpen("/dev/ttyUSB0", 9600)) < 0)
{
 return 1;
}

for(;;){
while (serialDataAvail (fd))
 {
 struct obj_Properties prop;
 prop.one = serialGetchar(fd);
 prop.two = serialGetchar(fd);
 prop.three = serialGetchar(fd);
 prop.four = serialGetchar(fd);
 printf ("%c %c %c %c\n", prop.one, prop.two, prop.three, prop.four);

 fflush (stdout) ;

}
usleep(10000);
}
}
2

As soon as the first byte is available you read four bytes. The first byte being ready does not imply all four bytes (plus line feed) have been received.

  • I added an extra char in the code and one less in the string and it is working. It seems obvious now but even though I used differing string sizes I never tested for 2 extra chars - thanks.. – peterretief Aug 23 '14 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.