home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 1.2 / amidev_cd_12.iso / reference_library / devices / dev_examples / complex_serial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  7.3 KB  |  228 lines

  1. /*
  2.  * Complex_Serial.c
  3.  *
  4.  * Complex tricky example of serial.device usage
  5.  *
  6.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  7.  *
  8.  * Run from CLI only
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <exec/io.h>
  14. #include <devices/serial.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/alib_protos.h>
  18.  
  19. #include <stdio.h>
  20.  
  21. #ifdef LATTICE
  22. int CXBRK(void) { return(0); }  /* Disable SAS CTRL/C handling */
  23. int chkabort(void) { return(0); }  /* really */
  24. #endif
  25.  
  26.  
  27. void main(void)
  28. {
  29. struct MsgPort  *SerialMP;          /* Define storage for one pointer */
  30. struct IOExtSer *SerialIO;         /* Define storage for one pointer */
  31.  
  32. #define READ_BUFFER_SIZE 32
  33. char SerialReadBuffer[READ_BUFFER_SIZE]; /* Reserve SIZE bytes storage */
  34.  
  35. struct IOExtSer *SerialWriteIO = 0;
  36. struct MsgPort  *SerialWriteMP = 0;
  37.  
  38. ULONG Temp;
  39. ULONG WaitMask;
  40.  
  41. if (SerialMP=CreatePort(0,0) )
  42.     {
  43.     if (SerialIO=(struct IOExtSer *)
  44.                  CreateExtIO(SerialMP,sizeof(struct IOExtSer)) )
  45.         {
  46.         SerialIO->io_SerFlags=0;    /* Example of setting flags */
  47.  
  48.         if (OpenDevice(SERIALNAME,0L,SerialIO,0) )
  49.             printf("%s did not open\n",SERIALNAME);
  50.         else
  51.             {
  52.             SerialIO->IOSer.io_Command  = SDCMD_SETPARAMS;
  53.             SerialIO->io_SerFlags      &= ~SERF_PARTY_ON;
  54.             SerialIO->io_SerFlags      |= SERF_XDISABLED;
  55.             SerialIO->io_Baud           = 9600;
  56.             if (Temp=DoIO(SerialIO))
  57.                 printf("Error setting parameters - code %ld!\n",Temp);
  58.  
  59.             SerialIO->IOSer.io_Command  = CMD_WRITE;
  60.             SerialIO->IOSer.io_Length   = -1;
  61.             SerialIO->IOSer.io_Data     = (APTR)"Amiga.";
  62.             SendIO(SerialIO);
  63.             printf("CheckIO %lx\n",CheckIO(SerialIO));
  64.             printf("The device will process the request in the background\n");
  65.             printf("CheckIO %lx\n",CheckIO(SerialIO));
  66.             WaitIO(SerialIO);
  67.  
  68.             SerialIO->IOSer.io_Command  = CMD_WRITE;
  69.             SerialIO->IOSer.io_Length   = -1;
  70.             SerialIO->IOSer.io_Data     = (APTR)"Save the whales! ";
  71.             DoIO(SerialIO);             /* execute write */
  72.  
  73.  
  74.             SerialIO->IOSer.io_Command  = CMD_WRITE;
  75.             SerialIO->IOSer.io_Length   = -1;
  76.             SerialIO->IOSer.io_Data     = (APTR)"Life is but a dream.";
  77.             DoIO(SerialIO);             /* execute write */
  78.  
  79.             SerialIO->IOSer.io_Command  = CMD_WRITE;
  80.             SerialIO->IOSer.io_Length   = -1;
  81.             SerialIO->IOSer.io_Data     = (APTR)"Row, row, row your boat.";
  82.             SerialIO->IOSer.io_Flags = IOF_QUICK;
  83.             BeginIO(SerialIO);
  84.  
  85.             if (SerialIO->IOSer.io_Flags & IOF_QUICK )
  86.                 {
  87.  
  88.                 /*
  89.                  * Quick IO could not happen for some reason; the device
  90.                  *  processed the command normally.  In this case
  91.                  *  BeginIO() acted exactly like SendIO().
  92.                  */
  93.  
  94.                 printf("Quick IO\n");
  95.                 }
  96.             else
  97.                 {
  98.  
  99.                 /* If flag is still set, IO was synchronous and is now finished.
  100.                  * The IO request was NOT appended a reply port.  There is no
  101.                  * need to remove or WaitIO() for the message.
  102.                  */
  103.  
  104.                 printf("Regular IO\n");
  105.                 }
  106.  
  107.             WaitIO(SerialIO);
  108.  
  109.  
  110.             SerialIO->IOSer.io_Command  = CMD_UPDATE;
  111.             SerialIO->IOSer.io_Length   = -1;
  112.             SerialIO->IOSer.io_Data     = (APTR)"Row, row, row your boat.";
  113.             SerialIO->IOSer.io_Flags = IOF_QUICK;
  114.             BeginIO(SerialIO);
  115.  
  116.             if (0 == SerialIO->IOSer.io_Flags & IOF_QUICK )
  117.                 {
  118.  
  119.                 /*
  120.                  * Quick IO could not happen for some reason; the device processed
  121.                  * the command normally.  In this case BeginIO() acted exactly
  122.                  * like SendIO().
  123.                  */
  124.  
  125.                 printf("Regular IO\n");
  126.  
  127.                 WaitIO(SerialIO);
  128.                 }
  129.             else
  130.                 {
  131.  
  132.                 /* If flag is still set, IO was synchronous and is now finished.
  133.                  * The IO request was NOT appended a reply port.  There is no
  134.                  * need to remove or WaitIO() for the message.
  135.                  */
  136.  
  137.                 printf("Quick IO\n");
  138.                 }
  139.  
  140.  
  141.             /* Precalculate a wait mask for the CTRL-C, CTRL-F and message
  142.              * port signals.  When one or more signals are received,
  143.              * Wait() will return.  Press CTRL-C to exit the example.
  144.              * Press CTRL-F to wake up the example without doing anything.
  145.              * NOTE: A signal may show up without an associated message!
  146.              */
  147.  
  148.             WaitMask = SIGBREAKF_CTRL_C|
  149.                         SIGBREAKF_CTRL_F|
  150.                          1L << SerialMP->mp_SigBit;
  151.  
  152.             SerialIO->IOSer.io_Command  = CMD_READ;
  153.             SerialIO->IOSer.io_Length   = READ_BUFFER_SIZE;
  154.             SerialIO->IOSer.io_Data     = (APTR)&SerialReadBuffer[0];
  155.             SendIO(SerialIO);
  156.  
  157.             printf("Sleeping until CTRL-C, CTRL-F, or serial input\n");
  158.  
  159.             while (1)
  160.                    {
  161.                    Temp = Wait(WaitMask);
  162.                    printf("Just woke up (YAWN!)\n");
  163.  
  164.                    if (SIGBREAKF_CTRL_C & Temp)
  165.                        break;
  166.  
  167.                    if (CheckIO(SerialIO) ) /* If request is complete... */
  168.                        {
  169.                        WaitIO(SerialIO);   /* clean up and remove reply */
  170.  
  171.                        printf("%ld bytes received\n",SerialIO->IOSer.io_Actual);
  172.                        break;
  173.                        }
  174.                    }
  175.  
  176.             AbortIO(SerialIO);  /* Ask device to abort request, if pending */
  177.             WaitIO(SerialIO);   /* Wait for abort, then clean up */
  178.  
  179.  
  180.             /*
  181.              * If two tasks will use the same device at the same time, it is preferred
  182.              * use two OpenDevice() calls and SHARED mode.  If exclusive access mode
  183.              * is required, then you will need to copy an existing IO request.
  184.              *
  185.              * Remember that two separate tasks will require two message ports.
  186.              */
  187.  
  188.             SerialWriteMP = CreatePort(0,0);
  189.             SerialWriteIO = (struct IOExtSer *)
  190.                             CreateExtIO( SerialWriteMP,sizeof(struct IOExtSer) );
  191.  
  192.             if (SerialWriteMP && SerialWriteIO )
  193.                 {
  194.  
  195.                 /* Copy over the entire old IO request, then stuff the
  196.                  * new Message port pointer.
  197.                  */
  198.  
  199.                 CopyMem( SerialIO, SerialWriteIO, sizeof(struct IOExtSer) );
  200.                 SerialWriteIO->IOSer.io_Message.mn_ReplyPort = SerialWriteMP;
  201.  
  202.                 SerialWriteIO->IOSer.io_Command  = CMD_WRITE;
  203.                 SerialWriteIO->IOSer.io_Length   = -1;
  204.                 SerialWriteIO->IOSer.io_Data     = (APTR)"A poet's food is love and fame";
  205.                 DoIO(SerialWriteIO);
  206.                 }
  207.  
  208.             if (SerialWriteMP)
  209.                 DeletePort(SerialWriteMP);
  210.  
  211.             if (SerialWriteIO)
  212.                 DeleteExtIO(SerialWriteIO);
  213.  
  214.             CloseDevice(SerialIO);
  215.             }
  216.  
  217.         DeleteExtIO(SerialIO);
  218.         }
  219.     else
  220.         printf("Unable to create IORequest\n");
  221.  
  222.     DeletePort(SerialMP);
  223.     }
  224. else
  225.     printf("Unable to create message port\n");
  226. }
  227.  
  228.