home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / serial_lib / serial_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-05  |  5.7 KB  |  175 lines  |  [TEXT/KAHL]

  1. /* serial_lib.c */
  2.  
  3. /* a collection of routines to simplify serial communication, including via a
  4.  * quadralink card.
  5.  *
  6.  * 5/2/94 by Darrell Anderson and Andy Forsberg
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <Serial.h>
  12. #include <stdlib.h>
  13. #include <OSUtils.h>
  14. #include <MacHeaders.h>
  15. #include "error_dlog.h"
  16.  #define __IN_SERIAL_LIB__
  17. #include "serial_lib.h"
  18.  #undef __IN_SERIAL_LIB__
  19.  
  20. // use our error window routine (see error_dlog.c & .h )
  21. #define use_report_error 1
  22.  
  23. // PROTOTYPES for internal use only functions
  24. void HandleError( char *errMsg, short errNum, int whichSerialPort );
  25.  
  26. // GLOBALS
  27. short ddRef_SerialPort[ (k_NumSerialPorts*2) ];
  28. // this keeps track of the device driver reference numbers. the user merely tells which
  29. // port to use (with a clearly labelled #define), as a reference into this array (no need
  30. // to force the user to keep track of these numbers, we do it internally..)
  31. // note: we need twice as many placeholders to keep track of both an _in and an _out device
  32. // driver reference number for each port.
  33.  
  34. // Macros for interpreting our enumerated indices (from serial_lib.h) to indices into the arrays
  35. // ddRef_SerialPort and k_DeviceDriverNames, which hold device driver reference numbers and
  36. // the character string names for serial ports (both _in and _out for each port)
  37. #define InRef(which) ((short)(which*2))
  38. #define OutRef(which) ((short)((which*2)+1))
  39.  
  40. void InitSerial( int whichSerialPort, short configurationParameter )
  41. //
  42. // open the serial port for reading and writing
  43. // configure the serial port to configurationParameter
  44. // report any problems
  45. //
  46. {
  47.     OSErr theErr;
  48.     long numBytes;
  49.     char flushed_byte;
  50.             
  51.     // open the port for reading    
  52.     theErr = OpenDriver( (char *) k_DeviceDriverNames[ InRef(whichSerialPort) ], 
  53.                             (short *) &(ddRef_SerialPort[ InRef(whichSerialPort) ]) );
  54.                             
  55.     // look for and report any errors. (better error handling is on the to do list)
  56.     if( theErr != 0 )
  57.         HandleError( "can't open serial port for reading", theErr, whichSerialPort );
  58.  
  59.     
  60.     // open the port for writing
  61.     theErr = OpenDriver( k_DeviceDriverNames[ OutRef(whichSerialPort) ], 
  62.                             &(ddRef_SerialPort[ OutRef(whichSerialPort) ]) );
  63.     
  64.     // look for and report any errors. (better error handling is on the to do list)
  65.     if( theErr != 0 )
  66.         HandleError( "can't open serial port for writing", theErr, whichSerialPort );
  67.     
  68.     // configure the _out port
  69.     theErr = SerReset( ddRef_SerialPort[ OutRef(whichSerialPort) ], configurationParameter );
  70.     
  71.     // look for and report any errors. (better error handling is on the to do list)
  72.     if( theErr != 0 )
  73.         HandleError( "can't configure serial port _out", theErr, whichSerialPort );
  74.     
  75.     // configure the _in port
  76.     theErr = SerReset( (short) ddRef_SerialPort[ InRef(whichSerialPort) ], (short) configurationParameter );
  77.  
  78.     // look for and report any errors. (better error handling is on the to do list)
  79.     if( theErr != 0 )
  80.         HandleError( "can't configure serial port _in", theErr, whichSerialPort );
  81.         
  82.     // clear the input buffer
  83.     theErr = SerGetBuf( ddRef_SerialPort[ InRef(whichSerialPort) ], &numBytes );
  84.  
  85.     // look for and report any errors
  86.     if( theErr != 0 )
  87.         HandleError( "can't get number of bytes in input buffer", theErr, whichSerialPort );
  88.     
  89.     // Flush the buffer one byte at a time until empty
  90.     while( numBytes > 0 ) {
  91.         ReadFromSerial(whichSerialPort,1,&flushed_byte);
  92.         numBytes--;
  93.     }
  94. }
  95.  
  96. void CloseSerial( int whichSerialPort )
  97. {
  98.     OSErr theErr;
  99.     
  100.     // close the _in driver
  101.     theErr = CloseDriver( ddRef_SerialPort[ InRef(whichSerialPort) ] );
  102.     
  103.     // look for and report any errors. (better error handling is on the to do list)
  104.     if( theErr != 0 )
  105.         HandleError( "can't close serial port", theErr, whichSerialPort );
  106.     
  107.     // close the _out driver
  108.     theErr = CloseDriver( ddRef_SerialPort[ OutRef(whichSerialPort) ] );
  109.  
  110.     // look for and report any errors. (better error handling is on the to do list)
  111.     if( theErr != 0 )
  112.         HandleError( "can't close serial port", theErr, whichSerialPort );
  113. }
  114.  
  115. void ReadFromSerial( int whichSerialPort, long howManyBytes, Ptr dataPtr )
  116. //
  117. // read 'howManyBytes' from 'whichSerialPort', reading into memory pointed to by dataPtr.
  118. //
  119. {
  120.     OSErr     theErr;
  121.     long     bytesRead = 0;
  122.     short     ddRef;    // reference to the device we're reading from
  123.     long     startTime;
  124.     
  125.     ddRef = ddRef_SerialPort[ InRef(whichSerialPort) ]; // compute this once, use many.
  126.     
  127.     GetDateTime( &startTime );
  128.     
  129.     // note: use SerGetBuf so we can timeout instead of hang..
  130.     while( bytesRead < howManyBytes )
  131.     {
  132.         SerGetBuf( ddRef, &bytesRead );
  133.         
  134.         // timeout code: (we're using the low memory global Time.. use 'GetDateTime' to
  135.         // be more toolbox compliant, but this is faster.
  136.         if( Time - startTime > k_serialTimeout )
  137.             HandleError( "serial port timed out!", 0, whichSerialPort );
  138.     }
  139.     
  140.     // SerGetBytes is telling us its there for the picking.. go get it.
  141.     theErr = FSRead( ddRef, &howManyBytes, dataPtr );
  142. }
  143.  
  144. void WriteToSerial( int whichSerialPort, long howManyBytes, Ptr dataPtr )
  145. //
  146. // write 'howManyBytes' to 'whichSerialPort', reading from memory pointed to by dataPtr.
  147. //
  148. {
  149.     OSErr theErr;    
  150.  
  151.     // send it out..    
  152.     theErr = FSWrite( ddRef_SerialPort[ OutRef(whichSerialPort) ], &howManyBytes, dataPtr );
  153. }
  154.  
  155. void HandleError( char *errMsg, short errNum, int whichSerialPort )
  156. {
  157.     char ErrorString[ 256 ];
  158.     
  159.     // we always reference ports so far, but you _could_ pass -1 (#define me please!)
  160.     // to ignore that feature.
  161.     if( whichSerialPort == -1 )
  162.         sprintf( ErrorString, "%s", errMsg );
  163.     else
  164.         sprintf( ErrorString, "%s [Port: %s]", errMsg, 
  165.                     k_UnderstandablePortNames[ whichSerialPort ] );
  166.  
  167.     // use ReportError or just print to a console window. (a la use_report_error being set)
  168. #if use_report_error
  169.     ReportError( ErrorString, errNum );
  170. #else
  171.     printf( "%s\nError number: %d\nClick the mouse to exit.\n", ErrorString, errNum );
  172.     while( !Button() );
  173.     ExitToShell();
  174. #endif
  175. }