home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / cmanual-3.0.lha / CManual / Devices / ParallelDevice / Example3.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  20KB  |  504 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Parallel Device             Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-26                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This example is rather similar to Example 1, but this time     */
  23. /* we do not wait for the parallel port to complete our request.  */
  24. /* We are also trying to read and write at the same time. To be   */
  25. /* able to do several requests simultaneously we need one request */
  26. /* block for each command. In this example we use three separate  */
  27. /* request blocks.                                                */
  28.  
  29.  
  30.  
  31. #include <exec/types.h>       /* STRPTR           */
  32. #include <devices/parallel.h> /* Parallel Device  */
  33.  
  34.  
  35.  
  36. /* Size of our data buffer: */
  37. #define MY_BUFFER_SIZE 200
  38.  
  39. /* Parallel flags: (Check for end-of-file characters.) */
  40. #define PARALLEL_FLAGS PARF_EOFMODE
  41.  
  42. /* Additional flags: (Nothing) */
  43. #define ADDITIONAL_FLAGS 0
  44.  
  45.  
  46.  
  47. /* Declare a pointer to our reply port: */
  48. struct MsgPort *replymp = NULL;
  49.  
  50. /* Declare pointers to our parallel request blocks:      */
  51. /* One is used for reading, the other for writing, and   */
  52. /* finally the last one is used for other commands.      */
  53. struct IOExtPar *parallel_req_read = NULL;    /* Read    */
  54. struct IOExtPar *parallel_req_write = NULL;   /* Write   */
  55. struct IOExtPar *parallel_req_command = NULL; /* Command */
  56.  
  57. /* Store the parallel device error here: */
  58. UWORD parallel_dever = TRUE;
  59.  
  60. /* Declare two data buffers. The first one will contain the  */
  61. /* information we want to send, and the other will be filled */
  62. /* with all data we have collected:                          */
  63. BYTE read_buffer[ MY_BUFFER_SIZE ];
  64. BYTE write_buffer[ MY_BUFFER_SIZE ];
  65.  
  66.  
  67.  
  68. /* Declare our functions: */
  69.  
  70. /* Our main function: */
  71. void main();
  72.  
  73. /* Clears and removes everything nice and neatly: */
  74. void clean_up( UBYTE error, STRPTR text );
  75.  
  76. /* Sets the parallel parameters: */
  77. UBYTE SetParParams(
  78.   struct IOExtPar *ioreq,
  79.   UBYTE parallel_flags,
  80.   ULONG extended_flags,
  81.   UBYTE *eof_chars
  82. );
  83.  
  84. /* Explains error messages: */
  85. void ParError( UBYTE error );
  86.  
  87. /* Sends data to the parallel device without waiting: */
  88. void ParWriteNoWait(
  89.   struct IOExtPar *ioreq,
  90.   BYTE *data,
  91.   ULONG length
  92. );
  93.  
  94. /* Collects data from the parallel device without waiting: */
  95. void ParReadNoWait(
  96.   struct IOExtPar *ioreq,
  97.   BYTE *data,
  98.   ULONG length
  99. );
  100.  
  101.  
  102.  
  103. void main()
  104. {
  105.   /* Error number: */
  106.   UBYTE error;
  107.   
  108.   /* Byte pointers, used to copy the request blocks: */
  109.   BYTE *r_ptr;
  110.   BYTE *w_ptr;
  111.   BYTE *c_ptr;
  112.  
  113.   /* Loop variable: */
  114.   int loop;
  115.  
  116.   /* The eight end-of-file characters: */
  117.   /* They MUST be in descending order! */
  118.   UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
  119.  
  120.   
  121.   
  122.   /* Get a reply port: (No name, priority 0) */
  123.   replymp = (struct MsgPort *)
  124.     CreatePort( NULL, 0 );
  125.   if( !replymp )
  126.     clean_up( 0, "Could not create the reply port!" );
  127.  
  128.  
  129.  
  130.   /* Create request block "Read": */
  131.   parallel_req_read = (struct IOExtPar *)
  132.     CreateExtIO( replymp, sizeof( struct IOExtPar ) );
  133.   if( !parallel_req_read )
  134.     clean_up( 0, "Not enough memory for the request block Read!" );
  135.  
  136.   /* Create request block "Write": */
  137.   parallel_req_write = (struct IOExtPar *)
  138.     CreateExtIO( replymp, sizeof( struct IOExtPar ) );
  139.   if( !parallel_req_write )
  140.     clean_up( 0, "Not enough memory for the request block Write!" );
  141.  
  142.   /* Create request block "Command": */
  143.   parallel_req_command = (struct IOExtPar *)
  144.     CreateExtIO( replymp, sizeof( struct IOExtPar ) );
  145.   if( !parallel_req_command )
  146.     clean_up( 0, "Not enough memory for the request block Command!" );
  147.  
  148.  
  149.  
  150.  
  151.   /* Open the parallel device for the read request: */
  152.   parallel_dever = OpenDevice( PARALLELNAME, 0, parallel_req_read, 0 );
  153.   if( parallel_dever )
  154.     clean_up( parallel_dever, "Could not open the Parallel Device!" );
  155.  
  156.   /* Since we can not open the parallel device any more  */
  157.   /* for the other request (we use exclusive access), we */
  158.   /* have to copy the whole read request block into the  */
  159.   /* write and command request blocks.                   */
  160.  
  161.   /* Get the start address of all request blocks: */
  162.   r_ptr = (BYTE *) parallel_req_read;
  163.   w_ptr = (BYTE *) parallel_req_write;
  164.   c_ptr = (BYTE *) parallel_req_command;
  165.  
  166.   /* Copy the request block, byte by byte: */
  167.   for( loop=0; loop < sizeof( struct IOExtPar ); loop++ )
  168.   {
  169.     /* Copy one byte: */
  170.     *w_ptr = *r_ptr; /* Write   */
  171.     *c_ptr = *r_ptr; /* Command */
  172.     
  173.     /* Step one byte foreward: */
  174.     w_ptr++;
  175.     r_ptr++;
  176.     c_ptr++;
  177.   }
  178.  
  179.  
  180.  
  181.   /* Set the parallel device's parameters: */
  182.   error = (UBYTE) SetParParams(
  183.     parallel_req_write, /* Pointer to our parallel request block.        */
  184.     PARALLEL_FLAGS,     /* Parallel flags.                               */
  185.     ADDITIONAL_FLAGS,   /* Additional flags.                             */
  186.     eof_char            /* Pointer to an array of eight end-of-file chr. */
  187.   );
  188.  
  189.   /* OK? */
  190.   if( error )
  191.     clean_up( error, "Could not set the parallel parameters!" );
  192.  
  193.  
  194.  
  195.  
  196.   /* Send 0 bytes to the parallel port and return immediately: */
  197.   /* (Since I do not know what you have connected to your      */
  198.   /* parallel port, it is best not to do anything.)            */
  199.   ParWriteNoWait( parallel_req_write, write_buffer, 0 ); 
  200.  
  201.   /* Collect 0 bytes from the parallel port and return immediately: */
  202.   ParReadNoWait( parallel_req_read, read_buffer, 0 ); 
  203.  
  204.  
  205.  
  206.   /* Do whatever you want... */
  207.  
  208.  
  209.  
  210.   /* When you are using asynchronious commands you have to be  */
  211.   /* careful with cleaning up. All requests you have started   */
  212.   /* MUST have been completed or aborted before you may close  */
  213.   /* the device. All reply massages must also be removed from  */
  214.   /* the reply port.                                           */
  215.   /*                                                           */
  216.   /* There exist several different ways on how to wait for     */
  217.   /* requests to be completed. In example 2 we used the        */
  218.   /* function CheckIO(), and Remove(). In this example will    */
  219.   /* we use the WaitIO() which puts our program to sleep and   */
  220.   /* will first wake up when the request have been completed.  */
  221.   /* WaitIO() will automatically remove the reply messages,    */
  222.   /* and if the request have already been competed it will     */
  223.   /*  immediately return.                                      */
  224.   /*                                                           */
  225.   /* The difference between a busy wait and a task sleep is    */
  226.   /* that your program can do other things while waiting if    */
  227.   /* you are using the busy wait. The task sleep should be     */
  228.   /* used if you do not want to do anything while waiting.     */
  229.   /*                                                           */
  230.   /* NOTE! Do NOT use a busy wait if you can manage with a     */
  231.   /* task sleep! Computer time should always be used with      */
  232.   /* care.                                                     */
  233.   /*                                                           */
  234.   /* We have three request blocks that has to be looked after. */
  235.   /* The "Command" block was only used by the SetParParams()   */
  236.   /* function and was used as a synchronous request. [DoIO()]  */
  237.   /* We will therefore (nor should