• kForth serial port interface update

    From Krishna Myneni@krishna.myneni@ccreweb.org to comp.lang.forth on Fri Nov 1 09:18:32 2024
    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
  • From sjack@sjack@dontemail.me (sjack) to comp.lang.forth on Fri Nov 1 15:26:03 2024
    From Newsgroup: comp.lang.forth

    Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
    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.

    RS-232 is good. I used to have a board called the octopus. It had two
    rs-232 connector on both ends to handle all sex. Had pinouts in between
    to swap tx/rx and/or tie HTS and CTS to ground or high (I forget).
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Krishna Myneni@krishna.myneni@ccreweb.org to comp.lang.forth on Sun Nov 3 04:11:42 2024
    From Newsgroup: comp.lang.forth

    On 11/1/24 10:26, sjack wrote:
    Krishna Myneni <krishna.myneni@ccreweb.org> wrote:
    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.

    RS-232 is good. I used to have a board called the octopus. It had two
    rs-232 connector on both ends to handle all sex. Had pinouts in between
    to swap tx/rx and/or tie HTS and CTS to ground or high (I forget).

    Yes, I know RS-232 well from past experience using it to control
    instruments and test wireless communications systems (long ago). For interfacing the serial port to read an external switch, the other modem
    lines can be used to read up to 3 switches (maybe 4).

    For my audio recording program, I currently have one button to start and
    stop recording. I plan to add two more buttons: one to start and stop
    playback of the last recorded file in the sequence,and another to redo
    the last recording. All three should be readable from a single serial port.

    --
    Krishna

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Krishna Myneni@krishna.myneni@ccreweb.org to comp.lang.forth on Mon Nov 4 06:26:17 2024
    From Newsgroup: comp.lang.forth

    On 11/1/24 09:18, Krishna Myneni wrote:
    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
    ...

    serial.4th has been updated to open USB -> RS232 devices also. These
    have device names /dev/ttyUSB0 /dev/ttyUSB1 etc. The predefined port constants USBCOM1 and USBCOM2 map to these dev interfaces. A word of
    caution -- if you have more than one USB->RS232 adapters, they may not
    map to the same /dev/ttyUSBx interface each time the system is booted.

    Also, please make sure to add the user to the dialout group in order to
    have permissions for read/write access to both the UART and USB serial interfaces (/dev/ttySx and /dev/ttyUSBx).

    --
    Krishna Myneni

    --- Synchronet 3.20a-Linux NewsLink 1.114