This is my first C program. Hello world! I'm sure this is not a problem for school programmers these days, but they did not have programming when I was in high school. :)
I want to write to the serial port until the line I'm writing returns to me. Then do other things. My code below works for a few seconds and then claims to see the line and ends even if it could not see the line. It behaves the same regardless of the fact that so, I am obviously something very wrong.
Yes, the serial device / dev / kittens is real and, from the terminal, bash echo lines for / dev / kittens are received (echo) on the serial port when the port is looped.
I would really appreciate anyone who could correct my mistakes.
#include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> int fd; char *buff; int open_port(void) { fd = open("/dev/kitens", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open /dev/kittens "); } else fcntl(fd, F_SETFL, 0); return (fd); } int main() { int wr,rd; open_port(); char msg[]="There are mice in the wire.\r"; do { /* Read from the port */ fcntl(fd, F_SETFL, FNDELAY); rd=read(fd,buff,sizeof(msg)); /* Write to the port */ wr = write(fd, msg, sizeof(msg)); printf("debugging - Wrote to port\n"); usleep(10000); if (wr < 0) { fputs("write() to port /dev/kittens failed!\n", stderr); break; } } while (buff != msg); if (buff=msg) printf(buff, "String Found! Now do the work."); /* system("dostuff.sh); */ /* Close the port on exit. */ close(fd); return 0; }
source share