home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************/
- /*
- /* serial.write.dll.c
- /* by Atul Butte
- /* Copyright © 1989 by Microsoft Corporation
- /* All Rights Reserved
- /*
- /* version 1.0
- /*
- /*
- /* This CALL/REGISTER will send a string through a serial port.
- /*
- /* Excel usage:
- /*
- /* = Register( "serial library", "serial.write", "IHCD" )
- /* = Call( ref, portNumber, writeConfigStr, string )
- /*
- /* where
- /* portNumber = number of port (1 = modem, 2 = printer)
- /* writeConfigStr = configuration of communications protocol, etc
- /* string = string to send
- /*
- /***********************************************************************/
-
- /***********************************************************************/
- /*
- /* D E F I N E S
- /*
- /***********************************************************************/
-
- #define ROUTINE_NAME "serial.write"
- #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 "absorb_echo.h"
- #include "get_write_flags.h"
-
- /***********************************************************************/
- /*
- /* main
- /*
- /***********************************************************************/
-
- pascal short main( port, pszConfig, pst )
-
- unsigned short port; /* serial port to use */
- register char *pszConfig; /* communications configuration string */
- register char *pst; /* holds the string to send */
-
- {
- register OSErr err; /* result code from Toolbox routines */
- ParamBlockRec param; /* parameter block for read/write */
-
- Boolean fAddLF = false; /* flag for adding linefeeds */
- Boolean fIgnore = false; /* flag for ignoring escape chars */
- Boolean fStrip8Bit = false; /* flag for stripping high bit */
- Boolean fAbsorbEcho = false; /* flag for waiting for echo from host */
-
- short refIn; /* reference number for input port */
- short refOut; /* reference number for output port */
- register long ich = 0; /* index in characters to send (# already sent) */
- long cch; /* count of total characters to send */
- long cchBuff = 0; /* number of characters waiting in read buffer */
- char ch; /* character to write */
-
- RememberA0();
- SetUpA4();
- if( pszConfig == pNIL ) {
- display_error( "The second parameter must be a configuration string." );
- RestoreA4( );
- return( errParam );
- }
-
- err = get_port( port, &refIn, &refOut );
- if( err != noErr ) {
- display_error( "Illegal port number." );
- RestoreA4( );
- return( err );
- }
-
- get_write_flags( pszConfig, &fAddLF, &fIgnore, &fStrip8Bit, &fAbsorbEcho );
-
- if( !fIgnore ) {
- err = interpret( pst );
- if( err != noErr ) {
- RestoreA4( );
- return( errStrFormat );
- }
- }
-
- if( fAbsorbEcho ) { /* flush input buffer */
- do {
- err = SerGetBuf( refIn, &cchBuff );
- if( err != noErr ) {
- display_error( "Error trying to count buffer." );
- break;
- }
- if( cchBuff != 0 ) {
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioBuffer = &ch;
- param.ioParam.ioRefNum = refIn;
- err = PBRead( ¶m, false );
- if( err != noErr ) {
- display_error( "Error trying to flush the input buffer." );
- return( errSerialRead );
- }
- }
- } while( cchBuff != 0 );
- }
-
- ich = 0;
- cch = *pst;
- pst++;
-
- while( ich < cch ) {
-
- ch = *pst;
- if( fStrip8Bit ) {
- ch &= 0x7f;
- }
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioBuffer = &ch;
- param.ioParam.ioRefNum = refOut;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error writing to serial port." );
- RestoreA4();
- return( errSerialWrite );
- }
- if( fAbsorbEcho ) {
- if( absorb_echo( refIn, ch ) != noErr ) {
- display_error( "Error reading echo from serial port." );
- RestoreA4();
- return( errSerialWrite );
- }
- }
- if( (ch == kchReturn) && (fAddLF) ) {
- ch = kchLinefeed;
- param.ioParam.ioReqCount = 1;
- param.ioParam.ioBuffer = &ch;
- param.ioParam.ioRefNum = refOut;
- err = PBWrite( ¶m, false );
- if( err != noErr ) {
- display_error( "Error writing linefeed to serial port." );
- RestoreA4();
- return( errSerialWrite );
- }
- if( fAbsorbEcho ) {
- if( absorb_echo( refIn, ch ) != noErr ) {
- display_error( "Error reading echo from serial port." );
- RestoreA4();
- return( errSerialRead );
- }
- }
- }
- pst++;
- ich++;
- }
- RestoreA4();
- return( noErr );
- }
-
- #include "get_write_flags.c"
- #include "get_port.c"
- #include "interpret.c"
- #include "absorb_echo.c"
-