This is how I've made do in gforth without the low level i/o words.  It
seems to work fine as long as I don't require key?.  The thing you gain
from low level i/o is non-blocking.  I intend to figure out the libc
stuff and imitate the kforth example, but I'm pretty busy right now.

\ ------------- Example. -------------- /

\ Serial I/O in Linux with Gforth.

1 value com?
9600 value current-baudrate

\ a list of four strings
here ," /dev/ttyS3"
here ," /dev/ttyS2"
here ," /dev/ttyS1"
here ," /dev/ttyS0"
create 'portnames   , , , ,

: "portname   (  - a u)   
        com? 4 min 1 max 1- cells 'portnames + @ count ;

\ Newer versions of stty allow the -F option to configure a serial port
\ other than the one you're using for your terminal.  Older versions
\ accomplish the same thing with < for redirecting input from the serial
\ port.  The version with < probably works for all systems.
\ : "stty   (  - a u)   s" stty -F " ;
: "stty   (  - a u)   s" stty < " ;

: "N81   (  - a u)
        s"  -parenb cs8 -cstopb -crtscts raw -echo" ;

: n81   (  - )
        "stty pad place   "portname pad +place
        "N81 pad +place   pad count system ;

: baud   ( n - )
        "stty pad place   "portname pad +place
        0 <# #s bl hold #> pad +place   pad count system ;

variable serial-fid

: open-comm   (  - )
        N81 current-baudrate baud
        "portname r/o open-file throw serial-fid ! ;

: close-comm   (  - )   serial-fid @ close-file throw ;

: port   ( n - )
        create  ,
        does>   close-comm @ to com? open-comm ;
1 port com1
2 port com2
3 port com3
4 port com4

: key-s   (  - c)
        pad 1 serial-fid @ read-file 0= 1 and - ?serial-problem
        pad c@ ;

: emit-s   ( c - )
        "portname w/o open-file throw
        tuck emit-file throw close-file throw ;

\ -------------- End of example. --------------- /

This is extracted from amrForth for Linux, a target compiler and
interpreter for microcontroller development.  See [www.amresearch.com]
for more details.

Charley.


-- 
Charles Shattuck
AM Research, Inc.
[EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to