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 >
Wrap
C/C++ Source or Header
|
1993-10-12
|
20KB
|
504 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Parallel Device Tulevagen 22 */
/* File: Example3.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. */
/* We are also trying to read and write at the same time. To be */
/* able to do several requests simultaneously we need one request */
/* block for each command. In this example we use three separate */
/* request blocks. */
#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 pointers to our parallel request blocks: */
/* One is used for reading, the other for writing, and */
/* finally the last one is used for other commands. */
struct IOExtPar *parallel_req_read = NULL; /* Read */
struct IOExtPar *parallel_req_write = NULL; /* Write */
struct IOExtPar *parallel_req_command = NULL; /* Command */
/* Store the parallel device error here: */
UWORD parallel_dever = TRUE;
/* Declare two data buffers. The first one will contain the */
/* information we want to send, and the other will be filled */
/* with all data we have collected: */
BYTE read_buffer[ MY_BUFFER_SIZE ];
BYTE write_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;
/* Byte pointers, used to copy the request blocks: */
BYTE *r_ptr;
BYTE *w_ptr;
BYTE *c_ptr;
/* Loop variable: */
int loop;
/* The eight end-of-file characters: */
/* They MUST be in descending order! */
UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
/* 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 request block "Read": */
parallel_req_read = (struct IOExtPar *)
CreateExtIO( replymp, sizeof( struct IOExtPar ) );
if( !parallel_req_read )
clean_up( 0, "Not enough memory for the request block Read!" );
/* Create request block "Write": */
parallel_req_write = (struct IOExtPar *)
CreateExtIO( replymp, sizeof( struct IOExtPar ) );
if( !parallel_req_write )
clean_up( 0, "Not enough memory for the request block Write!" );
/* Create request block "Command": */
parallel_req_command = (struct IOExtPar *)
CreateExtIO( replymp, sizeof( struct IOExtPar ) );
if( !parallel_req_command )
clean_up( 0, "Not enough memory for the request block Command!" );
/* Open the parallel device for the read request: */
parallel_dever = OpenDevice( PARALLELNAME, 0, parallel_req_read, 0 );
if( parallel_dever )
clean_up( parallel_dever, "Could not open the Parallel Device!" );
/* Since we can not open the parallel device any more */
/* for the other request (we use exclusive access), we */
/* have to copy the whole read request block into the */
/* write and command request blocks. */
/* Get the start address of all request blocks: */
r_ptr = (BYTE *) parallel_req_read;
w_ptr = (BYTE *) parallel_req_write;
c_ptr = (BYTE *) parallel_req_command;
/* Copy the request block, byte by byte: */
for( loop=0; loop < sizeof( struct IOExtPar ); loop++ )
{
/* Copy one byte: */
*w_ptr = *r_ptr; /* Write */
*c_ptr = *r_ptr; /* Command */
/* Step one byte foreward: */
w_ptr++;
r_ptr++;
c_ptr++;
}
/* Set the parallel device's parameters: */
error = (UBYTE) SetParParams(
parallel_req_write, /* 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: */
/* (Since I do not know what you have connected to your */
/* parallel port, it is best not to do anything.) */
ParWriteNoWait( parallel_req_write, write_buffer, 0 );
/* Collect 0 bytes from the parallel port and return immediately: */
ParReadNoWait( parallel_req_read, read_buffer, 0 );
/* Do whatever you want... */
/* When you are using asynchronious commands you have to be */
/* careful with cleaning up. All requests you have started */
/* MUST have been completed or aborted before you may close */
/* the device. All reply massages must also be removed from */
/* the reply port. */
/* */
/* There exist several different ways on how to wait for */
/* requests to be completed. In example 2 we used the */
/* function CheckIO(), and Remove(). In this example will */
/* we use the WaitIO() which puts our program to sleep and */
/* will first wake up when the request have been completed. */
/* WaitIO() will automatically remove the reply messages, */
/* and if the request have already been competed it will */
/* immediately return. */
/* */
/* The difference between a busy wait and a task sleep is */
/* that your program can do other things while waiting if */
/* you are using the busy wait. The task sleep should be */
/* used if you do not want to do anything while waiting. */
/* */
/* NOTE! Do NOT use a busy wait if you can manage with a */
/* task sleep! Computer time should always be used with */
/* care. */
/* */
/* We have three request blocks that has to be looked after. */
/* The "Command" block was only used by the SetParParams() */
/* function and was used as a synchronous request. [DoIO()] */
/* We will therefore (nor should