home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / introduction / v36_device_use.c < prev   
C/C++ Source or Header  |  1992-09-03  |  4KB  |  114 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  ***************************************************************************
  27.  *
  28.  * V36_Device_Use.c
  29.  *
  30.  * This is an example of using the serial device.
  31.  * First, we will attempt to create a message port with CreateMsgPort()
  32.  * Next, we will attempt to create the IORequest with CreateIORequest()
  33.  * Then, we will attempt to open the serial device with OpenDevice()
  34.  * If successful, we will send the SDCMD_QUERY command to it
  35.  * and reverse our steps.
  36.  * If we encounter an error at any time, we will gracefully exit.
  37.  *
  38.  * Compile with SAS C 5.10  lc -cfistq -v -y -L
  39.  *
  40.  * Requires Kickstart V36 or greater.
  41.  *
  42.  * Run from CLI only
  43.  */
  44.  
  45. #include <exec/types.h>
  46. #include <exec/memory.h>
  47. #include <exec/io.h>
  48. #include <exec/libraries.h>
  49. #include <devices/serial.h>
  50.  
  51. #include <clib/exec_protos.h>
  52. #include <clib/alib_protos.h>
  53.  
  54. #include <stdio.h>
  55.  
  56. #ifdef LATTICE
  57. int CXBRK(void) { return(0); }    /* Disable SAS CTRL/C handling */
  58. int chkabort(void) { return(0); } /* really */
  59. #endif
  60.  
  61. extern struct Library *SysBase;   /* Use this to check the OS version number */
  62.  
  63. void main(void)
  64. {
  65. struct MsgPort *SerialMP;         /* pointer to our message port */
  66. struct IOExtSer *SerialIO;        /* pointer to our IORequest */
  67.  
  68.    /* Check the version of the operating system */
  69. if(SysBase->lib_Version>=36)
  70.    {
  71.       /* Create the message port */
  72.    if (SerialMP=CreateMsgPort())
  73.       {
  74.          /* Create the IORequest */
  75.       if (SerialIO = CreateIORequest(SerialMP,sizeof(struct IOExtSer)))
  76.          {
  77.             /* Open the serial device */
  78.          if (OpenDevice(SERIALNAME,0,(struct IORequest *)SerialIO,0L))
  79.  
  80.             /* Inform user that it could not be opened */
  81.             printf("Error: %s did not open\n",SERIALNAME);
  82.          else
  83.             {
  84.             /* device opened, send query command to it */
  85.             SerialIO->IOSer.io_Command  = SDCMD_QUERY;
  86.             if (DoIO((struct IORequest *)SerialIO))
  87.  
  88.                /* Inform user that query failed */
  89.                printf("Query  failed. Error - %d\n",SerialIO->IOSer.io_Error);
  90.             else
  91.                /* Print serial device status - see include file for meaning */
  92.                printf("\n\tSerial device status: %x\n\n",SerialIO->io_Status);
  93.  
  94.             /* Close the serial device */
  95.             CloseDevice((struct IORequest *)SerialIO);
  96.             }
  97.          /* Delete the IORequest */
  98.          DeleteIORequest(SerialIO);
  99.          }
  100.       else
  101.          /* Inform user that the IORequest could be created */
  102.          printf("Error: Could create IORequest\n");
  103.  
  104.       /* Delete the message port */
  105.       DeleteMsgPort(SerialMP);
  106.       }
  107.    else
  108.       /* Inform user that the message port could not be created */
  109.       printf("Error: Could not create message port\n");
  110.    }
  111. else
  112.    printf("Error: Release 2 (V36) or a later version of the OS is required.\n");
  113. }
  114.