home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / c_sertes.sit < prev    next >
Encoding:
Text File  |  1988-06-20  |  4.4 KB  |  136 lines

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