I need to synchronize an IO pin value with a write to a serial port from user space (because I wasn't yet able to do it from kernel space - see my other question). My code (leaving out error checking) is as follows:
char buf[3] = {'U','U','U'};
int fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY); // supposed to be blocking
// fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK); <-- makes no difference
FILE *f = fopen("/sys/class/gpio/gpio200/value", "w"); // the relevant IO
// set IO
fprintf(f, "1");
fflush(f);
// send data
write(fd, buf, sizeof(buf));
// unset IO
fprintf(f, "0");
fflush(f);
The behavior is that the IO is quickly toggled to 1 and back at the start of the write. In other words, write()
returns long before the data has been actually put on the wire.
Is there a hope here?