13-May-87 10:08:05-PDT,4229;000000000001 Date: Wed, 13 May 87 10:06:42 PDT From: Marc Hannah Subject: another usenet posting to be forwarded to infomac >From: sdh@thumper.UUCP (Retief of the CDT) >Subject: A stripped-down terminal program >Date: 4 May 87 21:36:31 GMT /* 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 #include #include #include 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. */ }