; SEND
;
; Program for Proteus
;
; (C) 2003-2004 Simone Zanella Productions
;
; Send a file to serial port using specified parameters.
#!proteus -z
!include "win32.prt"
!include "console.prt"
; Default parameters: COM1, 9600, N, 8, 1, XON/XOFF flow control
ComPort = "COM1"
ComSpeed = 9600
ComParity = "N"
ComData = 8
ComStop = 1
ComFlow = "X"
H2 = FOPEN(ARGV(5), 1)
IF EQ(H2, -1)
IF GE(ARGC, 5)
CONSOLELN "File " ARGV(5) " NOT found."
FI
CONSOLELN ""
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " filename [/Pport] [/Ssettings]"
CONSOLELN ""
CONSOLELN "Purpose: send the specified file to the serial port, using specified parameters."
CONSOLELN ""
CONSOLELN "Default: COM1, 9600 baud, no parity, 8 data bits, 1 stop bit, XON/XOFF."
CONSOLELN ""
CONSOLELN "/Pport port = 1, 2, 3, .."
CONSOLELN "/Ssettings settings will include the following values,"
CONSOLELN " separated by commas:"
CONSOLELN " speed - 110, 300, 600, 1200, 2400, 4800, 9600, 19200,"
CONSOLELN " 38400, 57600, 115200"
CONSOLELN " parity - None, Even, Odd, Mark, Space"
CONSOLELN " data bits - 8 or 7"
CONSOLELN " stop bits - 1 or 2"
CONSOLELN " flow control - Rts/cts, Xon/xoff, Both, None"
CONSOLELN ""
CONSOLELN "For each setting only the first letter should be specified, e.g. /S9600,N,8,1,X"
ABORT 0
FI
FOR X = 6 TO ARGC
SetParameters(ARGV(X))
NEXT
TestParameters()
H = W32CREATEFILE(ComPort, NOR(W32_GENERIC_WRITE, W32_GENERIC_READ), 0, \
W32_OPEN_EXISTING, 0)
IF EQ(H, -1)
CONSOLELN "Could not open " ComPort "."
ABORT 2
FI
ComPar = VECNEW(13)
V = W32GETCOMSTATE(H, ComPar)
VECSET(ComPar, 2, ComSpeed)
V = NOR(W32_COM_BINARY, W32_COM_PARITY_ON)
SWITCH ComFlow STREQ
ON "R"
NOR(@V, W32_COM_RTS_HANDSHAKE, W32_COM_CTSFLOW_ON)
ON "X"
NOR(@V, W32_COM_XONXOFF_OUT, W32_COM_XONXOFF_IN, W32_COM_XOFF_CONTINUE)
ON "E"
NOR(@V, W32_COM_RTS_HANDSHAKE, W32_COM_CTSFLOW_ON, \
W32_COM_XONXOFF_OUT, W32_COM_XONXOFF_IN, W32_COM_XOFF_CONTINUE)
ON "N"
OFF
VECSET(ComPar, 3, V)
VECSET(ComPar, 7, ComData)
SWITCH ComParity STREQ
ON "N"
V = W32_COM_PARITY_NONE
ON "P"
V = W32_COM_PARITY_EVEN
ON "D"
V = W32_COM_PARITY_ODD
ON "M"
V = W32_COM_PARITY_MARK
ON "S"
V = W32_COM_PARITY_SPACE
OFF
VECSET(ComPar, 8, V)
SWITCH ComStop
ON 1
VECSET(ComPar, 9, 0)
ON 2
VECSET(ComPar, 9, 2)
OFF
V = W32SETCOMSTATE(H, ComPar)
IF V
CONSOLELN "Error setting port (" W32GETLASTERROR() ")."
W32CLOSEHANDLE(H)
ABORT 3
FI
TOut = VECNEW(5)
VECSET(TOut, 1, 0)
VECSET(TOut, 2, 0)
VECSET(TOut, 3, 0)
VECSET(TOut, 4, 0)
VECSET(TOut, 5, 0)
W32SETCOMTIMEOUTS(H, TOut)
DISPSET(DISP_BLINK, DISP_BLINK_OFF)
DISPSET(DISP_FOREG, YELLOW)
DISPSET(DISP_BACKG, BLUE)
DISPSET(DISP_UNSFG, YELLOW)
DISPSET(DISP_SELBG, GREEN)
DISPSET(DISP_JUST, DISP_JUST_NONE)
DISPSET(DISP_JLEN, 0)
DISPSET(DISP_SHADOW, DISP_SHADOW_RIGHT)
DISPSET(DISP_SOUND, 0)
DISPSET(DISP_JLEN, 17)
DISPCLS()
DISPWRITE(1, 1, "Serial port: " ComPort)
DISPWRITE(1, 2, "Parameters : " ComSpeed ", " ComParity ", " ComData ", " ComStop ", " ComFlow)
DISPWRITE(1, 3, "Sending : " ARGV(5))
Total = FSIZE(H2)
Prev = -1
Prev = ProgressBar(0, Total, 5, 1, Prev, 50, 3)
WHILE NOT(FEOF(H2))
N = FSEEK(H2, 0, 1)
L = FREAD(H2, 1024)
IF EQ(W32WRITEFILE(H, L), -1)
DISPWRITE(1, 8, "Error sending file.")
W32CLOSEHANDLE(H)
FCLOSE(H2)
ABORT 0
FI
W32PURGECOM(H, W32_PURGE_RXCLEAR)
Prev = ProgressBar(N, Total, 5, 1, Prev, 50, 3)
LOOP
Prev = ProgressBar(Total, Total, 5, 1, Prev, 50, 3)
W32CLOSEHANDLE(H)
FCLOSE(H2)
DISPWRITE(1, 8, "Done.")
ABORT 0
FUNCTION ProgressBar(value, total, starty, startx, prev, len, altezza)
; Print a progress bar
IF EQ(prev, -1)
FOR y1 = starty TO DEC(ADD(starty, altezza))
FOR x = startx TO DEC(ADD(startx, len))
DISPWRITE(x, y1, CHR(0xB0))
NEXT
NEXT
x = startx
ELSE
x = prev
FI
end = ADD(startx, IIF(total, FDIV(MUL(len, value), total), len))
FOR y1 = starty TO DEC(ADD(starty, altezza))
FOR x1 = x TO DEC(end)
DISPWRITE(x1, y1, CHR(0xDB))
NEXT
NEXT
RETURN end
FUNCTION TestParameters()
; Check parameters for serial port
SWITCH _ComSpeed EQ
ON 110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
OTHER
CONSOLELN "Wrong speed: " _ComSpeed
ABORT 1
OFF
SWITCH _ComParity STREQ
ON "N", "E", "O", "M", "S"
OTHER
CONSOLELN "Wrong parity: " _ComParity
ABORT 1
OFF
SWITCH _ComData
ON 8, 7
OTHER
CONSOLELN "Wrong data length: " _ComData
ABORT 1
OFF
SWITCH _ComStop
ON 1, 2
OTHER
CONSOLELN "Wrong stop bits: " _ComStop
ABORT 1
OFF
SWITCH _ComFlow STREQ
ON "R", "X", "B", "N"
OTHER
CONSOLELN "Wrong flow control: " _ComFlow
ABORT 1
OFF
RETURN
FUNCTION SetParameters(s)
; Set port parameters
s = RESTFROM(s, 2)
SWITCH LEFT(s, 1) STRIEQ
ON "P"
; Select port
IF OR(LT(RESTFROM(s, 2), 1), GT(RESTFROM(s, 2), 20))
CONSOLELN "Wrong port: /" s
ABORT 1
FI
_ComPort = "COM" RESTFROM(s, 2)
ON "S"
; Set communication parameters
t = TOKNEW(RESTFROM(s, 2), ",")
_ComSpeed = TOKGET(t, 1)
_ComParity = UPPER(TOKGET(t, 2))
_ComData = TOKGET(t, 3)
_ComStop = TOKGET(t, 4)
_ComFlow = UPPER(TOKGET(t, 5))
TOKFREE(t)
OTHER
CONSOLELN "Unknown parameter: /" s
ABORT 1
OFF
RETURN