From Newsgroup: comp.lang.forth
The RS232 serial port interface for kForth (-32/64 for Linux) has been
updated to be able to provide user access to the modem bits e.g. DSR,
CTS etc.
https://github.com/mynenik/kForth-64/blob/master/forth-src/serial.4th
One use is to use the serial port for reading an external switch. Code
example follows for a two-position switch. In my application, I have a
nice big bright red gamer spring-loaded push button switch, which I want
to use to begin an audio recording for recording a practice take on
guitar -- it's not easy to access the keyboard with guitar in hand,
positioned in front of a microphone (the recording code is actually a
Forth script which makes use of the ALSA commands arecord, aplay etc.
but more on that at a later date).
--
Krishna Myneni
=== begin rs232-switch.4th ===
\ rs232-switch.4th
\
\ External push button switch connected to serial port
\ via RTS/CTS lines may be queried with this code to
\ find out button status (pushed in/closed or open).
\ This is useful in applications when you can't access
\ the keyboard easily.
\
\ PC Serial Port (DB9 connector)
\
\ 1 DCD <-- ==============
\ 2 RXD <-- \ 1 2 3 4 5 / ( male connector )
\ 3 TXD --> \ 6 7 8 9 /
\ 4 DTR --> =========
\ 5 GND
\ 6 DSR <-- SW1 /==== SW3 ==== SW3
\ 7 RTS --> -->===/ -->\
\ 8 CTS <-- <--1K---==== SW2 <--1K--\==== SW2
\ 9 RI <-- (open/OFF) (closed/ON)
\
\ There is a 1K resistor in series with pin SW2 on the switch.
\
\ To use:
\ 1. Open the com port (COM1 is shown in this example)
\ 2. Enable the switch using RAISE-RTS
\ 3. Query the switch using READ-SWITCH
\ 4. Disable the switch using LOWER-RTS
\ 5. Close the com port
\
include ans-words
include modules
include struct-200x
include struct-200x-ext
include strings
include serial
Also serial
base @
decimal
hex
20 constant CTS_LINE
decimal
variable com
: open-it ( -- ior )
COM1 ∋ serial open com !
com @ 0> IF
com @ c" 8N1" set-params
com @ B57600 set-baud
com @ ∋ serial flush
0
ELSE 1 THEN
com @ lower-rts
;
: close-it ( -- ior ) com @ ∋ serial close ;
\ Return true if switch is closed (ON), false otherwise
: read-switch ( -- bOn )
com @ get-modem-bits CTS_LINE and 0<> ;
\ More thorough debugging can be perfomed with TEST-SWITCH
false value user-abort?
: test-switch ( -- )
open-it ABORT" Unable to open serial port!"
read-switch IF
cr ." CTS is raised. Ensure switch is OFF and try again."
cr close-it drop EXIT
THEN
cr ." Press a key on the keyboard to raise RTS."
BEGIN key? UNTIL key drop
com @ raise-rts
cr ." Press and hold the push-button switch."
cr ." If there is no effect, press Esc to exit the test." cr
false to user-abort?
BEGIN
key? dup IF
key 27 = and dup
IF true to user-abort? THEN
THEN
0=
WHILE
read-switch 0=
WHILE
REPEAT
cr ." CTS has been raised (switch is ON)."
cr ." Please release the switch to OFF position."
BEGIN
1000 usleep
read-switch 0=
UNTIL
cr ." CTS is low (switch is OFF)."
THEN
com @ lower-rts
close-it drop
user-abort? IF cr ." Test aborted by user!" cr THEN
;
cr cr .( Type 'TEST-SWITCH' to check operation of switch. ) cr
base !
=== end rs232-switch.4th ===
--- Synchronet 3.20a-Linux NewsLink 1.114