home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / misc / dr.str / Source / SerialI_O.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-22  |  2.7 KB  |  98 lines

  1. #include <Serial.h>                /* For THINK C 5.0.2, was <SerialDvr.h> for 4.0.5*/
  2. #include <stdlib.h>
  3. #include "SerialI/O.h"
  4.  
  5. static short    inRefNum = 0;                /* Current input reference number */
  6. static short    outRefNum = 0;                /* Current output reference number */
  7. static char        *inBuffer;                    /* Serial Input Buffer */
  8.  
  9.  
  10. /********************************
  11.   Macintosh Serial Port Routines
  12.  ********************************/
  13.  
  14. void    CloseSerial(void)
  15.     /* Close input and output serial drivers. Be sure to do this before exiting
  16.         your application! The Macintosh OS does not take care of it for you, and
  17.         it could cause some problems. */
  18. {
  19.     if (inRefNum != 0) {
  20.         CloseDriver(inRefNum);
  21.     }
  22.     if (outRefNum != 0) {
  23.         CloseDriver(outRefNum);
  24.     }
  25. }
  26.  
  27. Boolean ChangeSettings(int configuration)
  28. {
  29.     OSErr    err;
  30.     
  31.     err = SerReset(outRefNum, configuration);
  32.     if (err == noErr)
  33.         err = SerReset(inRefNum, configuration);
  34.     if (err != noErr) return FALSE;
  35. }
  36.  
  37. Boolean OpenSerial(int configuration)
  38.     /* Open the modem port and set it to read the EVBU
  39.        Return FALSE if there is an error */
  40. {
  41.     OSErr   err;
  42.  
  43.     err = OpenDriver(PORTIN, &inRefNum);            /* Open port input */
  44.     if (err == noErr) {
  45.         err = OpenDriver(PORTOUT, &outRefNum);      /* Open port output */
  46.         if (err != noErr) {                         /* Output open error */
  47.             CloseDriver(inRefNum);
  48.             inRefNum = outRefNum = 0;
  49.         }
  50.     }
  51.     if (err != noErr) return FALSE;
  52.  
  53.         /* Now set port to parameters in configuration */
  54.     err = SerReset(outRefNum, configuration);
  55.     if (err == noErr)
  56.         err = SerReset(inRefNum, configuration);
  57.     if (err != noErr) return FALSE;
  58.     
  59.         /* Create an input buffer for data read from the serial port */
  60.     inBuffer = NewPtr(BUFSIZE);
  61.     err = SerSetBuf(inRefNum, inBuffer, BUFSIZE);
  62.     if (inBuffer == NULL && BUFSIZE != 0)
  63.             err = -1;
  64.     
  65.     if (err != noErr) return FALSE;
  66.     else return TRUE;
  67. }
  68.  
  69. long    GetSerial(char *buffer)
  70.     /* Read bytes from the port into the given buffer, return # of bytes read */
  71. {
  72.     IOParam         PortPB;
  73.     long            bytes;
  74.     OSErr           err;
  75.     
  76.     err = SerGetBuf(inRefNum, &bytes);
  77.     if (err == noErr && bytes > 0) {
  78.         PortPB.ioRefNum = inRefNum;
  79.         PortPB.ioBuffer = buffer;
  80.         PortPB.ioReqCount = bytes;
  81.         err = PBRead(&PortPB, FALSE);
  82.     }
  83.     if (err != noErr)
  84.         bytes = -1;
  85.     return bytes;
  86. }
  87.  
  88. OSErr   PutSerial(char *buffer, long bytes)
  89.     /* Send bytes from the buffer to the port */
  90. {
  91.     IOParam     PortPB;
  92.     
  93.     PortPB.ioRefNum = outRefNum;
  94.     PortPB.ioBuffer = buffer;
  95.     PortPB.ioReqCount = bytes;
  96.     return PBWrite(&PortPB, FALSE);
  97. }
  98.