home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / lang / serialte.c < prev    next >
C/C++ Source or Header  |  1987-05-25  |  4KB  |  128 lines

  1. 13-May-87 10:08:05-PDT,4229;000000000001
  2. Date: Wed, 13 May 87 10:06:42 PDT
  3. From: Marc Hannah <mrh@shasta.stanford.edu>
  4. Subject: another usenet posting to be forwarded to infomac
  5.  
  6. >From: sdh@thumper.UUCP (Retief of the CDT)
  7. >Subject: A stripped-down terminal program
  8. >Date: 4 May 87 21:36:31 GMT
  9.  
  10.  
  11. /* sertest.c        5/4/87
  12.  * Steve Hawley
  13.  * A program to use the serial driver.  While it works, this is not
  14.  * a program to be used, but instead to be used as a model.
  15.  * You should be able to figure out how to use the serial port
  16.  * pretty well from this.  I use an integer 'e' in several places
  17.  * for error checking, but didn't actually _do_ any checking.  This
  18.  * is just to point out where checking could be done for user
  19.  * friendliness.
  20.  * This is written for LightspeedC, but could probably compile under
  21.  * Aztec C as well by changing the include file names, and linking
  22.  * it to run under the Aztec shell only or with mixcroot.o, but I
  23.  * haven't tried this.
  24.  * under LightspeedC, it will need the MacTraps and stdio libraries in
  25.  * the project.
  26.  */
  27. #include <stdio.h>
  28. #include <Quickdraw.h>
  29. #include <EventMgr.h>
  30. #include <SerialDvr.h>
  31.  
  32. struct port {
  33.  /* A simple structure to hold the port settings */
  34.     int refin, refout; /* the refin and out numbers */
  35.     unsigned short baud, parity, stopbits, databits;
  36.     /* all the rest of those wonderful settings */
  37. } PortA; 
  38. char inbuf[1024];
  39.  
  40. EventRecord myEvent;
  41.  
  42. main()
  43. {
  44.     
  45. /* this gets the window up for us, if you use your
  46.  * own windows, you can take this out.
  47.  */
  48.     printf("stupid terminal. Steve Hawley.\n");
  49.     printf(" ) 1986 THINK Technologies, Inc.\n");
  50.     printf("Certain portions of this software are copyrighted by "\n);
  51.     printf("THINK Technologies, Inc.\n");
  52.     InitSerial(); /* start up the serial ports */
  53.     
  54.     while(1) {
  55.         getblock();  /* read and print incoming data */
  56.         GetNextEvent(everyEvent, &myEvent);
  57.         /* snag mouse and key events */
  58.         if (myEvent.what == mouseDown) break; /* exit on button press */
  59.         else if (myEvent.what == keyDown || myEvent.what == autoKey) {
  60.             putout((int)myEvent.message & 0x7f); /* send key out port */
  61.             /* its being anded to send only 7 bits. This will not send
  62.              * control characters.  They have to be fudged by checking
  63.              * for event modifiers.
  64.              */
  65.         }
  66.     }
  67. }
  68.  
  69. InitSerial()
  70. {
  71.     int e; /* Error returned by routines */
  72.     
  73.     PortA.baud = baud9600; /* baud rate */
  74.     PortA.parity = noParity; /* parity */
  75.     PortA.databits = data8;  /* 8 bits of data */
  76.     PortA.stopbits = stop10; /* 1 stop bit */
  77.     
  78.     e = OpenDriver("\P.aout", &PortA.refout); /* open the ROM drivers */
  79.     e = OpenDriver("\P.ain", &PortA.refin);
  80.     /* if e isn't noErr upon return, something bad has happened.
  81.      */
  82.  
  83.     SerReset(PortA.refin,
  84.         PortA.baud | PortA.parity | PortA.stopbits | PortA.databits);
  85.     /* fill in our default settings */
  86.     SerSetBuf(PortA.refin, inbuf, 1024);
  87.     /* make a bigger buffer so things won't overflow as fast */
  88. }
  89.  
  90. getblock()
  91. {
  92.     unsigned char c[1024]; /* big buffer */
  93.     int e; /* error codes */
  94.     register long i; /* index for chars */
  95.     long whatread; /* how many chars are actually waiting */
  96.     
  97.     SerGetBuf(PortA.refin, &whatread); /* anything there? */
  98.     if (whatread > 0) {
  99.         if (whatread > 1024) whatread = 1024L;
  100.         /* if more than our buffer (shouldn't happen) coerce to
  101.          * the buffer size, so we don't go destroying memory.
  102.          * If there's more, it will have to wait until the next pass
  103.          */
  104.         e = FSRead(PortA.refin, &whatread, c); /* Grab the chars */
  105.         if (e == 0) /* no error, print the characters */
  106.             for (i=0; i< whatread; i++) putch(c[i] & 0x7f);
  107.             /* we mask off the 8th bit, 'cause it doesn't really
  108.              * mean anything to us.
  109.              */
  110.     }
  111. }
  112.  
  113. putout(c)
  114. char c;
  115. {
  116.     long cnt = 1;
  117.     int e;
  118.     
  119.     e = FSWrite(PortA.refout, &cnt, &c); /* write out 1 character. */
  120.     /* this could very easily be a #define statement since its so simple.
  121.      * note that for large blocks of data (ie, transfer of a file) where
  122.      * throughput faster than typing is required, a routine that buffers
  123.      * should be used.  That is, fill a large block of memory with the
  124.      * data to be written out, than write it as one block instead of
  125.      * sending it all one character at a time.
  126.      */
  127. }
  128.