home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / serial / serial2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  6.7 KB  |  224 lines

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