zappy zappy - 1 month ago 8
Linux Question

Linux read() call not returning error when i Unplug serial cable

I am new to C and Linux. I am trying to read some data from the serial port by using the

read()
system call, and it is working fine. Problem is that
read()
is not returning a negative value even if I unplug the serial cable while the code is running.Please help

Please feel free to ask if the scenario is not clear.Thanks for your time

Edit:
It returns a Zero when I unplug the cable.

char cBuff[100];
buffptr = cBuff;
while (( nbytes = read(fd, buffptr, cBuff + sizeof(cBuff) - buffptr - 1)) > 0)
{
//Do something
}

if(nbytes < 0 )
{
perror("Serial Read Thread ERROR:");
}
else if(nbytes >= 0 )
{
//Do something
}

Answer

Unplugging the serial cable has only the effect that no data cannot arrive any longer, but there is no real "connection" that wound be interrupted somehow.

Your serial port now only notices that no further "0" values arrive as the line now remains on "1" constantly.

Consequently, the driver just sees that there aren't any data, but not that there can't be any data as the plug is unplugged.