I have the following program that tries to read data from an arduino using the serial port, the thing is it mostly doesn't read anything, except sometimes it reads a piece of what I'm sending. The arduino code its just writing a single letter in a loop.
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main() {
int serialfd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (serialfd == -1)
perror("Error opening the serial port");
else
fcntl(serialfd, F_SETFL, 0);
fprintf(stdout, "Device is open, attempting read \n");
fcntl(serialfd, F_SETFL, 0);
char buf[11] = {0};
read(serialfd, buf, 10);
fprintf(stdout, "Buffer: %s", buf);
close(serialfd);
return 0;
}
for example the output is like this
Device is open, attempting read
Buffer: AAAAAAAAAAA⏎
If I try to run it again (several times) I just get the 0'd buffer
Device is open, attempting read
Buffer: ⏎