home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / DeviceUse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  5.8 KB  |  104 lines

  1. ;/* DeviceUse.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 DeviceUse.c
  3. Blink FROM LIB:c.o,DeviceUse.o TO DeviceUse LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. /* The following short example demonstrates use of an Amiga device.  The */
  7. /* example opens the serial.device and then demonstrates both            */
  8. /* synchronous (DoIO()) and asynchronous (SendIO()) use of the serial    */
  9. /* command SDCMD_QUERY.  This command is used to determine the status of */
  10. /* the serial device lines and registers.  The example uses the backward */
  11. /* compatible amiga.lib functions for creation and deletion of the       */
  12. /* message port and I/O request.                                         */
  13.  
  14. /* DeviceUse.c - an example of using an Amiga device (here, serial device)    */
  15. /*    - attempt to create a message port with CreatePort()   (from amiga.lib) */
  16. /*    - attempt to create the I/O request with CreateExtIO() (from amiga.lib) */
  17. /*    - attempt to open the serial device with Exec OpenDevice()              */
  18. /*                                                                            */
  19. /* If successful, use the serial command SDCMD_QUERY, then reverse our steps. */
  20. /* If we encounter an error at any time, we will gracefully exit.  Note that  */
  21. /* applications which require at least V37 OS should use the Exec functions   */
  22. /* CreateMsgPort()/DeleteMsgPort() and CreateIORequest()/DeleteIORequest()    */
  23. /* instead of the similar amiga.lib functions which are used in this example. */
  24.  
  25. #include <exec/types.h>
  26. #include <exec/memory.h>
  27. #include <exec/io.h>
  28. #include <devices/serial.h>
  29.  
  30. #include <clib/exec_protos.h> /* Prototypes for Exec library functions */
  31. #include <clib/alib_protos.h> /* Prototypes for amiga.lib functions    */
  32.  
  33. #include <stdio.h>
  34.  
  35. #ifdef LATTICE
  36. int CXBRK(void) { return(0); }     /* Disable SAS CTRL/C handling */
  37. int chkabort(void) { return(0); }  /* really */
  38. #endif
  39.  
  40. void main(void)
  41. {
  42.     struct MsgPort  *serialMP;      /* for pointer to our message port */
  43.     struct IOExtSer *serialIO;      /* for pointer to our I/O request  */
  44.     struct IOExtSer *reply;         /* for use with GetMsg             */
  45.  
  46.     if (serialMP=CreatePort(NULL,NULL)) /* Create the message port. */
  47.     {
  48.         /* Create the I/O request. Note that <devices/serial.h> defines the type */
  49.         /* of IORequest required by the serial device--an IOExtSer. Many devices */
  50.         /* require specialized extended IO requests which start with an embedded */
  51.         /* struct IORequest. The generic Exec and amiga.lib device IO functions  */
  52.         /* are prototyped for IORequest, so some pointer casting is necessary.   */
  53.  
  54.         if (serialIO = (struct IOExtSer *)CreateExtIO(serialMP,sizeof(struct IOExtSer)))
  55.         {
  56.             /* Open the serial device (non-zero return value means failure here). */
  57.             if (OpenDevice( SERIALNAME, 0, (struct IORequest *)serialIO, 0L))
  58.                 printf("Error: %s did not open\n",SERIALNAME);
  59.             else
  60.             {
  61.                 /* Device is open */                         /* DoIO - demonstrates synchronous */
  62.                 serialIO->IOSer.io_Command  = SDCMD_QUERY;   /* device use, returns error or 0. */
  63.                 if (DoIO((struct IORequest *)serialIO))
  64.                     printf("Query  failed. Error - %d\n",serialIO->IOSer.io_Error);
  65.                 else
  66.                     /* Print serial device status - see include file for meaning */
  67.                     /* Note that with DoIO, the Wait and GetMsg are done by Exec */
  68.                     printf("Serial device status: $%x\n\n",serialIO->io_Status);
  69.  
  70.                 serialIO->IOSer.io_Command  = SDCMD_QUERY; /* SendIO - demonstrates asynchronous */
  71.                 SendIO((struct IORequest *)serialIO);      /* device use (returns immediately).  */
  72.  
  73.                 /* We could do other things here while the query is being done.      */
  74.                 /* And to manage our asynchronous device IO:                         */
  75.                 /*   - we can CheckIO(serialIO) to check for completion              */
  76.                 /*   - we can AbortIO(serialIO) to abort the command                 */
  77.                 /*   - we can WaitPort(serialMP) to wait for any serial port reply   */
  78.                 /*  OR we can WaitIO(serialIO) to wait for this specific IO request  */
  79.                 /*  OR we can Wait(1L << serialMP_>mp_SigBit) for reply port signal  */
  80.  
  81.                 Wait(1L << serialMP->mp_SigBit);
  82.  
  83.                 while(reply = (struct IOExtSer *)GetMsg(serialMP))
  84.                 {    /* Since we sent out only one serialIO request the while loop is */
  85.                      /* not really needed--we only expect one reply to our one query  */
  86.                      /* command, and the reply message pointer returned by GetMsg()   */
  87.                      /* will just be another pointer to our one serialIO request.     */
  88.                      /* With Wait() or WaitPort(), you must GetMsg() the message.     */
  89.                     if(reply->IOSer.io_Error)
  90.                         printf("Query  failed. Error - %d\n",reply->IOSer.io_Error);
  91.                     else
  92.                         printf("Serial device status: $%x\n\n",reply->io_Status);
  93.                 }
  94.                 CloseDevice((struct IORequest *)serialIO);  /* Close the serial device.    */
  95.             }
  96.             DeleteExtIO(serialIO);                          /* Delete the I/O request.     */
  97.         }
  98.         else printf("Error: Could create I/O request\n");   /* Inform user that the I/O    */
  99.                                                             /* request could be created.   */
  100.         DeletePort(serialMP);                               /* Delete the message port.    */
  101.     }
  102.     else printf("Error: Could not create message port\n");  /* Inform user that the message*/
  103. }                                                           /* port could not be created.  */
  104.