[comp.sys.mac.programmer] serial driver routines on Macintosh in LIGHTSPEED C

sfm3166@ultb.UUCP (S.F. Modi) (06/22/89)

Hi  there everyone (anyone ?????).
I have to write a program to transfer/receive data through the MODEM
port in LIGHTSPEED C on the Macintosh.  Problem is I have never used the Mac 
Toolbox routines before. So it would help me if I could take a look at any code
written for init & calling the driver routines for the modem port
in LIGHTSPEED C. I just need to take a look at code to give me a start.
Any help would be greatly appreciated.
		Thankyou
			Sohrab Modi

please reply to:  {rutgers,cornell}!rochester!rit!ultb!sfm3166
 

sdh@wind.bellcore.com (Stephen D Hawley) (06/22/89)

Here is a bare-bones terminal program (I don't even dare using the word
emulator).  It will give you enough information to start up on your own.

Beware that you will have to put considerably more work into this to get
flow control and all those other bells and whistles to work properly.

Steve Hawley
sdh@flash.bellcore.com
PS this was at Sumex, but I couldn't find it there...

/* sertest.c		5/4/87
 * Steve Hawley
 * A program to use the serial driver.  While it works, this is not
 * a program to be used, but instead to be used as a model.
 * You should be able to figure out how to use the serial port
 * pretty well from this.  I use an integer 'e' in several places
 * for error checking, but didn't actually _do_ any checking.  This
 * is just to point out where checking could be done for user
 * friendliness.
 * This is written for LightspeedC, but could probably compile under
 * Aztec C as well by changing the include file names, and linking
 * it to run under the Aztec shell only or with mixcroot.o, but I
 * haven't tried this.
 * under LightspeedC, it will need the MacTraps and stdio libraries in
 * the project.
 */
#include <stdio.h>
#include <Quickdraw.h>
#include <EventMgr.h>
#include <SerialDvr.h>

struct port {
 /* A simple structure to hold the port settings */
	int refin, refout; /* the refin and out numbers */
	unsigned short baud, parity, stopbits, databits;
	/* all the rest of those wonderful settings */
} PortA; 
char inbuf[1024];

EventRecord myEvent;

main()
{
	
/* this gets the window up for us, if you use your
 * own windows, you can take this out.
 */
	printf("stupid terminal. Steve Hawley.\n");
	printf(" ) 1986 THINK Technologies, Inc.\n");
	printf("Certain portions of this software are copyrighted by "\n);
	printf("THINK Technologies, Inc.\n");
	InitSerial(); /* start up the serial ports */
	
	while(1) {
		getblock();  /* read and print incoming data */
		GetNextEvent(everyEvent, &myEvent);
		/* snag mouse and key events */
		if (myEvent.what == mouseDown) break; /* exit on button press */
		else if (myEvent.what == keyDown || myEvent.what == autoKey) {
			putout((int)myEvent.message & 0x7f); /* send key out port */
			/* its being anded to send only 7 bits. This will not send
			 * control characters.  They have to be fudged by checking
			 * for event modifiers.
			 */
		}
	}
}

InitSerial()
{
	int e; /* Error returned by routines */
	
	PortA.baud = baud9600; /* baud rate */
	PortA.parity = noParity; /* parity */
	PortA.databits = data8;  /* 8 bits of data */
	PortA.stopbits = stop10; /* 1 stop bit */
	
	e = OpenDriver("\P.aout", &PortA.refout); /* open the ROM drivers */
	e = OpenDriver("\P.ain", &PortA.refin);
	/* if e isn't noErr upon return, something bad has happened.
	 */

	SerReset(PortA.refin,
		PortA.baud | PortA.parity | PortA.stopbits | PortA.databits);
	/* fill in our default settings */
	SerSetBuf(PortA.refin, inbuf, 1024);
	/* make a bigger buffer so things won't overflow as fast */
}

getblock()
{
	unsigned char c[1024]; /* big buffer */
	int e; /* error codes */
	register long i; /* index for chars */
	long whatread; /* how many chars are actually waiting */
	
	SerGetBuf(PortA.refin, &whatread); /* anything there? */
	if (whatread > 0) {
		if (whatread > 1024) whatread = 1024L;
		/* if more than our buffer (shouldn't happen) coerce to
		 * the buffer size, so we don't go destroying memory.
		 * If there's more, it will have to wait until the next pass
		 */
		e = FSRead(PortA.refin, &whatread, c); /* Grab the chars */
		if (e == 0) /* no error, print the characters */
			for (i=0; i< whatread; i++) putch(c[i] & 0x7f);
			/* we mask off the 8th bit, 'cause it doesn't really
			 * mean anything to us.
			 */
	}
}

putout(c)
char c;
{
	long cnt = 1;
	int e;
	
	e = FSWrite(PortA.refout, &cnt, &c); /* write out 1 character. */
	/* this could very easily be a #define statement since its so simple.
	 * note that for large blocks of data (ie, transfer of a file) where
	 * throughput faster than typing is required, a routine that buffers
	 * should be used.  That is, fill a large block of memory with the
	 * data to be written out, than write it as one block instead of
	 * sending it all one character at a time.
	 */
}