COM4

\ Example: Simple terminal program

create Tag$ 4096 allot

: +char ( ascii -- )

  Tag$ count + c!

  Tag$ c@ 1 + Tag$ c! 

  ;

0  value Com4Hndl

variable Com4key-val            \ a place to save last key received

0  value Com4key-flg?           \ have we already got a key?

: Com4key?      ( -- c1 )       \ get a key from serial port, don't wait long

                Com4key-flg? 0=

                if      Com4key-val 1 Com4Hndl read-file        \ -- len flag

                        abort" Failed to read serial port"      \ -- len

                        to Com4key-flg?

                then    Com4key-flg? ;

: Com4key       ( -- c1 )       \ must return a key

                begin   Com4key?                \ loop till we get one

                until

                Com4key-val c@                  \ return the key

                0 to Com4key-flg? ;             \ clear the save buffer

: Com4emit      { charin -- }     \ write a character to the serial port

              \  dup emit

                &OF charin 1 Com4Hndl write-file

                abort" Failed to write serial port" ;

: do-Com4key    ( c1 -- f1 )    \ process keys pressed on user keyboard

                dup k_ESC =                     \ was ESC pressed?

                if      drop                    \ if so, discard c1 and shut down

                        Com4Hndl ComClose       \ close the com port

                        0 to Com4Hndl           \ clear handle value

                        ." Done"          TRUE  \ return TRUE, we are terminating

                else    

    \            dup emit

                Com4emit          FALSE \ else emit char and continue

                then    ;

create comand_test 256 allot

variable comand_test_count

s" help"  comand_test place

crlf$ count comand_test +place

: Com4talk      ( -- )          \ simple terminal talker to the serial port

                cr ." Initializing Serial Port to: "

                cr ." Com4: baud=38400 parity=N data=8 stop=1"

                z" Com4" ComOpen to Com4Hndl

                    Reset: DCB

                FALSE Put: DCB.fOutxCtsFlow      \ Don't pay attention to CTS

                CBR_38400 8 NOPARITY ONESTOPBIT Com4Hndl ComSetup

                Com4Hndl ComTimeouts

                

                0 comand_test_count !

                0 tag$ c!

                cr ." Press ESC to stop talking." cr

                BEGIN   key?

                        if      key do-Com4key ?EXIT                      

                        then

                                                

                        \ Forth command Line

                        comand_test_count @ comand_test c@ >

                        if

                        else 

                          comand_test_count @ 1 + comand_test_count ! 

                          comand_test  comand_test_count @ + c@

                          do-Com4key drop

                        then

                        

                        Com4key?

                        if      Com4key 

                          dup  

                          0x0a = 

                          if

                            tag$ count  type

                            0 tag$ c!

                          else

                           dup +char

                          then

                          drop \ emit

                        then

                AGAIN ;