DECLARE SUB rs232checkpar (ok$) '**************************************************************************** 'Module RS232 'This module contains I/O subroutines for RS232 communications. ' 'Zachary Wolf '12/21/96 '**************************************************************************** 'Keep shared parameters in a common block COMMON SHARED /rs232/ comfilenumP%, portnumP$, baudrateP$, parityP$, ndatabitsP$, nstopbitsP$, specificsP$ SUB rs232checkpar (ok$) '**************************************************************************** 'This subroutine checks that all required parameters have been assigned 'values. ' 'Output: ' ok$, "y" if parameter assignments are ok, "n" otherwise ' 'Zachary Wolf '12/21/96 '**************************************************************************** 'Default value ok$ = "n" 'comfilenumP% IF comfilenumP% <= 0 THEN PRINT "RS232: file number not defined: " + STR$(comfilenumP%) EXIT SUB END IF 'portnumP$ IF portnumP$ <> "1" AND portnumP$ <> "2" THEN PRINT "RS232: COM port number not allowed: " + portnumP$ EXIT SUB END IF 'baudrateP$ IF baudrateP$ <> "150" AND baudrateP$ <> "300" AND baudrateP$ <> "600" AND baudrateP$ <> "1200" AND baudrateP$ <> "1800" AND baudrateP$ <> "2400" AND baudrateP$ <> "4800" AND baudrateP$ <> "9600" AND baudrateP$ <> "19200" THEN PRINT "RS232: baud rate not allowed: " + baudrateP$ EXIT SUB END IF 'parityP$ IF parityP$ <> "N" AND parityP$ <> "E" AND parityP$ <> "O" AND parityP$ <> "S" AND parityP$ <> "M" AND parityP$ <> "PE" THEN PRINT "RS232: parity not allowed: " + parityP$ EXIT SUB END IF 'ndatabitsP$ IF ndatabitsP$ <> "5" AND ndatabitsP$ <> "6" AND ndatabitsP$ <> "7" AND ndatabitsP$ <> "8" THEN PRINT "RS232: number of data bits not allowed: " + ndatabitsP$ EXIT SUB END IF IF (parityP$ = "E" OR parityP$ = "O") AND ndatabitsP$ = "8" THEN PRINT "RS232: E or O parity with 8 data bits is not allowed" EXIT SUB END IF 'nstopbitsP$ IF nstopbitsP$ <> "1" AND nstopbitsP$ <> "1.5" AND nstopbitsP$ <> "2" THEN PRINT "RS232: number of stop bits not allowed: " + nstopbitsP$ EXIT SUB END IF 'If we made it this far, all parameters have values ok$ = "y" END SUB SUB rs232close '**************************************************************************** 'This subroutine closes the RS232 COM port. ' 'Zachary Wolf '12/21/96 '**************************************************************************** 'Close the port CLOSE #comfilenumP% END SUB SUB rs232inbyte (byte$) '**************************************************************************** 'This subroutine inputs a byte from the RS232 buffer. ' 'Input: ' byte$, a byte from the buffer ' 'Zachary Wolf '12/21/96 '**************************************************************************** 'Input the byte byte$ = INPUT$(1, comfilenumP%) END SUB SUB rs232init '**************************************************************************** 'This subroutine opens an RS232 COM port. ' 'Zack Wolf '12/21/96 '**************************************************************************** 'Get a file number and save it comfilenumP% = FREEFILE 'Check all RS232 parameters CALL rs232checkpar(ok$) IF ok$ <> "y" THEN PRINT "Can not open RS232 COM port." EXIT SUB END IF 'Open the COM port com$ = "COM" + portnumP$ + ":" + baudrateP$ + "," + parityP$ + "," + ndatabitsP$ + "," + nstopbitsP$ + "," + specificsP$ OPEN com$ FOR RANDOM AS #comfilenumP% 'Clear the input buffer nbytes% = LOC(comfilenumP%) IF nbytes% > 0 THEN buffer$ = INPUT$(nbytes%, comfilenumP%) 'print buffer$ END IF END SUB SUB rs232out (msg$) '**************************************************************************** 'This subroutine outputs a string to an RS232 device. ' 'Input: ' msg$, message to send to the device ' 'Zachary Wolf '12/21/96 '**************************************************************************** 'Send the message PRINT #comfilenumP%, msg$ END SUB SUB rs232queuelen (nbytes%) '**************************************************************************** 'This subroutine gives the number of bytes available in the RS232 buffer. ' 'Input: ' nbytes%, the number of bytes available in the buffer ' 'Zachary Wolf '12/21/96 '**************************************************************************** 'Get the number of bytes available nbytes% = LOC(comfilenumP%) END SUB SUB rs232setpar (portnum$, baudrate$, parity$, ndatabits$, nstopbits$, specifics$) '**************************************************************************** 'This subroutine sets the parameters required by this module. ' 'Input: ' portnum$, COM port number: 1 or 2 ' baudrate$, baudrate: 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 9600, or 19200 ' parity$, parity: N (none), E (even), O (odd), S (space), M (mark), or PE (enable error checking) ' ndatabits$, number of bits in each word: 5, 6, 7, or 8; databits + parity bit <= 8 ' nstopbits$, number of stop bits: 1, 1.5, or 2 ' specifics$, list of communication specifics: ASC,BIN,CD,CS,DS,LF,OP,RB,RS,TB ' 'Zachary Wolf '12/21/96 '**************************************************************************** 'Set the parameters portnumP$ = portnum$ baudrateP$ = baudrate$ parityP$ = parity$ ndatabitsP$ = ndatabits$ nstopbitsP$ = nstopbits$ specificsP$ = specifics$ END SUB