[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gforth] Example Forth code for polling & reading from serial port f

From: Alexander Shpilkin
Subject: Re: [gforth] Example Forth code for polling & reading from serial port for Linux
Date: Wed, 13 Mar 2019 17:12:33 +0300

(Sorry for the double post.)

On Wed, Mar 6, 2019 at 9:18 PM John Chludzinski <address@hidden>

> Does anyone have an example or a link to an example?

I’m not sure what confuses you, to be honest.  Reading from or writing to a
serial port in Linux is just a matter of opening /dev/tty<whatever> (or
/dev/serial/<whatever> if you’re using udev device names) and reading from
or writing to it just as you would with a normal file, except maybe you’ll
need to flush the buffer at appropriate moments.  I have some code that
does that[1], but it’s really quite trivial given an understanding of the
FILE-ACCESS ANS wordset[1].  I don’t think ANS offers a way to poll for
input from a file, but Gforth at least has KEY?-FILE for that.

What’s non-trivial is the correspondence between the bytes you read or
write and what goes on the wire.  First, there are actual wire protocol
settings like flow control, baud rate, and number of stop and parity bits,
because there is no standard way to negotiate those.  Second, there are
settings for the translation layer that converts between the bytes your
code sees and the bytes that go on the serial port, because original UNIX
baked in a terminal driver into the serial port devices and now we’re stuck
with it[3].  (It’s not a driver for any particular terminal, but rather a
universal driver that can be adapted to a terminal by turning some knobs.
There *is* a configuration of those knobs that means “don’t touch my data,
thank you very much”, usually called “raw mode”.)  All of these are
communicated to the kernel out of band using ioctl(2), usually accessed via
the termios(3) library[4] or a utility like stty(2).  Unfortunately, Gforth
doesn’t provide a way to do this from Forth code.  You could write some C
code to do the job, or invoke stty from Forth using SYSTEM, or just execute
stty from the shell before entering Gforth.  A reasonable default for when
you need the kernel driver to get out of the way is "stty 115200 cs8
-cstopb -parity raw -echo", where you can replace 115200 by the desired
baud rate.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]