home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
dev
/
cmanual-3.0.lha
/
CManual
/
Devices
/
ParallelDevice
/
Example2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-12
|
16KB
|
425 lines
/***********************************************************/
/* */
/* 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.