home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************/
- /*
- /* serial.readTilStr.dll.c
- /* by Atul Butte
- /* Copyright © 1989 by Microsoft Corporation
- /* All Rights Reserved
- /*
- /* version 1.0
- /*
- /*
- /* This CALL/REGISTER will read from the serial port until the specifed
- /* string is read up to 255 characters. The characters are stored in
- /* a string and are returned.
- /*
- /* Excel usage:
- /*
- /* = Register( "serial library", "serial.readTilStr", "GHDJCG" )
- /* = Call( ref, portNumber, matchStr, maxTime, readConfigStr, )
- /*
- /* where
- /* portNumber = number of port (1 = modem, 2 = printer)
- /* matchStr = keep reading until we read this string
- /* maxTime = maximum amount of time to wait for characters
- /* in 1/60 second units
- /* readConfigStr = configuration of communications protocol, etc
- /*
- /* The extra comma at the end of the CALL parameter list must be present.
- /*
- /***********************************************************************/
-
- /***********************************************************************/
- /*
- /* D E F I N E S
- /*
- /***********************************************************************/
-
- #define ROUTINE_NAME "serial.readTilStr"
- #define hNIL 0L
- #define pNIL 0L
-
- /***********************************************************************/
- /*
- /* I N C L U D E S
- /*
- /***********************************************************************/
-
- #include "serial.h"
- #include "error.h"
- #include "get_port.h"
- #include "interpret.h"
- #include "get_read_flags.h"
-
- /***********************************************************************/
- /*
- /* P R O T O T Y P E S
- /*
- /***********************************************************************/
-
- void initialize_next( Ptr pnxt, char *pstMatch );
- char check_for_match( char ch, unsigned char cchMatched, unsigned char cMatchLength, char *pstMatch, Ptr pnxt );
-
- /***********************************************************************/
- /*
- /* main
- /*
- /***********************************************************************/
-
- pascal char *main( port, pstMatch, timeDur, pszConfig, pstBuff )
-
- unsigned short port; /* serial port to use */
- char *pstMatch; /* keep reading until we read this string */
- unsigned long timeDur; /* maximum time to wait for chars (in ticks) */
- char *pszConfig; /* communications configuration string */
- char *pstBuff; /* buffer in which to return string */
-
- {
- register OSErr err; /* result code from Toolbox routines */
- ParamBlockRec param; /* parameter block for read/write */
-
- Boolean fEcho = false; /* flag for echoing characters */
- Boolean fEdit = false; /* flag for allowing edit characters */
- Boolean fStripLF = false; /* flag for stripping line feeds */
- Boolean fStrip8Bit = false; /* flag for stripping high bit */
- Boolean fAddLF = false; /* flag for adding LF after CR */
- Boolean fIgnore = false; /* flag for ignoring escape chars */
-
- Handle hnxt; /* handle to the next table */
- register Ptr pnxt; /* pointer to the next table */
- long cchBuff = 0; /* number of characters waiting in read buffer */
- register unsigned long timeStop; /* time at which to stop */
- short refIn; /* reference number for input port */
- short refOut; /* reference number for output port */
- register char *pst; /* pointer to current character in buffer */
- char chRead; /* buffer used to read a character */
- register char ch; /* character read */
- char echoBackspace[3]; /* characters to send to echo Backspace */
- char echoLinefeed; /* characters to send to echo Linefeed */
- register unsigned char cchMatched; /* number of characters matching so far */
- register unsigned char cchMatch; /* number of characters to match */
-
- register unsigned char cch = 0;
-
- RememberA0();
- SetUpA4();
-
- if( pstBuff == pNIL ) {
- display_error( "The fifth parameter must be a pointer to an empty string buffer." );
- RestoreA4( );
- return( pstBuff );
- }
- pst = pstBuff;
- *pst = 0;
- pst++;
-
- if( pszConfig == pNIL ) {
- display_error( "The fourth parameter must be a configuration string." );
- RestoreA4( );
- return( pstBuff );
- }
- if( pstMatch == pNIL ) {
- display_error( "The second parameter must be a string to match." );
- RestoreA4( );
- return( pstBuff );
- }
- if( *pstMatch == 0 ) {
- display_error( "You must specify a string to match with at least one character." );
- RestoreA4( );
- return( pstBuff );
- }
-
- err = get_port( port, &refIn, &refOut );
- if( err != noErr ) {
- display_error( "Illegal port number." );
- RestoreA4( );
- return( pstBuff );
- }
-
- get_read_flags( pszConfig, &fEcho, &fEdit, &fStripLF, &fStrip8Bit, &fAddLF, &fIgnore );
-
- if( !fIgnore ) {
- err = interpret( pstMatch );
- if( err != noErr ) {
- RestoreA4( );
- return( pstBuff );
- }
- }
-
- cchMatched = 1;
- cchMatch = *pstMatch;
- if( cchMatch > 1 ) {
- hnxt = NewHandle( (long) *pstMatch + 1 );
- HLock( hnxt );
- pnxt = *hnxt;
- initialize_next( pnxt, pstMatch );
- }
-
- if( fEcho ) {
- echoBackspace[0] = kchBackspace;
- echoBackspace[1] = ' ';
- echoBackspace[2] = kchBackspace;
- if( fAddLF ) {
- echoLinefeed = kchLinefeed;
- }
- }
-
- timeStop = TickCount( ) + timeDur;
-
- while( cch < 254 ) {
- if( ( timeDur != 0 ) && ( TickCount( ) >= timeStop ) ) {
- break;
- }
- err = SerGetBuf( refIn, &cchBuff );
- if( err != noErr ) {
- display_error( "Error trying to count buffer." );
- break;
- }
- if( cchBuff == 0 )
- continue;
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioBuffer = &chRead;
- param.ioParam.ioRefNum = refIn;
- err = PBRead( ¶m, false );
- if( err != noErr ) {
- display_error( "Error reading from serial port." );
- break;
- }
- ch = chRead;
- if( ( (ch == kchBackspace) || (ch == kchDelete) ) && (fEdit) ) {
- if( fEcho ) {
- param.ioParam.ioReqCount = 3;
- param.ioParam.ioRefNum = refOut;
- param.ioParam.ioBuffer = echoBackspace;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error echoing backspace to serial port." );
- break;
- }
- }
- } else if( fEcho ) {
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioRefNum = refOut;
- param.ioParam.ioBuffer = &chRead;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error echoing to serial port." );
- break;
- }
- if( (ch == kchReturn) && (fAddLF) ) {
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioRefNum = refOut;
- param.ioParam.ioBuffer = &echoLinefeed;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error echoing linefeed to serial port." );
- break;
- }
- }
- }
- if( fStrip8Bit ) {
- ch &= 0x7F;
- }
- if( ( (ch == kchBackspace) || (ch == kchDelete) ) && (fEdit) ) {
- cch--;
- if( cch >= 0 ) {
- pst--;
- } else {
- cch = 0;
- }
- } else if( (ch == kchLinefeed) && (fStripLF) ) {
- } else {
- *pst = ch;
- pst++;
- cch++;
- }
-
- if( (ch == kchLinefeed) && (fStripLF) ) {
- } else {
- cchMatched = check_for_match( ch, cchMatched, cchMatch, pstMatch, pnxt );
- if( cchMatched > cchMatch ) /* if we found a match */
- break;
- }
- }
-
- if( cchMatch > 1 )
- DisposHandle( hnxt );
- *pstBuff = cch;
- RestoreA4()
- return( pstBuff );
- }
-
- /***********************************************************************/
- /*
- /* initialize_next
- /*
- /***********************************************************************/
-
- void initialize_next( pnxt, pstMatch )
-
- register Ptr pnxt; /* pointer to the next table */
- char *pstMatch; /* keep reading until we read this string */
-
- {
- register unsigned long i = 1, j = 0; /* index through match string */
- register unsigned char cchMatch = *pstMatch; /* number of characters to match */
-
- pnxt[1] = 0;
- do {
- if( ( j == 0 ) || ( pstMatch[i] == pstMatch[j] ) ) {
- i++;
- j++;
- pnxt[i] = j;
- } else {
- j = pnxt[j];
- }
- } while( i <= cchMatch );
- }
-
- /***********************************************************************/
- /*
- /* check_for_match
- /*
- /***********************************************************************/
-
- char check_for_match( ch, cchMatched, cchMatch, pstMatch, pnxt )
-
- register char ch; /* character read */
- register unsigned char cchMatched; /* number of characters matching so far */
- register unsigned char cchMatch; /* number of characters to match */
- char *pstMatch; /* keep reading until we read this string */
- register Ptr pnxt; /* pointer to the next table */
-
- {
- if( cchMatch == 1 ) {
- if( ch == pstMatch[1] ) {
- return( 2 );
- } else {
- return( 1 );
- }
- } else {
- do {
- if( ( cchMatched == 0 ) || ( ch == pstMatch[cchMatched] ) ) {
- cchMatched++;
- break;
- } else {
- cchMatched = pnxt[cchMatched];
- }
- } while( cchMatched <= cchMatch );
-
- return( cchMatched );
- }
- }
-
- #include "get_port.c"
- #include "interpret.c"
- #include "get_read_flags.c"
-
-