home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM Devices Amiga C Club */
- /* Chapter: Parallel Device Tulevagen 22 */
- /* File: Example2.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-04-26 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
-
-
- /* This example is rather similar to Example 1, but this time */
- /* we do not wait for the parallel port to complete our request. */
- /* Instead we do somethings (well not very much) and now and */
- /* then checks if the request has been completed. */
-
-
-
- #include <exec/types.h> /* STRPTR */
- #include <devices/parallel.h> /* Parallel Device */
-
-
-
- /* Size of our data buffer: */
- #define MY_BUFFER_SIZE 200
-
- /* Parallel flags: (Check for end-of-file characters.) */
- #define PARALLEL_FLAGS PARF_EOFMODE
-
- /* Additional flags: (Nothing) */
- #define ADDITIONAL_FLAGS 0
-
-
-
- /* Declare a pointer to our reply port: */
- struct MsgPort *replymp = NULL;
-
- /* Declare a pointer to our parallel request block: */
- struct IOExtPar *parallel_req = NULL;
-
- /* Store the parallel device error here: */
- UWORD parallel_dever = TRUE;
-
- /* Declare our data buffer: */
- BYTE buffer[ MY_BUFFER_SIZE ];
-
-
-
- /* Declare our functions: */
-
- /* Our main function: */
- void main();
-
- /* Clears and removes everything nice and neatly: */
- void clean_up( UBYTE error, STRPTR text );
-
- /* Sets the parallel parameters: */
- UBYTE SetParParams(
- struct IOExtPar *ioreq,
- UBYTE parallel_flags,
- ULONG extended_flags,
- UBYTE *eof_chars
- );
-
- /* Explains error messages: */
- void ParError( UBYTE error );
-
- /* Sends data to the parallel device without waiting: */
- void ParWriteNoWait(
- struct IOExtPar *ioreq,
- BYTE *data,
- ULONG length
- );
-
- /* Collects data from the parallel device without waiting: */
- void ParReadNoWait(
- struct IOExtPar *ioreq,
- BYTE *data,
- ULONG length
- );
-
-
-
- void main()
- {
- /* Error number: */
- UBYTE error;
-
- /* The eight end-of-file characters: */
- /* They MUST be in descending order! */
- UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
-
- /* Declare a pointer and set it to NULL: */
- struct IOExtPar *ptr = NULL;
-
-
-
- /* Get a reply port: (No name, priority 0) */
- replymp = (struct MsgPort *)
- CreatePort( NULL, 0 );
- if( !replymp )
- clean_up( 0, "Could not create the reply port!" );
-
-
-
- /* Create a parallel request block: */
- parallel_req = (struct IOExtPar *)
- CreateExtIO( replymp, sizeof( struct IOExtPar ) );
- if( !parallel_req )
- clean_up( 0, "Not enough memory for the parallel request block!" );
-
-
-
- /* Open the Parallel Device: */
- parallel_dever = OpenDevice( PARALLELNAME, 0, parallel_req, 0 );
- if( parallel_dever )
- clean_up( parallel_dever, "Could not open the Parallel Device!" );
-
-
-
- /* Set the parallel device's parameters: */
- error = (UBYTE) SetParParams(
- parallel_req, /* Pointer to our parallel request block. */
- PARALLEL_FLAGS, /* Parallel flags. */
- ADDITIONAL_FLAGS, /* Additional flags. */
- eof_char /* Pointer to an array of eight end-of-file chr. */
- );
-
- /* OK? */
- if( error )
- clean_up( error, "Could not set the parallel parameters!" );
-
-
-
- /* Send 0 bytes to the parallel port and return immediately: */
- ParWriteNoWait( parallel_req, buffer, 0 );
-
-
- /* As long as the pointer is not pointing to */
- /* the request we should stay in the loop: */
- while( ptr == NULL )
- {
- /* ... do something ... */
- /* Well, I do not know what. */
-
- /* Check if the request has been completed: (If the */
- /* request has been compleded CheckIO() will return */
- /* a pointer to the request, else NULL is returned.) */
- ptr = (struct IOExtPar *) CheckIO( parallel_req );
- }
-
- /* At last the request was completed! */
-
-
- /* Remove the requstblock's message. (The ptr and ioreq */
- /* are in this example identical, so it does not matter */
- /* whichever you will use. The paranteces around the */
- /* expression is actually unnecessary, but this looks */
- /* better.) */
- Remove( &(ptr->IOPar.io_Message.mn_Node) );
-
-
- /* Check if everything is OK? */
- if( parallel_req->IOPar.io_Error )
- ParError( error );
- else
- printf( "Data sent without any problems!\n" );
-
-
-
- /* Clean up and quit: */
- clean_up( 0, "The End!" );
- }
-
-
-
- /* Close and return everything that has been */
- /* opened and allocated before we quit: */
-
- void clean_up( UBYTE error, STRPTR text )
- {
- /* Print some information about the problem: */
- if( error )
- ParError( error );
-
- /* Close the Parallel Device: */
- if( !parallel_dever )
- CloseDevice( parallel_req );
-
- /* Deallocate the parallel request block: */
- if( parallel_req )
- DeleteExtIO( parallel_req, sizeof( struct IOExtPar ) );
-
- /* Remove the replyport: */
- if( replymp )
- DeletePort( replymp);
-
-
- /* Print the message: */
- printf( "%s\n", text );
-
- /* Quit: */
- exit( 0 );
- }
-
-
-
- /* SetParParams() sets the parallel parameters. It initializes a IOExtPar */
- /* structure, and does a PDCMD_SETPARAMS commad. If everything is OK it */
- /* returns NULL, else an error number is returned. */
- /* */
- /* Synopsis: er = SetParParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
- /* */
- /* er: (UBYTE) SetParParams() returns 0 if everything was OK, else */
- /* an error value is returned. See function ParError() for more */
- /* information. */
- /* */
- /* io: (struct IOExtPar *) Pointer to the parallel request block you */
- /* want to initialize. */
- /* */
- /* PARF_RAD_BOOGIE Not supported by the parallel device for the */
- /* moment. */
- /* */
- /* PARF_SHARED Set this flag if you want to allow other */
- /* tasks running at the same time to use the */
- /* parallel device. The default is exclusive- */
- /* access. (If some other task is using the */
- /* parallel device with the shared bit set, and */
- /* you call this function with exclusive access, */
- /* your request will fail.) */
- /* */
- /* PARF_EOFMODE Set this flag if you want to check for end of */
- /* file characters. (You may use up to eight end */
- /* of file characters, which are specified */
- /* below.) */
- /* */
- /* ef: (ULONG) Not supported by the parallel device for the moment. */
- /* */
- /* chr: (UBYTE *) Pointer to an array containing eight end-of-file */
- /* characters. If the parallel flag "PARF_EOFMODE" is set, the */
- /* parallel device will check each character which is sent or */
- /* received, and if it matches one of the end-of-file characters */
- /* the read/wite request is terminated. */
-
- UBYTE SetParParams(
- struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
- UBYTE parallel_flags, /* Parallel flags. */
- ULONG extended_flags, /* Additional parallel flags. */
- UBYTE *eof_chars /* Pointer to an array containing eight end-of- */
- /* file characters. */
- )
- {
- int loop; /* Used in the loop. */
- UBYTE *ptr; /* Unsigned byte pointer. */
-
-
- /* Set parallel flags: */
- ioreq->io_ParFlags = parallel_flags;
-
- /* Set additional flags: */
- ioreq->io_PExtFlags = extended_flags;
-
-
- /* Get the address of the IOTArray: */
- ptr = (UBYTE *) &(ioreq->io_PTermArray);
-
- /* Set all eight end of file characters: */
- for( loop=0; loop < 8; loop++ )
- {
- /* Copy character after character: */
- *ptr = eof_chars[ loop ];
-
- /* Step one byte foreward: */
- ptr++;
- }
-
- /* All values have now been set, lets do a PDCMD_SETPARAMS request: */
- ioreq->IOPar.io_Command = PDCMD_SETPARAMS;
-
- /* Do our request, and when complete return 0 if */
- /* OK, else an error value: */
- return( (UBYTE) DoIO( ioreq ) );
- }
-
-
-
- /* ParError() tells the user what went wrong. You give it the error code */
- /* you received, and ParError() will print a short description of the */
- /* problem. Useful when debugging. */
- /* */
- /* Synopsis: ParError( error ); */
- /* */
- /* error: (UBYTE) The error value you want to have explained. */
-
- void ParError( UBYTE error )
- {
- switch( error )
- {
- case ParErr_DevBusy:
- printf( "Some other task is already using the parallel Device!\n" );
- break;
- case ParErr_BufTooBig:
- printf( "The parallel buffer is too big! (?)\n" );
- break;
- case ParErr_InvParam:
- printf( "Invalid parameters!\n" );
- break;
- case ParErr_LineErr:
- printf( "Line error, check all cables!\n" );
- break;
- case ParErr_NotOpen:
- printf( "The Parallel Device is not open!\n" );
- break;
- case ParErr_PortReset:
- printf( "Someone has resetted the Parallel Device!\n" );
- break;
- case ParErr_InitErr:
- printf( "Could not initialize the Parallel Device!\n" );
- break;
-
- default:
- printf( "An unknown error was reported! Error nr: %d\n", error );
- }
- }
-
-
-
-
-
- /* ParWriteNoWait() sends some data to the Parallel Port, but returns */
- /* immediately. You only have to give it a pointer to the data you */
- /* want to write and tell it how many bytes you want to transfer. */
- /* Since it does not wait for the request to be completed, you have */
- /* to take care of removing the message yourself. Note that all */
- /* requests that have been started must be completed or aborted */
- /* before your program may close the parallel device. */
- /* */
- /* Synopsis: error = ParWriteNoWait( io, data, length ); */
- /* */
- /* io: (struct IOExtPar *) Pointer to an initialized parallel */
- /* request block. */
- /* */
- /* data: (BYTE *) Pointer to the first byte of the data you want */
- /* to send (write). */
- /* */
- /* length: (ULONG) How many bytes you want to transfer. If you want */
- /* to continue to send data until we have received an end- */
- /* of-file character, set the length to -1. (Note that the */
- /* parallel device will then ONLY stop when it receives one */
- /* of the specified end-of-file characters.) */
-
- void ParWriteNoWait(
- struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
- BYTE *data, /* Pointer to the data you want to send. */
- ULONG length /* The length of the data you want to send. */
- )
- {
- /* We want to send (write) some data: */
- ioreq->IOPar.io_Command = CMD_WRITE;
-
- /* Give the start address of our data: */
- ioreq->IOPar.io_Data = (APTR) data;
-
- /* Set the length of the message: */
- ioreq->IOPar.io_Length = length;
-
- /* Do our request and return immediately: */
- SendIO( ioreq );
- }
-
-
-
- /* ParReadNoWait() reads some data from the Parallel Port, but returns */
- /* immediately. You only have to give it a pointer to some memory */
- /* where the data should be stored, and tell it how many bytes you */
- /* want to read. Since it does not wait for the request to be */
- /* completed, you have to take care of removing the message yourself. */
- /* Note that all requests that have been started must be completed or */
- /* aborted before your program may close the parallel device. */
- /* */
- /* */
- /* Synopsis: error = ParReadNoWait( io, data, length ); */
- /* */
- /* io: (struct IOExtPar *) Pointer to an initialized parallel */
- /* request block. */
- /* */
- /* data: (BYTE *) Pointer to the memory buffer where you want to */
- /* store all data. */
- /* */
- /* length: (ULONG) How many bytes you want to read. If you want to */
- /* continue to send data until we have received an end-of- */
- /* file character, set the length to -1. (Note that the */
- /* parallel device will then ONLY stop when it receives one */
- /* of the end-of-file characters.) */
-
- void ParReadNoWait(
- struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
- BYTE *data, /* Where the data should be placed. */
- ULONG length /* How many bytes you want to read. */
- )
- {
- /* We want to read some data: */
- ioreq->IOPar.io_Command = CMD_READ;
-
- /* Give the start address of our data: */
- ioreq->IOPar.io_Data = (APTR) data;
-
- /* Set how many bytes you want to read: */
- ioreq->IOPar.io_Length = length;
-
- /* Do our request and return immediately: */
- DoIO( ioreq );
- }
-
-