[comp.sys.mac.programmer] Reading chars from serial port

peterson@peterson.applicon.UUCP (07/20/88)

I want to read (at fairly high speed) characters from the serial port
until I get a certain character.  It would also be nice to timeout
if there is a large blank period.

The Serial Driver routines are not explained very clearly in Inside Mac.
I'm not sure if FSRead is capable of reading less characters than you
specify (I'm not sure how many characters will be read - only that
the last character is a known value).  I am trying to read about 64K.

This task was easy on my old PC.  I just wrote a tight loop polling the
serial port.  But the Mac does not seem to have an equivalent "GetChar."

Somebody must have done this; please help me!

Joe Peterson
Schlumberger
Billerica, Ma

uucp:  ...mit-eddie!applicon!peterson

beard@ux1.lbl.gov (Patrick C Beard) (07/30/88)

In article <77900004@peterson> peterson@peterson.applicon.UUCP writes:
>
>I want to read (at fairly high speed) characters from the serial port
>until I get a certain character. 
>
>Joe Peterson
>Schlumberger
>Billerica, Ma
>
>uucp:  ...mit-eddie!applicon!peterson

Here is how I do it:

/* serialhelp.c */

#include <SerialDvr.h>

SetUpPort(port,buf,length, inRefPtr, outRefPtr)
SPortSel port;	/* selector for which port to set up */
char *buf;
long length;
int *inRefPtr, *outRefPtr;
{
	/* decide which port to open -- port A is modem, B printer */
	switch(port) {
	case sPortA:
		error=OpenDriver("\p.AIn",inRefPtr);
		error=OpenDriver("\p.Aout",outRefPtr);
		break;
	case sPortB:
		error=OpenDriver("\p.BIn",inRefPtr);
		error=OpenDriver("\p.Bout",outRefPtr);
		break;
	}

	/* set up the speed of the buffers -- hardwired to 1200 baud here */
	SerReset(*outRefPtr,baud1200+stop10+data8+noParity);
	SerReset(*inRefPtr,baud1200+stop10+data8+noParity);
	
	/* set up the incoming buffer */
	SerSetBuf(*inRefPtr, buf, length);
}

/* non-blocking read of serial port */
ModemGetChar(inRef)
int inRef;	/* reference to the serial port */
{
	long count, bytes;
	char theChar;
	
	SerGetBuf(inRef,&count);
	
	if(count) {	/* if any characters, then read one of them */
		bytes=1L;
		FSRead(inRef,&bytes,&theChar);
	} else {
		theChar=0;	/* flag to indicate no character available */
	}

	return(theChar);
}

I hope this helps you.

Patrick Beard
Lawrence Berkeley Laboratory
beard@ux1.lbl.gov