1

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
}
zappy
  • 1,864
  • 3
  • 18
  • 36

3 Answers3

2

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.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

read won't return an error after you unplug serial cable.

i'll explain you with this little exemple :

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define READ_SIZE 2047

int     main(void)
{
  char  str[READ_SIZE + 1];
  int   ret;
  int   fd;

  fd = open("test.txt", O_RDONLY | O_CREAT); // create a file
  system("rm test.txt"); // delete the file
  ret = read(fd, str, READ_SIZE); // read on a file that doesn't exist
  printf("%d\n", ret); // print the return value of read
  close(fd);
  return (0);
}

this program will print 0, and not -1 like you thought because read doesn't treat this like an error it just see they're nothing to read and return the number of bytes red or 0.

plean
  • 124
  • 1
  • 9
  • Your example is way off. Your file indeed *does* exist (supposing you are on Linux or alike) as long as you have an open file descriptor on it. It is just empty so that you are on EOF and thus don't have anything to read. – glglgl Oct 24 '16 at 11:25
  • @glglgl the **file desciptor** exist but i removed the **file** before reading it – plean Oct 24 '16 at 12:18
  • 1
    You removed the **file entry in the directory**. The files ceases to exist once all file entries and all file descriptors referencing it are gone. – glglgl Oct 24 '16 at 12:18
  • ho, I wasn't aware of that behavior, thanks for the clarification – plean Oct 24 '16 at 12:21
-2

read won't return errors in this case. As there are number of bytes (zero or more) has been already read.

According to read man page:

It is not an error if this number (return value) is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.

You can check whether read returned -1. Then, check errno for error details.

Mahmoud Mubarak
  • 139
  • 1
  • 11