; DTPS
;
; Program for Proteus
;
; (C) 2004 Simone Zanella Productions
;
; Receives data coming from a terminal connected via RS-232 using the DTP protocol;
; the data received (if the transmission is successful) are transmitted in keyboard emulation -
; every line is terminated by POSTFIX.
; This program can be installed as a script for Proteus Service.
;
; Note: this script requires the following DLLs for working:
; - PROTEXT.DLL (default DLL distributed with Proteus)
; - DTPCLIENT.DLL (Datalogic® DLL)
; - DTPCMDS.DLL (Datalogic® DLL)
;
; Communication parameters can be found at the very beginning of the program; the meaning is as follow:
; - COMPORT = communication port (see the constants below)
; - COMSPEED = communication speed (see the constants below)
; - POLLINTERVAL = number of seconds after which the program verifies if a terminal is connected
; - TIMEOUT = timeout (ms) for receiving an answer from the terminal
; - FILENAME = name of the file to be downloaded (full path in the terminal)
; - TEMPFILE = temporary file name, where data will be received
; - KILLFILE = boolean value (1 or 0) indicating if the file on the terminal should be deleted after transmission
; - QUITAFTERRX = boolean value (1 or 0) indicating if DTPS on the terminal should exit after transmission
; - PACE = delay to wait after sending each line in keyboard emulation (in seconds)
; - POSTFIX = terminator (usually: "{ENTER}")
;
; The lines below (commented out) allow for alternate behaviours:
; - bring to the foreground a specific window (Ultra-Edit) and emulate keyboard;
; - open notepad (if it is not running), emulate keyboard and return to previous window.
#!proteus -z -j
!include "win32.prt"
!extern DTPOpenClient, PROTEXT.DLL, ProteusDTPOpenClient, 2, 0
!extern DTPSetFailTimeout, PROTEXT.DLL, ProteusDTPSetFailTimeout, 2, 0
!extern DTPPing, PROTEXT.DLL, ProteusDTPPing, 1, 0
!extern DTPSetAttrib, PROTEXT.DLL, ProteusDTPSetAttrib, 5, 0
!extern DTPFileRx, PROTEXT.DLL, ProteusDTPFileRx, 4, 0
!extern DTPCloseClient, PROTEXT.DLL, ProteusDTPCloseClient, 1, 0
!extern DTPDelete, PROTEXT.DLL, ProteusDTPDelete, 3, 0
!extern DTPExit, PROTEXT.DLL, ProteusDTPExit, 1, 0
!extern DTPChDir, PROTEXT.DLL, ProteusDTPChDir, 2, 0
CONST DTP_DELETE_ALL = 1
CONST DTP_DELETE_SOME = 0
; Serial ports
CONST DTP_COM1 = 1
CONST DTP_COM2 = 2
CONST DTP_COM3 = 3
CONST DTP_COM4 = 4
; Speed
CONST DTP_BR_300 = 0
CONST DTP_BR_600 = 1
CONST DTP_BR_1200 = 2
CONST DTP_BR_2400 = 3
CONST DTP_BR_4800 = 4
CONST DTP_BR_9600 = 5
CONST DTP_BR_19200 = 6
CONST DTP_BR_38400 = 7
CONST DTP_BR_57600 = 8
CONST DTP_BR_115200 = 9
; Receive/transmit errors:
; success
CONST ERR_RV_OK = 0
; timeout
CONST ERR_RV_TOUT = 1
; frame error
CONST ERR_RV_FE = 2
; user abort
CONST ERR_RV_ABORT = 3
; bad frame format
CONST ERR_RV_BAD_FRAME = 4
; bad checksum
CONST ERR_RV_BAD_CRC = 5
; bad sequence
CONST ERR_RV_BAD_SEQ = 6
; bad source
CONST ERR_RV_BAD_SOURCE = 7
; bad parameters
CONST ERR_RV_BAD_PARAM = 8
; file already esists
CONST ERR_RV_EXIST = 9
; generic error
CONST ERR_RV_GENERIC = 10
; Constants for file creation (receive/transmit)
CONST DTP_CREATE_APPEND = 3
CONST DTP_CREATE_RENAME = 2
CONST DTP_CREATE_NEW = 1
CONST DTP_CREATE_ALWAYS = 0
; Configuration parameters
; ------------------------
; Serial port
COMPORT = DTP_COM1
; Speed
COMSPEED = DTP_BR_38400
; Terminator
POSTFIX = CHR(13)
; Inter-line delay (seconds)
PACE = 0.02
; Polling interval
POLLINTERVAL = 1
; Timeout (ms) for receiving an answer from the terminal
TIMEOUT = 1000
; Name of the file to be downloaded (full path in the terminal)
FILENAME = "C:\\DATI.TXT"
; Temporary file name, where data will be received
TEMPFILE = "C:\\DTPS.TMP"
; Remove file after transmission
KILLFILE = 0
; Exit DTPS after transmission
QUITAFTERRX = 1
; ---------------------------
; Open serial port
CS = DTPOpenClient(COMPORT, COMSPEED)
IF EQ(CS, 0)
!ifndef SERVICE
CONSOLELN "Could not open port " COMPORT "."
!endif
ABORT 2
FI
; Set timeout for ping
DTPSetFailTimeout(CS, TIMEOUT)
P = STRRSTR(FILENAME, "\\")
IF NEQ(P, 0)
PATH = LEFT(FILENAME, DEC(P))
IF AND(EQ(STRLEN(PATH), 2), STREQ(RIGHT(PATH, 1), ":"))
PATH = PATH "\\"
FI
NAME = RESTFROM(FILENAME, INC(P))
ELSE
PATH = ""
NAME = FILENAME
FI
WHILE 1
; Wait for POLLINTERVAL seconds
SLEEP(POLLINTERVAL)
; If it is not a service and the user pressed ESC, exit
!ifndef SERVICE
IF KBDHIT()
IF EQ(GETCH(), 27)
BREAK
FI
FI
!endif
; Ping terminal
RESULT = DTPPing(CS)
IF EQ(RESULT, 0)
; Check if file to be downloaded is available
IF ISNOTEMPTY(PATH)
RESULT = DTPChDir(CS, PATH)
FI
RESULT = DTPSetAttrib(CS, NAME, 0, 0xFFFF, 0xFFFF)
IF NEQ(RESULT, 0)
CONTINUE
FI
; Remove temporary file
FREMOVE(TEMPFILE)
; Receive file from the terminal
RESULT = DTPFileRx(CS, FILENAME, TEMPFILE, DTP_CREATE_ALWAYS)
!ifndef SERVICE
SWITCH RESULT
ON ERR_RV_OK
msg = ""
ON ERR_RV_TOUT
msg = "Timeout"
ON ERR_RV_FE
msg = "Frame error"
ON ERR_RV_ABORT
msg = "User abort"
ON ERR_RV_BAD_FRAME
msg = "Incorrect frame format"
ON ERR_RV_BAD_CRC
msg = "Wrong checksum"
ON ERR_RV_BAD_SEQ
msg = "Sequence error"
ON ERR_RV_BAD_SOURCE
msg = "Unexpected source"
ON ERR_RV_BAD_PARAM
msg = "Wrong parameters"
ON ERR_RV_EXIST
msg = "File already exists"
ON ERR_RV_GENERIC
msg = "Generic error"
OFF
IF NEQ(RESULT, 0)
CONSOLELN "Error: " msg
FI
!endif
IF EQ(RESULT, 0)
IF KILLFILE
DTPDelete(CS, FILENAME, DTP_DELETE_ALL)
FI
IF QUITAFTERRX
DTPExit(CS)
FI
EMULATEKBD(TEMPFILE)
FI
FI
LOOP
; Close the serial port
RESULT = DTPCloseClient(CS)
ABORT 0
FUNCTION EMULATEKBD(tempfile)
h = FOPEN(tempfile, 1)
IF EQ(h, -1)
RETURN
FI
WHILE NOT(FEOF(h))
l = FREADLN(h)
; If a specific window is to be selected, the following function can be used:
; W32SETFOCUS(W32FINDWINDOW("*ULTRAEDIT-32*"))
W32SENDKEYS(KTrans(l) _POSTFIX)
SLEEP(_PACE)
LOOP
; Alternate version which saves/restores current window and selects Notepad before wedging data
; ; Saves current window
; hOld = W32GETFOCUS()
;
; ; Look for Notepad
; hNew = W32FINDWINDOW("*Notepad*")
; IF EQ(hNew, 0)
; ; Not found - open it
; W32SHELL("NOTEPAD.EXE")
; ; Wait 1 second for it to become available
; SLEEP(1)
; ; Look up again its window
; hNew = W32FINDWINDOW("*Notepad*")
; FI
; ; If found, select its window and wedge data
; IF NEQ(hNew, 0)
; W32SETFOCUS(hNew)
; WHILE NOT(FEOF(h))
; l = FREADLN(h)
; W32SENDKEYS(KTrans(l) _POSTFIX)
; SLEEP(_PACE)
; LOOP
; ELSE
; ; Notepad could not be opened - data lost!
; FI
; W32SETFOCUS(hOld)
FCLOSE(h)
RETURN
FUNCTION KTrans(s)
l = STRLEN(s)
r = ""
FOR x = 1 TO l
c = SUBSTR(s, x, 1)
; Special characters: characters which are not available on the keyboard
; require special treatment (Alt + 3 digits) - useful for foreign keyboards
SWITCH c STREQ
ON "~"
r = r "{ALT DOWN}{NUMPAD1}{NUMPAD2}{NUMPAD6}{ALT UP}"
ON "{"
r = r "{ALT DOWN}{NUMPAD1}{NUMPAD2}{NUMPAD3}{ALT UP}"
ON "}"
r = r "{ALT DOWN}{NUMPAD1}{NUMPAD2}{NUMPAD5}{ALT UP}"
ON "+", "^", "%", "(", ")", "[", "]"
r = r "{" c "}"
OTHER
r = r c
OFF
NEXT
RETURN r