; SPOOLER
;
; Program for Proteus
;
; (C) 2003-2004 Simone Zanella Productions
;
; Verify if a file exists; if it is so and the file is writable (it is not locked for update),
; process it by sending strings (e.g. commands to create a label on a barcode printer) merged with
; data available in the file.
; The printer where data are to be sent is specified by network address and port.
;
; Working parameters are at the beginning of the program and are the following:
; - Filename = data file name
; - NetAddr = printer address
; - NetPort = printer port
#!proteus -z -j
!include "win32.prt"
!include "socket.prt"
; When run as a service, use constant parameters; otherwise, extract them from command line
!ifdef SERVICE
Filename = "c:\\spool\\DATI.TXT"
NetAddr = "172.16.0.82"
NetPort = 9090
!else
IF LT(ARGC, 7)
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " datafile address port"
CONSOLELN ""
CONSOLELN "Purpose: use datafile for printing labels to the printer (address, port)"
ABORT 0
FI
Filename = ARGV(5)
NetAddr = ARGV(6)
NetPort = ARGV(7)
!endif
WHILE 1
; Wait 0.5 seconds
SLEEP(0.5)
IF ISFILE(Filename)
H2 = FOPEN(Filename, 4)
IF EQ(H2, -1)
; File locked, it is being written
CONTINUE
FI
HSock = W32SOCKET(AF_INET, SOCK_STREAM, IPPROTO_TCP)
IF EQ(HSock, -1)
Message("Errore creating socket")
Message("Socket error: " W32LASTSOCKETERR())
FCLOSE(H2)
CONTINUE
FI
Result = W32CONNECT(HSock, AF_INET, NetPort, NetAddr)
IF EQ(Result, -1)
Message("Error connecting socket")
Message("Socket error: " W32LASTSOCKETERR())
W32SHUTDOWN(HSock, SD_BOTH)
W32CLOSESOCKET(HSock)
FCLOSE(H2)
CONTINUE
FI
Errore = 0
WHILE NOT(FEOF(H2))
S2 = FREADLN(H2)
IF NOT(ISEMPTY(S2))
; Suppose the file holds (in each line) a text to be printed as a barcode and
; the number of labels to be printed, separated by a comma
BarCode = TOKEN(S2, 1, ",")
NumLabels = TOKEN(S2, 2, ",")
IF ISEMPTY(NumLabels)
NumLabels = 1
FI
; Prepara i dati da inviare alla stampante (codice di esempio FingerPrint per stampanti Intermec)
S = "\r\nCLIP ON\r\nDIR 1\r\nAN 7\r\nPP 100, 100\r\nBT \"CODE39\"\r\n" \
"BARRATIO 2,1 : BARHEIGHT 120 : BARMAG 3 : BARFONT \"Swiss 721 BT\" ON\r\n" \
"PB \"" BarCode "\"\r\nPF " NumLabels "\r\n"
Result = W32SEND(HSock, S, 0)
IF EQ(Result, -1)
Message("Send error")
Message("Socket error: " W32LASTSOCKETERR())
W32SHUTDOWN(HSock, SD_BOTH)
W32CLOSESOCKET(HSock)
Errore = 1
BREAK
FI
FI
LOOP
FCLOSE(H2)
IF EQ(Errore, 0)
W32SHUTDOWN(HSock, SD_BOTH)
W32CLOSESOCKET(HSock)
Message("Send completed.")
FREMOVE(Filename)
FI
FI
LOOP
ABORT 0
FUNCTION Message(s)
; If run as a service, save message to a log file;
; otherwise, print message to video
!ifdef SERVICE
FAPPEND("c:\\spooler\\log.txt", s "\r\n")
!else
CONSOLELN s
!endif
RETURN