Re: Blocking read() from a USB->Serial adapter.



Charles Sullivan wrote:
On Sat, 25 Nov 2006 01:49:03 +0100, Bjoern Schliessmann wrote:

Charles Sullivan wrote:

A blocking read() of more than one character from an actual RS232
serial port returns when the number of characters specified has
been read.
No. Have a look at man 2 read. Note the word "attempts".

| READ(2) Linux Programmer?s Manual READ(2)
|
| NAME
| read - read from a file descriptor
|
| SYNOPSIS
| #include <unistd.h>
|
| ssize_t read(int fd, void *buf, size_t count);
|
| DESCRIPTION
| read() attempts to read up to count bytes from file
| descriptor fd into the buffer starting at buf.
|
| If count is zero, read() returns zero and has no other
| results. If count is greater than SSIZE_MAX, the result is
| unspecified.
|
| RETURN VALUE
| On success, the number of bytes read is returned [...]
However I've found that a blocking read() of more than one
character from a USB->Serial adapter reads only the first
character before returning and I have to loop to read all the
characters.
- You have to do this no matter which FD you read from. If you
don't, 100 times the reading may succeed and the 101. time it may
not. Funny bugs may arise.

I can understand the "attempts to read" for a non-blocking read -
returning fewer characters than specified if the characters
aren't immediately available to be read.

But I had the impression that a blocking read is supposed to block
forever or until it either reads the specified number of characters
or is interrupted. You're saying maybe it will and maybe it won't
and that's to be expected.

fread works that way.
read however, reads /up to/, meaning it might read less than, the nr of bytes you specify.

.



Relevant Pages