home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM Devices Amiga C Club */
- /* Chapter: Printer Device Tulevagen 22 */
- /* File: Example2.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-04-27 */
- /* 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 program demonstrates how you can use the Printer */
- /* Device to send (raw as well as translated) text to a */
- /* printer. However, instead of waiting for our request to */
- /* be completed as in Example 1, we use asynchronous */
- /* requests. */
-
-
-
- #include <exec/types.h> /* Data types. */
- #include <exec/errors.h> /* Exec error messages. */
- #include <devices/printer.h> /* Printer Device. */
- #include <exec/io.h> /* Standard request block. */
-
-
-
- #define LINE_LENGTH 44 /* 44 characters/line. */
- #define BUFFER_SIZE 44*40 /* 40 lines. */
-
-
-
- /* Declare how the printer request block look like: */
- union printerIO
- {
- struct IOStdReq ios;
- struct IODRPReq iodrp;
- struct IOPrtCmdReq iopc;
- };
-
- /* Declare a pointer to our reply port: */
- struct MsgPort *replymp = NULL;
-
- /* Declare a pointer our printer request block: */
- union printerIO *printer_req = NULL;
-
- /* Store the printer device error here: */
- UWORD printer_dever = TRUE;
-
- /* Declare our data buffer: */
- BYTE buffer[BUFFER_SIZE];
-
-
-
- /* Declare our functions: */
-
- /* Our main function: */
- void main();
-
- /* Clears and removes everything nice and neatly: */
- void clean_up( BYTE error, STRPTR text );
-
- /* Prints some information about the error: */
- void PrtError( BYTE error );
-
- /* Sends characters (which are translated) to the printer: */
- void PrintTextNoWait(
- union printerIO *ioreq,
- BYTE *data,
- ULONG length
- );
-
- /* Sends raw (untranslated) characters to the printer: */
- void PrintRawNoWait(
- union printerIO *ioreq,
- BYTE *data,
- ULONG length
- );
-
-
-
- void main()
- {
- int x, y;
- union printerIO *ptr;
-
-
-
- /* 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 the printer request block: */
- printer_req = (union printerIO *)
- CreateExtIO( replymp, sizeof(union printerIO) );
- if( !printer_req )
- clean_up( 0, "Not enough memory for the printer request block!" );
-
-
-
- /* Open the Printer Device: */
- printer_dever = OpenDevice( "printer.device", 0, printer_req, 0 );
- if( printer_dever )
- clean_up( 0, "Could not open the Printer Device!" );
-
-
-
- /* Fill the buffer with characters. 40 lines with the text: */
- /* "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ". */
- for( y = 0; y < BUFFER_SIZE; y += LINE_LENGTH )
- {
- /* Print 43 characters: (ASCII: 0-Z) */
- for( x = 48; x < 91; x++ )
- buffer[ y + x-48 ] = x;
-
- /* Put a line feed (ASCII 10: LF) after each line: */
- buffer[ y + 43 ] = 10;
- }
-
-
-
- /* Send some text to the printer: (Will be translated) */
- PrintTextNoWait( printer_req, buffer, BUFFER_SIZE );
-
-
- /* As long as the pointer is not pointing to */
- /* the request we should stay in the loop: */
- ptr = NULL;
- 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 = (union printerIO *) CheckIO( printer_req );
- }
-
- /* At last the request was completed! */
-
-
- /* Remove the requstblock's message. (The ptr and */
- /* printer_req are in this example identical, so it */
- /* does not matter whichever you use. The paranteces */
- /* around the expression is actually unnecessary, but */
- /* this looks better.) */
- Remove( &(ptr->ios.io_Message.mn_Node) );
-
-
- /* Check if everything is OK? */
- if( ptr->ios.io_Error )
- PrtError( ptr->ios.io_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( BYTE error, STRPTR text )
- {
- /* Print some information about the problem: */
- if( error )
- PrtError( error );
-
- /* Close the Printer Device: */
- if( !printer_dever )
- CloseDevice( printer_req );
-
- /* Deallocate the printer request block: */
- if( printer_req )
- DeleteExtIO( printer_req, sizeof(union printerIO) );
-
- /* Remove the replyport: */
- if( replymp )
- DeletePort( replymp);
-
- /* Print the message: */
- printf( "\n%s\n", text );
-
- /* Quit: */
- exit( 0 );
- }
-
-
-
- /* PrtError() tells the user what went wrong. You give it the error code */
- /* you received, and PrtError() will print a short description of the */
- /* problem. Useful when debugging. (Printer errors) */
- /* */
- /* Synopsis: PrtError( error ); */
- /* */
- /* error: (BYTE) The error value you want to have explained. */
-
- void PrtError( BYTE error )
- {
- switch( error )
- {
- /* EXEC error messages: (defined in "exec/errors.h") */
- case IOERR_OPENFAIL:
- printf( "Could not open the device!\n" );
- break;
-
- case IOERR_ABORTED:
- printf( "The request was aborted!\n" );
- break;
-
- case IOERR_NOCMD:
- printf( "Unknown Command!\n" );
- break;
-
- case IOERR_BADLENGTH:
- printf( "Bad length of the command - data!\n" );
-
-
- /* Printer Device errors: (defined in "devices/printer.h") */
- case PDERR_CANCEL:
- printf( "User cancelled the request!\n" );
- break;
-
- case PDERR_NOTGRAPHICS:
- printf( "The printer does not support graphics!\n" );
- break;
-
- case PDERR_BADDIMENSION:
- printf( "The printer dimension is not valid!\n" );
- break;
-
-
- case PDERR_INTERNALMEMORY:
- printf( "Not enough memory for the internal printer functions!\n" );
- break;
-
- case PDERR_BUFFERMEMORY:
- printf( "Not enough memory for the print buffer!\n" );
- break;
-
- default:
- printf( "An unknown error was reported! Error nr: %d\n", error );
- }
- }
-
-
-
- /* PrintTextNoWait() sends characters (which will be translated) */
- /* to the printer, but will not wait for the request to be */
- /* completed. Remember that all requests that you have started */
- /* must have been completed or aborted before your program may */
- /* terminate. (Do not forget to remove the message.) */
- /* */
- /* Since the printer device will use the Preference's settings, */
- /* it will know to which port (parallel or serial) the printer is */
- /* connected to, what type of printer it is, and what special */
- /* settings (margins, density, quality mode etc) the user have */
- /* defined. */
- /* */
- /* Note! All characters which are sent with this function may be */
- /* translated by Preferences before it is passed on to the */
- /* printer. */
- /* */
- /* Synopsis: PrintTextNoWait( io, data, length ); */
- /* */
- /* io: (struct IOStdReq *) Pointer to the printer request */
- /* block. */
- /* */
- /* data: (BYTE *) Pointer to the first character that should */
- /* be printed. */
- /* */
- /* length (ULONG) How many characters (bytes) you want to send */
- /* to the printer. */
-
- void PrintTextNoWait(
- union printerIO *ioreq, /* Pointer to the printer request block. */
- BYTE *data, /* Pointer to the data which should be printed. */
- ULONG length /* How many characters (bytes) should be printed. */
- )
- {
- /* We want to print some text: (send data to PRT:) */
- ioreq->ios.io_Command = CMD_WRITE;
-
- /* Give the start address of our data: */
- ioreq->ios.io_Data = (APTR) data;
-
- /* Set number of chracters that should be printed: */
- ioreq->ios.io_Length = length;
-
- /* Do our request and return immediately: */
- SendIO( ioreq );
- }
-
-
-
- /* PrintRawNoWait() sends raw (untranslated) characters to the */
- /* printer, but will not wait for the request to be completed. */
- /* Remember that all requests that you have started must have */
- /* been completed or aborted before your program may terminate. */
- /* (Do not forget to remove the message.) */
- /* */
- /* Note that sending untranslated characters is usually not a */
- /* very good idea. Since the characters will not be translated */
- /* by Preferences, you can not be sure that the characters you */
- /* send will be the same when printed. */
- /* */
- /* Synopsis: PrintRawNoWait( io, data, length ); */
- /* */
- /* io: (struct IOStdReq *) Pointer to the printer request */
- /* block. */
- /* */
- /* data: (BYTE *) Pointer to the first character that */
- /* should be printed. */
- /* */
- /* length (ULONG) How many characters (bytes) you want to */
- /* send to the printer. */
-
- void PrintRawNoWait(
- union printerIO *ioreq, /* Pointer to the printer request block. */
- BYTE *data, /* Pointer to the data which should be printed. */
- ULONG length /* How many characters (bytes) should be printed. */
- )
- {
- /* We want to print some raw (untranslated) text: */
- ioreq->ios.io_Command = PRD_RAWWRITE;
-
- /* Give the start address of our data: */
- ioreq->ios.io_Data = (APTR) data;
-
- /* Set number of chracters that should be printed: */
- ioreq->ios.io_Length = length;
-
- /* Do our request and return immediately: */
- SendIO( ioreq );
- }
-