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

  1. /*
  2.  * Simple_Serial.c
  3.  *
  4.  * This is an example of using the serial device.  First, we will attempt
  5.  * to create a message port with CreateMsgPort().  Next, we will attempt
  6.  * to create the IORequest with CreateExtIO().  Then, we will attempt to
  7.  * open the serial device with OpenDevice().  If successful, we will write
  8.  * a NULL-terminated string to it and reverse our steps.  If we encounter
  9.  * an error at any time, we will gracefully exit.
  10.  *
  11.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  12.  *
  13.  * Run from CLI only
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/io.h>
  19. #include <devices/serial.h>
  20.  
  21. #include <clib/exec_protos.h>
  22. #include <clib/alib_protos.h>
  23.  
  24. #include <stdio.h>
  25.  
  26. #ifdef LATTICE
  27. int CXBRK(void) { return(0); }  /* Disable SAS CTRL/C handling */
  28. int chkabort(void) { return(0); }  /* really */
  29. #endif
  30.  
  31. void main(void)
  32. {
  33. struct MsgPort *SerialMP;       /* pointer to our message port */
  34. struct IOExtSer *SerialIO;      /* pointer to our IORequest */
  35.  
  36. /* Create the message port */
  37. if (SerialMP=CreateMsgPort())
  38.     {
  39.     /* Create the IORequest */
  40.     if (SerialIO = (struct IOExtSer *)
  41.                     CreateExtIO(SerialMP,sizeof(struct IOExtSer)))
  42.         {
  43.         /* Open the serial device */
  44.         if (OpenDevice(SERIALNAME,0,(struct IORequest *)SerialIO,0L))
  45.  
  46.             /* Inform user that it could not be opened */
  47.             printf("Error: %s did not open\n",SERIALNAME);
  48.         else
  49.             {
  50.             /* device opened, write NULL-terminated string */
  51.             SerialIO->IOSer.io_Length   = -1;
  52.             SerialIO->IOSer.io_Data     = (APTR)"Amiga ";
  53.             SerialIO->IOSer.io_Command  = CMD_WRITE;
  54.             if (DoIO((struct IORequest *)SerialIO))     /* execute write */
  55.                 printf("Write failed.  Error - %ld\n",SerialIO->IOSer.io_Error);
  56.  
  57.             /* Close the serial device */
  58.             CloseDevice((struct IORequest *)SerialIO);
  59.             }
  60.         /* Delete the IORequest */
  61.         DeleteExtIO(SerialIO);
  62.         }
  63.     else
  64.         /* Inform user that the IORequest could be created */
  65.         printf("Error: Could create IORequest\n");
  66.  
  67.     /* Delete the message port */
  68.     DeleteMsgPort(SerialMP);
  69.     }
  70. else
  71.     /* Inform user that the message port could not be created */
  72.     printf("Error: Could not create message port\n");
  73. }
  74.  
  75. /*
  76.    DoIO() vs. SendIO().
  77.    --------------------
  78.    The above example code contains some simplifications.  The DoIO()
  79.    function in the example is not always appropriate for executing the
  80.    CMD_READ or CMD_WRITE commands.  DoIO() will not return until the I/O
  81.    request has finished.  With serial handshaking enabled, a write
  82.    request may never finish.  Read requests will not finish until
  83.    characters arrive at the serial port.  The following sections will
  84.    demonstrate a solution using the SendIO() and AbortIO() functions.
  85.  
  86. */
  87.