(on a linux system) i wrote the following code snippet to
1. write to a serial port
2. read the data that i have just written to the serial port.
the write is functioning, but the read is not.
if i use just the read (without "write", but by connecting a serial device), it is working fine.
int main(int argc, char *argv)
int file= open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(file == 0)
perror("open_port: Unable to open /dev/ttyS0 - ");
fcntl(file, F_SETFL, 0);
printf("enter input data:\n");
printf("cannot write to port\n");
fcntl(file, F_SETFL, FNDELAY);
printf("cannot read from port\n");
/*CODE TO EXIT THE LOOP GOES HERE
enter input data:
cannot read from port
Print the value of the file descriptor. When I printed the value I got -1.
One reason could be, as the article mentioned that I am unable to open the file due to lack of permissions.
Have you taken care of this issue ?
i have. i am the root user, so i had the required permissions.
in the other examples i came across, there are mentions of "configuring the port".
is this essential? reading or writing works fine separately. when i do both it isn't working.