home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / paralleldevice / example1.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  15KB  |  401 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:    Example1.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 program demonstrates how you can use the Parallel Device. */
  23. /* It does not do very much since I do not know what you have     */
  24. /* connected to your parallel port, but with small modifications  */
  25. /* you should be able to write your own parallel communication    */
  26. /* packages.                                                      */
  27.  
  28.  
  29.  
  30. #include <exec/types.h>        /* STRPTR          */
  31. #include <devices/parallel.h>  /* Parallel Device */
  32.  
  33.  
  34.  
  35. /* Size of our data buffer: */
  36. #define MY_BUFFER_SIZE 200
  37.  
  38. /* Parallel flags: (Check for end-of-file characters.) */
  39. #define PARALLEL_FLAGS PARF_EOFMODE
  40.  
  41. /* Additional flags: (Nothing) */
  42. #define ADDITIONAL_FLAGS 0
  43.  
  44.  
  45.  
  46.  
  47.  
  48. /* Declare a pointer to our reply port: */
  49. struct MsgPort *replymp = NULL;
  50.  
  51. /* Declare a pointer to our parallel request block: */
  52. struct IOExtPar *parallel_req = NULL;
  53.  
  54. /* Store the parallel device error here: */
  55. UWORD parallel_dever = TRUE;
  56.  
  57. /* Declare our data buffer: */
  58. BYTE buffer[ MY_BUFFER_SIZE ];
  59.  
  60.  
  61.  
  62. /* Declare our functions: */
  63.  
  64. /* Our main function: */
  65. void main();
  66.  
  67. /* Clears and removes everything nice and neatly: */
  68. void clean_up( UBYTE error, STRPTR text );
  69.  
  70. UBYTE SetParParams(
  71.   struct IOExtPar *ioreq,
  72.   UBYTE parallel_flags,
  73.   ULONG extended_flags,
  74.   UBYTE *eof_chars
  75. );
  76.  
  77. void ParError( UBYTE error );
  78.  
  79. UBYTE ParWrite(
  80.   struct IOExtPar *ioreq,
  81.   BYTE *data,
  82.   ULONG length
  83. );
  84.  
  85. UBYTE ParRead(
  86.   struct IOExtPar *ioreq,
  87.   BYTE *data,
  88.   ULONG length
  89. );
  90.  
  91. void main()
  92. {
  93.   /* Error number: */
  94.   UBYTE error;
  95.   
  96.   /* The eight end-of-file characters: */
  97.   /* They MUST be in descending order! */
  98.   UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
  99.   
  100.   
  101.   
  102.   /* Get a reply port: (No name, priority 0) */
  103.   replymp = (struct MsgPort *)
  104.     CreatePort( NULL, 0 );
  105.   if( !replymp )
  106.     clean_up( 0, "Could not create the reply port!" );
  107.  
  108.  
  109.  
  110.   /* Create a parallel request block: */
  111.   parallel_req = (struct IOExtPar *)
  112.     CreateExtIO( replymp, sizeof( struct IOExtPar ) );
  113.   if( !parallel_req )
  114.     clean_up( 0, "Not enough memory for the parallel request block!" );
  115.  
  116.  
  117.  
  118.   /* Open the Parallel Device: */
  119.   parallel_dever = OpenDevice( PARALLELNAME, 0, parallel_req, 0 );
  120.   if( parallel_dever )
  121.     clean_up( 0, "Could not open the Parallel Device!" );
  122.  
  123.  
  124.  
  125.   /* Set the parallel device's parameters: */
  126.   error = (UBYTE) SetParParams(
  127.     parallel_req,     /* Pointer to our parallel request block.        */
  128.     PARALLEL_FLAGS,   /* Parallel flags.                               */
  129.     ADDITIONAL_FLAGS, /* Additional flags.                             */
  130.     eof_char          /* Pointer to an array of eight end-of-file chr. */
  131.   );
  132.  
  133.   /* OK? */
  134.   if( error )
  135.     clean_up( error, "Could not set the parallel parameters!" );
  136.  
  137.  
  138.  
  139.   /* Send 0 bytes to the parallel port: */
  140.   error = ParWrite( parallel_req, buffer, 0 ); 
  141.   if( error )
  142.     ParError( error );
  143.   else
  144.     printf( "Data has now been sent to the parallel port!\n" );
  145.  
  146.  
  147.  
  148.   /* Collect 0 bytes from the parallel port: */
  149.   error = ParRead( parallel_req, buffer, 0 ); 
  150.   if( error )
  151.     ParError( error );
  152.   else
  153.     printf( "We have now collected some data from the paralle port!\n" );
  154.  
  155.  
  156.  
  157.   /* Clean up and quit: */
  158.   clean_up( 0, "The End!" );
  159. }
  160.  
  161.  
  162.  
  163. /* Close and return everything that has been */
  164. /* opened and allocated before we quit:      */
  165.  
  166. void clean_up( UBYTE error, STRPTR text )
  167. {
  168.   /* Print some information about the problem: */
  169.   if( error )
  170.     ParError( error );
  171.  
  172.   /* Close the Parallel Device: */ 
  173.   if( !parallel_dever )
  174.     CloseDevice( parallel_req );
  175.  
  176.   /* Deallocate the parallel request block: */
  177.   if( parallel_req )
  178.     DeleteExtIO( parallel_req, sizeof( struct IOExtPar ) );
  179.  
  180.   /* Remove the replyport: */
  181.   if( replymp )
  182.     DeletePort( replymp);
  183.  
  184.   /* Print the message: */
  185.   printf( "\n%s\n", text );
  186.  
  187.   /* Quit: */
  188.   exit( 0 );
  189. }
  190.  
  191.  
  192.  
  193. /* SetParParams() sets the parallel parameters. It initializes a IOExtPar  */
  194. /* structure, and does a PDCMD_SETPARAMS commad. If everything is OK it    */
  195. /* returns NULL, else an error number is returned.                         */
  196. /*                                                                         */
  197. /* Synopsis: er = SetParParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
  198. /*                                                                         */
  199. /* er:       (UBYTE) SetParParams() returns 0 if everything was OK, else   */
  200. /*           an error value is returned. See function ParError() for more  */
  201. /*           information.                                                  */
  202. /*                                                                         */ 
  203. /* io:       (struct IOExtPar *) Pointer to the parallel request block you */
  204. /*           want to initialize.                                           */
  205. /*                                                                         */
  206. /*           PARF_RAD_BOOGIE Not supported by the parallel device for the  */
  207. /*                           moment.                                       */
  208. /*                                                                         */
  209. /*           PARF_SHARED     Set this flag if you want to allow other      */
  210. /*                           tasks running at the same time to use the     */
  211. /*                           parallel device. The default is exclusive-    */
  212. /*                           access. (If some other task is using the      */
  213. /*                           parallel device with the shared bit set, and  */
  214. /*                           you call this function with exclusive access, */
  215. /*                           your request will fail.)                      */
  216. /*                                                                         */
  217. /*           PARF_EOFMODE    Set this flag if you want to check for end of */
  218. /*                           file characters. (You may use up to eight end */
  219. /*                           of file characters, which are specified       */
  220. /*                           below.)                                       */
  221. /*                                                                         */
  222. /* ef:       (ULONG) Not supported by the parallel device for the moment.  */
  223. /*                                                                         */
  224. /* chr:      (UBYTE *) Pointer to an array containing eight end-of-file    */
  225. /*           characters. If the parallel flag "PARF_EOFMODE" is set, the   */
  226. /*           parallel device will check each character which is sent or    */
  227. /*           received, and if it matches one of the end-of-file characters */
  228. /*           the read/wite request is terminated.                          */
  229.  
  230. UBYTE SetParParams(
  231.   struct IOExtPar *ioreq, /* Pointer to our parallel request block.        */
  232.   UBYTE parallel_flags,   /* Parallel flags.                               */
  233.   ULONG extended_flags,   /* Additional parallel flags.                    */
  234.   UBYTE *eof_chars        /* Pointer to an array containing eight end-of-  */
  235.                           /* file characters.                              */
  236. )
  237. {
  238.   int loop;           /* Used in the loop.      */
  239.   UBYTE *ptr;         /* Unsigned byte pointer. */
  240.   
  241.   
  242.   /* Set parallel flags: */
  243.   ioreq->io_ParFlags = parallel_flags;
  244.  
  245.   /* Set additional flags: */
  246.   ioreq->io_PExtFlags = extended_flags;
  247.   
  248.   
  249.   /* Get the address of the IOTArray: */
  250.   ptr = (UBYTE *) &(ioreq->io_PTermArray);
  251.  
  252.   /* Set all eight end of file characters: */
  253.   for( loop=0; loop < 8; loop++ )
  254.   {
  255.     /* Copy character after character: */
  256.     *ptr = eof_chars[ loop ];
  257.  
  258.     /* Step one byte foreward: */
  259.     ptr++;
  260.   }
  261.   
  262.   /* All values have now been set, lets do a PDCMD_SETPARAMS request: */
  263.   ioreq->IOPar.io_Command = PDCMD_SETPARAMS;
  264.   
  265.   /* Do our request, and when complete return 0 if */
  266.   /* OK, else an error value:                      */
  267.   return( (UBYTE) DoIO( ioreq ) );
  268. }
  269.  
  270.  
  271.  
  272. /* ParError() tells the user what went wrong. You give it the error code */
  273. /* you received, and ParError() will print a short description of the    */
  274. /* problem. Useful when debugging.                                       */
  275. /*                                                                       */
  276. /* Synopsis: ParError( error );                                          */
  277. /*                                                                       */
  278. /* error:    (UBYTE) The error value you want to have explained.         */
  279.  
  280. void ParError( UBYTE error )
  281. {
  282.   switch( error )
  283.   {
  284.     case ParErr_DevBusy:
  285.       printf( "Some other task is already using the parallel Device!\n" );
  286.       break;
  287.     case ParErr_BufTooBig:
  288.       printf( "The parallel buffer is too big! (?)\n" );
  289.       break;
  290.     case ParErr_InvParam:
  291.       printf( "Invalid parameters!\n" );
  292.       break;
  293.     case ParErr_LineErr:
  294.       printf( "Line error, check all cables!\n" );
  295.       break;
  296.     case ParErr_NotOpen:
  297.       printf( "The Parallel Device is not open!\n" );
  298.       break;
  299.     case ParErr_PortReset:
  300.       printf( "Someone has resetted the Parallel Device!\n" );
  301.       break;
  302.     case ParErr_InitErr:
  303.       printf( "Could not initialize the Parallel Device!\n" );
  304.       break;
  305.  
  306.     default:
  307.       printf( "An unknown error was reported! Error nr: %d\n", error );
  308.   }
  309. }
  310.  
  311.  
  312.  
  313. /* ParWrite() sends some data to the Parallel Port. You only have */
  314. /* to give it a pointer to the data you want to write and tell it */
  315. /* how many bytes you want to transfer.                           */
  316. /*                                                                */
  317. /* Synopsis: error = ParWrite( io, data, length );                */
  318. /*                                                                */
  319. /* error:    (UBYTE) ParWrite() returns 0 if everything was OK,   */
  320. /*           else an error number is returned.                    */
  321. /*                                                                */
  322. /* io:       (struct IOExtPar *) Pointer to an initialized        */
  323. /*           parallel request block.                              */
  324. /*                                                                */
  325. /* data:     (BYTE *) Pointer to the first byte of the data you   */
  326. /*           want to send (write).                                */
  327. /*                                                                */
  328. /* length:   (ULONG) How many bytes you want to transfer. If you  */
  329. /*           want to continue to send data until we have received */
  330. /*           an end-of-file character, set the length to -1.      */
  331. /*           (Note that the parallel device will then ONLY stop   */
  332. /*           when it has received one of the end-of-file          */
  333. /*           characters.)                                         */
  334.  
  335. UBYTE ParWrite(
  336.   struct IOExtPar *ioreq, /* Pointer to our parallel request block.   */
  337.   BYTE *data,             /* Pointer to the data you want to send.    */
  338.   ULONG length            /* The length of the data you want to send. */
  339. )
  340. {
  341.   /* We want to send (write) some data: */
  342.   ioreq->IOPar.io_Command = CMD_WRITE;
  343.  
  344.   /* Give the start address of our data: */
  345.   ioreq->IOPar.io_Data = (APTR) data;
  346.  
  347.   /* Set the length of the message: (If you want to continue    */
  348.   /* to write until you have received an end-of-file character, */
  349.   /* set length to -1.)                                         */
  350.   ioreq->IOPar.io_Length = length;
  351.  
  352.   /* Do our request, and return 0 if everything is OK, else */
  353.   /* return an error number: (This is a task sleep.)        */
  354.   return( (UBYTE) DoIO( ioreq ) );
  355. }
  356.  
  357.  
  358.  
  359. /* ParRead() reads some data from the Parallel Port. You only have   */
  360. /* to give it a pointer to some memory where the data should be      */
  361. /* stored, and tell it how many bytes you want to read. The rest is  */
  362. /* done automatically.                                               */
  363. /*                                                                   */
  364. /* Synopsis: error = ParRead( io, data, length );                    */
  365. /*                                                                   */
  366. /* error:    (UBYTE) ParRead() returns 0 if everything was OK, else  */
  367. /*           an error number is returned.                            */
  368. /*                                                                   */
  369. /* io:       (struct IOExtPar *) Pointer to an initialized parallel  */
  370. /*           request block.                                          */
  371. /*                                                                   */
  372. /* data:     (BYTE *) Pointer to the memory buffer where you want    */
  373. /*           to store all collected data.                            */
  374. /*                                                                   */
  375. /* length:   (ULONG) How many bytes you want to read. If you want to */
  376. /*           continue to read data until we have received an end-of- */
  377. /*           file character, set the length to -1.                   */
  378.  
  379. UBYTE ParRead(
  380.   struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
  381.   BYTE *data,             /* Where the data should be placed.       */
  382.   ULONG length            /* How many bytes you want to read.       */
  383. )
  384. {
  385.   /* We want to read some data: */
  386.   ioreq->IOPar.io_Command = CMD_READ;
  387.  
  388.   /* Give the start address of our data: */
  389.   ioreq->IOPar.io_Data = (APTR) data;
  390.  
  391.   /* Set how many bytes you want to read. (If you want to continue  */
  392.   /* to read data until you have received an end-of-file character, */
  393.   /* set length to -1.)                                             */
  394.   ioreq->IOPar.io_Length = length;
  395.  
  396.   /* Do our request, and return 0 if everything is OK, else */
  397.   /* return an error number: (This is a task sleep. Zzz )   */
  398.   return( (UBYTE) DoIO( ioreq ) );
  399. }
  400.  
  401.