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



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.

- Why is this a problem? Considering you use a serial interface with
blocking calls the overhead of a C loop shouldn't be a problem at
all.

Not a problem. I am just puzzled why reading from the adapter
differs from reading from an actual serial port.

BTW, the number of characters to be read is never more than 10.

Regards,
Charles Sullivan

.



Relevant Pages

  • Re: Serial port monitoring
    ... if (!characters) return; ... you would not want to use blocking I/O on a serial port. ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)
  • Re: keybd_event problems.
    ... applications that use it for various things like serial port wedging. ... These don't map to the ascii table at all, ... lower case non language characters? ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: sio interrupt-level buffer overflows
    ... That generally means that the application reading the ... serial port is unable to keep up with the receive rate. ... operates from an interrupt and simply stores the characters ... > I have a problem with my sio0 interface which my modem is connected to. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: display output from serial port device
    ... If your program fails to open the serial port, ... If you do want parity, restore INPCK, but also add PARENB to ... always get the right number of characters. ...
    (comp.os.linux.development.system)
  • RE: SerialPort read&write members display string value above 127 as "?
    ... Serial Port Class methods of Read and Write default to ASCII code ... namespace EncodeTest1 ... // A Unicode string with two characters outside an 8-bit code ... "This unicode string contains two characters " + ...
    (microsoft.public.dotnet.framework)