Programma di esempio UPLOG.PRT |
; UPLOG
;
; Programma per Proteus
;
; (C) 2003 Simone Zanella Productions
;
; Invia un file sulla porta seriale e con i parametri specificati,
; salvando tutti i caratteri ritornati dalla periferica destinataria.
#!proteus -z
!include "win32.prt"
!include "console.prt"
; Parametri di default: COM1, 9600, N, 8, 1, controllo di flusso XON/XOFF
ComPort = "COM1"
ComSpeed = 9600
ComParity = "N"
ComData = 8
ComStop = 1
ComFlow = "X"
H2 = FOPEN(ARGV(5), 1)
HLog = FOPEN(ARGV(6), 28)
IF OR(EQ(H2, -1), EQ(HLog, -1))
IF GE(ARGC, 5)
IF EQ(H2, -1)
CONSOLELN "File " ARGV(5) " NON trovato."
FI
FI
IF GE(ARGC, 6)
IF EQ(HLog, -1)
CONSOLELN "Impossibile aprire " ARGV(6) "."
FI
FI
CONSOLELN ""
CONSOLELN "Sintassi: " ARGV(1) " " ARGV(2) " nomefile log [/Pporta] [/Simpostazioni]"
CONSOLELN ""
CONSOLELN "Scopo: invia il file specificato sulla porta e con i parametri indicati,"
CONSOLELN " salvando sul log quanto ritornato dalla periferica destinataria."
CONSOLELN "Default: COM1, 9600 baud, nessuna parita', 8 bit dati, 1 bit stop, XON/XOFF."
CONSOLELN ""
CONSOLELN "/Pporta porta = 1, 2, 3, .."
CONSOLELN "/Simpostazioni la stringa impostazioni contiene i seguenti valori, "
CONSOLELN " separati da virgole:"
CONSOLELN " velocita' - 110, 300, 600, 1200, 2400, 4800, 9600, 19200,"
CONSOLELN " 38400, 57600, 115200"
CONSOLELN " parita' - Nessuna, Pari, Dispari, Marca, Spazio"
CONSOLELN " bit di dati - 8 o 7"
CONSOLELN " bit di stop - 1 o 2"
CONSOLELN " controllo di flusso - Rts/cts, Xon/xoff, Entrambi, Nessuno"
CONSOLELN ""
CONSOLELN "Per ogni impostazione e' sufficiente specificare l'iniziale, es. /S9600,N,8,1,X"
ABORT 0
FI
FOR X = 7 TO ARGC
ElaboraParametri(ARGV(X))
NEXT
ConvalidaParametri()
H = W32CREATEFILE(ComPort, NOR(W32_GENERIC_WRITE, W32_GENERIC_READ), 0, \
W32_OPEN_EXISTING, 0)
IF EQ(H, -1)
CONSOLELN "Impossibile aprire " 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 "Errore nell'impostazione della porta (" 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, "Porta seriale: " ComPort)
DISPWRITE(1, 2, "Parametri: " ComSpeed ", " ComParity ", " ComData ", " ComStop ", " ComFlow)
DISPWRITE(1, 3, "Caricamento in corso di: " ARGV(5))
Total = FSIZE(H2)
Prev = -1
Prev = ProgressBar(0, Total, 5, 1, Prev, 50, 3)
Status = VECNEW(3)
WHILE NOT(FEOF(H2))
N = FSEEK(H2, 0, 1)
L = FREAD(H2, 1024)
IF EQ(W32WRITEFILE(H, L), -1)
DISPWRITE(1, 8, "Errore nell'invio.")
W32CLOSEHANDLE(H)
FCLOSE(H2)
ABORT 0
FI
S = W32CLEARCOMERR(H, @Errori, Status)
N2 = VECGET(Status, 2)
IF N2
RXS = REPLICATE(" ", N2)
IF NEQ(W32READFILE(H, @RXS), -1)
FWRITE(HLog, RXS)
FI
FI
Prev = ProgressBar(N, Total, 5, 1, Prev, 50, 3)
LOOP
N2 = 1
WHILE N2
SLEEP(0.5)
S = W32CLEARCOMERR(H, @Errori, Status)
N2 = VECGET(Status, 2)
IF N2
RXS = REPLICATE(" ", N2)
IF NEQ(W32READFILE(H, @RXS), -1)
FWRITE(HLog, RXS)
FI
FI
LOOP
Prev = ProgressBar(Total, Total, 5, 1, Prev, 50, 3)
W32CLOSEHANDLE(H)
FCLOSE(H2)
DISPWRITE(1, 8, "Fatto.")
ABORT 0
FUNCTION ProgressBar(value, total, starty, startx, prev, len, altezza)
; Procedura per il disegno di una barra di progresso
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 ConvalidaParametri()
; Convalida i parametri della porta seriale
SWITCH _ComSpeed EQ
ON 110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
OTHER
CONSOLELN "Velocita' errata: " _ComSpeed
ABORT 1
OFF
SWITCH _ComParity STREQ
ON "N", "P", "D", "M", "S"
OTHER
CONSOLELN "Parita' errata: " _ComParity
ABORT 1
OFF
SWITCH _ComData
ON 8, 7
OTHER
CONSOLELN "Lunghezza dati errata: " _ComData
ABORT 1
OFF
SWITCH _ComStop
ON 1, 2
OTHER
CONSOLELN "Bit di stop errati: " _ComStop
ABORT 1
OFF
SWITCH _ComFlow STREQ
ON "R", "X", "E", "N"
OTHER
CONSOLELN "Controllo di flusso errato: " _ComFlow
ABORT 1
OFF
RETURN
FUNCTION ElaboraParametri(s)
; Legge i parametri per la porta
s = RESTFROM(s, 2)
SWITCH LEFT(s, 1) STRIEQ
ON "P"
; Seleziona la porta
IF OR(LT(RESTFROM(s, 2), 1), GT(RESTFROM(s, 2), 20))
CONSOLELN "Porta errata: /" s
ABORT 1
FI
_ComPort = "COM" RESTFROM(s, 2)
ON "S"
; Imposta i parametri di comunicazione
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 "Parametro non riconosciuto: /" s
ABORT 1
OFF
RETURN