0

I want to set interrupt for serial port on linux,so I do it by signal.And the handler of signal hava worded,but I don't know how to get the number of character.Specifically I am not sure the third parameter in read() function when the handler is called by system.So,I need a solution that knows the amount of serial data.

Thanks you all.

PS:My English is not good,so the above may not be clearly expressed

void serialHandler(int sig)
{
    read(fd,buffer,I don't know);
}
  • 1
    The third parameter depends on the amount of data you expect and the size of the buffer you pass to `read()` (whatever is less) – Ctx Jan 28 '19 at 14:38
  • It sounds like you need `aio_read`, which sets up the read with a buffer and specified size limit, and sends you a signal when the read is complete. Once that comes, `aio_return` tells you how many bytes were actually transferred. – Ben Voigt Jan 28 '19 at 15:15
  • Alternatively set the file descriptor to non-blocking. Then read will return with however many character are present. – Goswin von Brederlow Jan 28 '19 at 16:29

1 Answers1

1

Specifically I am not sure the third parameter in read() function when the handler is called by system

read() is described fully here, and includes the following example:

#include <sys/types.h>
#include <unistd.h>
...
char buf[20];
size_t nbytes;
ssize_t bytes_read;
int fd;
...
nbytes = sizeof(buf);
bytes_read = read(fd, buf, nbytes);

It is common to use a loop construct (for example around similar code to that shown above) while testing the output of read for an exit criteria. In the above implementation (not looped) bytes_read contains the number of bytes successfully read, excluding any carriage return characters removed. If a read error or end-of-file ( EOF ) is encountered, the returned value can be less than that specified in the number_ofBytes parameter. If an error occurs, read returns 0 and sets errno to a nonzero value.

Note: As mentioned in the comments, using read() in conjunction with a serial port most likely precludes it will ever see an EOF condition.

Also to expound on the comment about using timeouts with read(), and how to implement a timeout for the read function itself using the select() function.

There is more information here to help with creating algorithms to read from port.

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 1
    It might be worth mentioning however, that a read on a serial port usually never returns EOF. Length and end of data has to be determined by some sort of framing protocol. – Ctx Jan 28 '19 at 14:56
  • Emmmm,It seems that my statement has a problem.I mean that ,for example,three bytes came from the serial port,but I don't know it's three,if I set the third parameter to one,there will be two bytes lost. – brainsding Jan 28 '19 at 15:03
  • 1
    @buyuer The bytes are not lost, you can read them in in a subsequent call to `read()`. – Ctx Jan 28 '19 at 15:05
  • @Ctx-Thank you very much ,I seem got it,and I know what I should do.Thank you again. – brainsding Jan 28 '19 at 16:48