COM
\ define the field names for the Communications Timeout structure
0 CELL Field+ .ReadIntervalTimeout nostack
CELL Field+ .ReadTotalTimeoutMultiplier
CELL Field+ .ReadTotalTimeoutConstant
CELL Field+ .WriteTotalTimeoutMultiplier
CELL Field+ .WriteTotalTimeoutConstant
CONSTANT COMMTIMEOUTSBYTES nostack1
: ComTimeouts { cHndl \ CT -- } \ Initialize the communications timeouts
COMMTIMEOUTSBYTES LocalAlloc: CT \ allocate a CT structure
CT COMMTIMEOUTSBYTES erase \ initialize it to zeros
\ set read timeouts to magic value of don't wait, just poll
-1 CT .ReadIntervalTimeout !
0 CT .ReadTotalTimeoutMultiplier !
0 CT .ReadTotalTimeoutConstant !
1 CT .WriteTotalTimeoutMultiplier !
20 CT .WriteTotalTimeoutConstant !
CT
cHndl
Call SetCommTimeouts drop ;
: ComOpen ( z1 -- cHndl ) \ Open Com port for z" COM1", or z" COM2" etc. If port is higher than 9 then it
\ must be in the form z" \\.\COM10" to work correctly.
>R
NULL \ no template
NULL \ open file attributes
OPEN_EXISTING \ creation distribution
NULL \ no security attributes
0 \ exclusive access
GENERIC_READ GENERIC_WRITE or \ desired access modes
R> \ zstring filename
Call CreateFile \ returns handle or -1
dup -1 = \ if -1 then error
Abort" Failed to open COM port!"
; \ -- chndl ;return handle to port
\ ************************************************************
\ here is a list of valid parameters for ComSetup. Except for
\ the ByteSize parameter, these are all windows constants.
\ --- BuadRate CBR_110 CBR_300 CBR_600
\ CBR_1200 CBR_2400 CBR_4800
\ CBR_9600 CBR_14400 CBR_56000
\ CBR_19200 CBR_38400 CBR_57600
\ CBR_115200 CBR_128000 CBR_256000
\
\ --- ByteSize 5, 6, 7, 8
\
\ --- Parity NOPARITY ODDPARITY MARKPARITY
\ EVENPARITY SPACEPARITY
\
\ --- StopBits ONESTOPBIT TWOSTOPBITS ONE5STOPBITS
\
\ ************************************************************
\ Setup the Communications state to the parameters specified
: ComSetup { baud size parity stop cHndl -- }
DCB.AddrOf
cHndl
Call GetCommState ?win-error
baud Put: DCB.BaudRate
size Put: DCB.ByteSize
parity 0<> Put: DCB.fParity \ parity enabled flag
parity Put: DCB.Parity
stop Put: DCB.StopBits
DCB.AddrOf
cHndl
Call SetCommState ?win-error ;
: ComClose ( cHndl -- ) \ close com port if its open
?dup
if Call CloseHandle drop
then ;
: ComErrorClear { comhndl \ lpErrors -- f } \ true = success
COMSTAT.addrof
lpErrors
comhndl Call ClearCommError 0<> ;