home *** CD-ROM | disk | FTP | other *** search
- #include <Serial.h> /* For THINK C 5.0.2, was <SerialDvr.h> for 4.0.5*/
- #include <stdlib.h>
- #include "SerialI/O.h"
-
- static short inRefNum = 0; /* Current input reference number */
- static short outRefNum = 0; /* Current output reference number */
- static char *inBuffer; /* Serial Input Buffer */
-
-
- /********************************
- Macintosh Serial Port Routines
- ********************************/
-
- void CloseSerial(void)
- /* Close input and output serial drivers. Be sure to do this before exiting
- your application! The Macintosh OS does not take care of it for you, and
- it could cause some problems. */
- {
- if (inRefNum != 0) {
- CloseDriver(inRefNum);
- }
- if (outRefNum != 0) {
- CloseDriver(outRefNum);
- }
- }
-
- Boolean ChangeSettings(int configuration)
- {
- OSErr err;
-
- err = SerReset(outRefNum, configuration);
- if (err == noErr)
- err = SerReset(inRefNum, configuration);
- if (err != noErr) return FALSE;
- }
-
- Boolean OpenSerial(int configuration)
- /* Open the modem port and set it to read the EVBU
- Return FALSE if there is an error */
- {
- OSErr err;
-
- err = OpenDriver(PORTIN, &inRefNum); /* Open port input */
- if (err == noErr) {
- err = OpenDriver(PORTOUT, &outRefNum); /* Open port output */
- if (err != noErr) { /* Output open error */
- CloseDriver(inRefNum);
- inRefNum = outRefNum = 0;
- }
- }
- if (err != noErr) return FALSE;
-
- /* Now set port to parameters in configuration */
- err = SerReset(outRefNum, configuration);
- if (err == noErr)
- err = SerReset(inRefNum, configuration);
- if (err != noErr) return FALSE;
-
- /* Create an input buffer for data read from the serial port */
- inBuffer = NewPtr(BUFSIZE);
- err = SerSetBuf(inRefNum, inBuffer, BUFSIZE);
- if (inBuffer == NULL && BUFSIZE != 0)
- err = -1;
-
- if (err != noErr) return FALSE;
- else return TRUE;
- }
-
- long GetSerial(char *buffer)
- /* Read bytes from the port into the given buffer, return # of bytes read */
- {
- IOParam PortPB;
- long bytes;
- OSErr err;
-
- err = SerGetBuf(inRefNum, &bytes);
- if (err == noErr && bytes > 0) {
- PortPB.ioRefNum = inRefNum;
- PortPB.ioBuffer = buffer;
- PortPB.ioReqCount = bytes;
- err = PBRead(&PortPB, FALSE);
- }
- if (err != noErr)
- bytes = -1;
- return bytes;
- }
-
- OSErr PutSerial(char *buffer, long bytes)
- /* Send bytes from the buffer to the port */
- {
- IOParam PortPB;
-
- PortPB.ioRefNum = outRefNum;
- PortPB.ioBuffer = buffer;
- PortPB.ioReqCount = bytes;
- return PBWrite(&PortPB, FALSE);
- }
-