Serial communication

Gert Doering (gert@greenie.muc.de)
Tue, 23 Nov 1999 23:25:42 +0100


Hi,

On Tue, Nov 23, 1999 at 09:45:16AM +0100, Eric Veldhuyzen wrote:
> I know this isn't really the correct place to ask, but since a lot of
> you guys have a lot of experience with serial communications I'd like
> to ask my question here anyway.

Let's try to find some answer.

> I have written a library to send files from my system (linux) to
> another, based on the uucp 'g' protocol (I stripped some functionality
> like variable window size and variable blocksize to save me some
> time). I wanted to use the same library for both socket communication
> and serial line communication, so I use the filedescriptor for reading
> and writing to the device (socket). Reading and write works the same
> way: I first check with select() if the the device is ready, and if it
> is I read/write one character. This also give me the possibility to
> have a timeout on the device.

This is reasonable, but not very CPU effective.

> This all works just fine, but it seems to be quite slow. Especially on
> reading characters, 

... which is normal when doing "read( fd, buf, 1)" stuff, as the system
call overhead is pretty high.  This is something that should be changed 
to "read as many bytes as are there in the kernel buffers [up to the 
maximum that has space in the buffer]".

You need to set the tty to "raw" mode, and "VMIN=1, VTIME=0" (see mgetty's
tio.c, tio_mode_raw()), otherwise the read call might block if there are
not enough bytes present.

That's also what mgetty does, and with that, I was able to sustain four
faxmodems at full speed on a 386/16 with no byte losses.

> writing seems fine. 

Hmmm, if you write large amounts, writing 1 byte at a time should hurt as
well.

[..]
> A college has made a DOS version of this protocol in turbo pascal, and
> his version is a lot faster when receiving, (has the same strange
> delay when he is sending me data) but has the problem that it seems to
> drop characters at receiving. I am not quite sure what happens. When I
> am sending my characters he suddenly doesn't seem to receive any data
> anymore and then times out.

It could be that the serial comms library he is using is not doing IRQ
driven/buffered I/O.  If you don't do that (for example, just use the BIOS
functions), you'll drop characters if you do "something".

When I played around with this ages ago, I found out that just doing a
"printf()" was sometimes enough to drop one byte.

> Is there anyone here who can give me some clues about what to do to
> make all this work correctly? Am I using the wrong method to read
> characters from the device? Or is there some what to speed it up? Is
> there anything else I do wrong here? Can it be caused by some
> modemsettings perhaps?

It could also be a window size problem - if the latency is high (because
of high overhead when reading/writing), it's not possible to saturate the
link, resulting in low throughput.  Try using a higher window size and see
what happens.

gert
-- 
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             gert@greenie.muc.de
fax: +49-89-35655025                        gert.doering@physik.tu-muenchen.de