home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
devices
/
serialdevice
/
example4.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-27
|
34KB
|
758 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Serial Device Tulevagen 22 */
/* File: Example4.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 does not do anything, but it consists of */
/* several useful functions that you can use yourself after */
/* small modifications. The functions demonstrates all */
/* commands there exist for the serial device, so if you */
/* had problems in understanding how a command was used you */
/* can look here. */
#include <exec/types.h>
#include <exec/errors.h>
#include <devices/serial.h>
/* Declare the functions: */
UBYTE SetSerParams(
struct IOExtSer *ioreq, /* Pointer to our serial request block. */
ULONG buffer_length, /* Size of the Serial Device's own input buffer. */
ULONG baud_rate, /* Baud rate (read and write). [110 - 292000] */
ULONG break_time, /* Break time in microseconds. */
UBYTE read_length, /* Nr of bits, read (1-8). Parity not included. */
UBYTE write_length, /* Nr of bits, write (1-8). Parity not included. */
UBYTE stop_length, /* Nr of bits, stop (1 or 2). */
UBYTE serial_flags, /* Serial flags. */
ULONG extended_flags, /* Additional serial flags. */
UBYTE *eof_chars /* Pointer to an array containing eight end-of- */
/* file characters. */
);
void SerError( UBYTE error );
UBYTE SerWrite(
struct IOExtSer *ioreq, /* Pointer to our serial request block. */
BYTE *data, /* Pointer to the data you want to send. */
ULONG length /* The length of the data you want to send. */
);
UBYTE SerRead(
struct IOExtSer *ioreq, /* Pointer to our serial request block. */
BYTE *data, /* Where the data should be placed. */
ULONG length /* How many bytes you want to read. */
);
void SerWriteNoWait(
struct IOExtSer *ioreq, /* Pointer to our serial request block. */
BYTE *data, /* Pointer to the data you want to send. */
ULONG length /* The length of the data you want to send. */
);
void SerReadNoWait(
struct IOExtSer *ioreq, /* Pointer to our serial request block. */
BYTE *data, /* Where the data should be placed. */
ULONG length /* How many bytes you want to read. */
);
UBYTE SerBreak( struct IOExtSer *ioreq );
UBYTE SerClear( struct IOExtSer *ioreq );
UBYTE SerFlush( struct IOExtSer *ioreq );
UBYTE SerQuery( struct IOExtSer *ioreq );
UBYTE SerReset( struct IOExtSer *ioreq );
UBYTE SerStop( struct IOExtSer *ioreq );
UBYTE SerStart( struct IOExtSer *ioreq );
/* Very short main() module: */
void main();
void main()
{
printf( "See source code for more information..." );
}
/*************************************/
/* SERIAL DEVICE - SUPPORT FUNCTIONS */
/*************************************/
/* SetSerParams() sets the serial parameters. It initializes a IOExtSer */
/* structure, and does a SDCMD_SETPARAMS commad. If everything is OK it */
/* returns NULL, else an error number is returned. */
/* */
/* Synopsis: er = SetSerParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
/* */
/* er: (UBYTE) SetSerParams() returns 0 if everything was OK, else */
/* an error value is returned. See function SerError() for more */
/* information. */
/* */
/* io: (struct IOExtSer *) Pointer to the serial request block you */
/* want to initialize. */
/* */
/* bl: (ULONG) Size of the internal serial buffer which will be used */
/* when you read data. Must be at least 512 (bytes), but more is */
/* recommended. The faster and more data you want to read, the */
/* bigger should the internal buffer be. Some recommended sizes: */
/* 512, 1024, 2048, 4096, 8192 or 16384. */
/* */
/* br: (ULONG) Baud rate. Can be anything between 110 and 292000. */
/* (Up to 292000 is all right for the hardware, but the software */
/* can not cope with this, especially since other tasks may be */
/* running at the same time. You should therefore not use baud */
/* rates above 31250.) Some recommended values: 110, 300, 1200, */
/* 2400, 4800, 9600, 19200 or 31250 (the last is a bit though). */
/* */
/* bt: (ULONG) Break time in micro seconds. All break requests will */
/* be set to this time. */
/* */
/* rl: (UBYTE) How many bits chould be read for each character. */
/* Usually 7 or 8 bits. */
/* */
/* wl: (UBYTE) How many bits chould be written for each character. */
/* Usually 7 or 8 bits. */
/* */
/* sl: (UBYTE) How many stop bits shoud be written or expected. */
/* Normally set to 1, but you may set it to 2 if rl/wl = 7. */
/* */
/* sf: (UBYTE) You may use the following serial flags: */
/* */
/* SERF_PARTY_ON Parity checking/writing is turned on. (The */
/* sum of all data bits are divided by two, and */
/* the remainder is the parity bit. If even */
/* parity is used the bit will be set to 1 if */
/* the remainder is even. If odd parity is used */
/* the parity bit will be set to 0 if the */
/* remainder is even. */
/* */
/* SERF_PARTY_ODD Set this flag if you want to use odd parity. */
/* (The default setting is even parity.) */
/* */
/* SERF_7WIRE This flag should only be used when you call */
/* the OpenDevice(), and not by this function. */
/* If the flag is set, seven-wire "handshaking" */
/* will be used. (Default is three-wire.) */
/* */
/* SERF_QUEUEDBRK Set this flag if you want break commands to */
/* be queued along with all other signals. The */
/* default is that a break command interrupts */
/* the process immediately. */
/* */
/* SERF_RAD_BOOGIE Set this bit if you want high speed mode. */
/* This can be useful when you want to send and */
/* receive signals at high speed. When this flag */
/* is set no parity is used, xON/xOFF handling */
/* is turned off, no break signals are allowed, */
/* and finally only eight-bit characters are */
/* used. */
/* */
/* SERF_SHARED Set this falg if you want to allow other */
/* tasks running at the same time to use the */
/* serial device. The default is exclusive- */
/* access. (If some other task is using the */
/* serial device with the shared bit set, and */
/* you call this function with exclusive access, */
/* your request will fail.) */
/* */
/* SERF_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.) */
/* */
/* SERF_XDISABLED xOn/xOFF handling is turned off. (Default is */
/* on.) */
/* */
/* ef: (ULONG) You may use the following extra flags: */
/* */
/* SEXTF_MSPON Set this flag if you want to use mark-space */
/* parity rather than odd-even parity. */
/* */
/* SEXTF_MARK If this and the SEXTF_MSPON flag is set, it */
/* will mark. */
/* */
/* chr: (UBYTE *) Pointer to an array containing eight end of file */
/* characters. If the serial flag "SERF_EOFMODE" is set, the */
/* serial 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 SetSerParams(
struct IOExtSer *ioreq, /* Pointer to our serial request block. */
ULONG buffer_length, /* Size of the Serial Device's own input buffer. */
ULONG baud_rate, /* Baud rate (read and write). [110 - 292000] */
ULONG break_time, /* Break time in microseconds. */
UBYTE read_length, /* Nr of bits, read (1-8). Parity not included. */
UBYTE write_length, /* Nr of bits, write (1-8). Parity not included. */
UBYTE stop_length, /* Nr of bits, stop (1 or 2). */
UBYTE serial_flags, /* Serial flags. */
ULONG extended_flags, /* Additional serial 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 the size of the Serial Device's own input buffer: */
ioreq->io_RBufLen = buffer_length;
/* Set baud rate: */
ioreq->io_Baud = baud_rate;
/* Set break time (in microseconds): */
ioreq->io_BrkTime = break_time;
/* Nr of bits to read per character: */
ioreq->io_ReadLen = read_length;
/* Nr of bits to write per character: */
ioreq->io_WriteLen = write_length;
/* Nr of stop bits: (Normally 1, if write_length is */
/* equal to 7 you may set stop_length to 2.) */
ioreq->io_StopBits = stop_length;
/* Set serial flags: */
ioreq->io_SerFlags = serial_flags;
/* Set additional flags: */
ioreq->io_ExtFlags = extended_flags;
/* Get the address of the IOTArray: */
ptr = (UBYTE *) &(ioreq->io_TermArray);
/* 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 SDCMD_SETPARAMS request: */
ioreq->IOSer.io_Command = SDCMD_SETPARAMS;
/* Do our request, and when complete return 0 if */
/* OK, else an error value: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerError() tells the user what went wrong. You give it the error code */
/* you received, and SerError() will print a short description of the */
/* problem. Useful when debugging. */
/* */
/* Synopsis: SerError( error ); */
/* */
/* error: (UBYTE) The error value you want to have explained. */
void SerError( UBYTE error )
{
switch( error )
{
/* The serial device: */
case SerErr_DevBusy:
printf( "Some other task is already using the Serial Device!\n" );
break;
case SerErr_BufErr:
printf( "Not enough memory for the new input buffer!\n" );
break;
case SerErr_InvParam:
printf( "Invalid parameters!\n" );
break;
case SerErr_LineErr:
printf( "Line error!\n" );
break;
case SerErr_ParityErr:
printf( "Problems with the parity!\n" );
break;
case SerErr_TimerErr:
printf( "Timer error!\n" );
break;
case SerErr_BufOverflow:
printf( "Buffer overflowed!\n" );
break;
case SerErr_NoDSR:
printf( "No DSR!\n" );
break;
case SerErr_DetectedBreak:
printf( "A break was detected!\n" );
break;
/* Exec: */
case IOERR_OPENFAIL:
printf( "The device could not be opened!\n" );
break;
case IOERR_ABORTED:
printf( "The request was aborted!\n" );
break;
case IOERR_NOCMD:
printf( "The serial device does not know about this command!\n" );
break;
case IOERR_BADLENGTH:
printf( "The length of the request was not valid!\n" );
break;
/* Unknown error: */
default:
printf( "Unknown error! Error code: %d\n", error );
}
}
/* SerWrite() sends some data to the Serial Port. 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. */
/* */
/* Synopsis: error = SerWrite( io, data, length ); */
/* */
/* error: (UBYTE) SerWrite() returns 0 if everything was OK, */
/* else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized serial */
/* 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 it will then ONLY stop when it receives one of */
/* the end-of-file characters.) */
UBYTE SerWrite(
struct IOExtSer *ioreq, /* Pointer to our serial 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->IOSer.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->IOSer.io_Data = (APTR) data;
/* Set the length of the message: (If you want to continue */
/* to write until you have received an end-of-file character, */
/* set length to -1.) */
ioreq->IOSer.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerRead() reads some data from the Serial Port. 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. The rest is done */
/* automatically. */
/* */
/* Synopsis: error = SerRead( io, data, length ); */
/* */
/* error: (UBYTE) SerRead() returns 0 if everything was OK, eles */
/* an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized serial */
/* 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 it */
/* will then ONLY stop when it receives one of the end-of- */
/* file characters.) */
UBYTE SerRead(
struct IOExtSer *ioreq, /* Pointer to our serial 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->IOSer.io_Command = CMD_READ;
/* Give the start address of our data: */
ioreq->IOSer.io_Data = (APTR) data;
/* Set how many bytes you want to read. (If you want to continue */
/* to read data until you have received an end-of-file character, */
/* set length to -1.) */
ioreq->IOSer.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerWriteNoWait() sends some data to the Serial 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 serial device. */
/* */
/* Synopsis: error = SerWriteNoWait( io, data, length ); */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized serial */
/* 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 it will then ONLY stop when it receives one of */
/* the end-of-file characters.) */
void SerWriteNoWait(
struct IOExtSer *ioreq, /* Pointer to our serial 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->IOSer.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->IOSer.io_Data = (APTR) data;
/* Set the length of the message: */
ioreq->IOSer.io_Length = length;
/* Do our request and return immediately: */
SendIO( ioreq );
}
/* SerReadNoWait() reads some data from the Serial 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 serial device. */
/* */
/* */
/* Synopsis: error = SerReadNoWait( io, data, length ); */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized serial */
/* 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 it */
/* will then ONLY stop when it receives one of the end-of- */
/* file characters.) */
void SerReadNoWait(
struct IOExtSer *ioreq, /* Pointer to our serial 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->IOSer.io_Command = CMD_READ;
/* Give the start address of our data: */
ioreq->IOSer.io_Data = (APTR) data;
/* Set how many bytes you want to read: */
ioreq->IOSer.io_Length = length;
/* Do our request and return immediately: */
DoIO( ioreq );
}
/* SerBreak() will send a break signal to the serial device. */
/* The default is that all serial requests are immediately */
/* halted, but if the serial flag "SERF_QUEUEDBRK" is set, the */
/* break command will be queued as all other requests and the */
/* device will first take a break when all previous requests */
/* have been completed. */
/* */
/* The serial device will normally take a 250000 microseconds */
/* (1/4 seconds) long break, but you may change this break */
/* time by altering the serial device's "io_BrkTime" parameter */
/* (see function SetSerParams()). */
/* */
/* Synopsis: error = SerBreak( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized */
/* serial request block. */
UBYTE SerBreak( struct IOExtSer *ioreq )
{
/* We want to take a puse: */
ioreq->IOSer.io_Command = SDCMD_BREAK;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerClear() will clear the internal input buffer. */
/* */
/* Synopsis: error = SerClear( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized */
/* serial request block. */
UBYTE SerClear( struct IOExtSer *ioreq )
{
/* We want to clear the input buffer: */
ioreq->IOSer.io_Command = CMD_CLEAR;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerFlush() will remove all queued commands. */
/* */
/* Synopsis: error = SerFlush( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized */
/* serial request block. */
UBYTE SerFlush( struct IOExtSer *ioreq )
{
/* We want to remove all queued requests: */
ioreq->IOSer.io_Command = CMD_FLUSH;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerQuery() will print some information about the serial */
/* device. This is very useful for debugging. */
/* */
/* Synopsis: error = SerQuery( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized */
/* serial request block. */
UBYTE SerQuery( struct IOExtSer *ioreq )
{
UBYTE error;
/* We want to check the serial device: */
ioreq->IOSer.io_Command = SDCMD_QUERY;
/* Do our request: */
error = DoIO( ioreq );
/* Everything OK? */
if( !error )
{
/* Check the "io_Status" field: */
printf( "Bit Description Status\n" );
printf( "----------------------------------------\n" );
printf( " 0 Reserved: %s\n",
ioreq->io_Status & 0x0001 ? "On" : "Off" );
printf( " 1 Reserved: %s\n",
ioreq->io_Status & 0x0002 ? "On" : "Off" );
printf( " 2 Connected to parallel select: %s\n",
ioreq->io_Status & 0x0004 ? "On" : "Off" );
printf( " 3 Data Set Ready: %s\n",
ioreq->io_Status & 0x0008 ? "On" : "Off" );
printf( " 4 Clear To Send: %s\n",
ioreq->io_Status & 0x0010 ? "On" : "Off" );
printf( " 5 Carrier Detect: %s\n",
ioreq->io_Status & 0x0020 ? "On" : "Off" );
printf( " 6 Ready To Send: %s\n",
ioreq->io_Status & 0x0040 ? "On" : "Off" );
printf( " 7 Data Terminal Ready: %s\n",
ioreq->io_Status & 0x0080 ? "On" : "Off" );
printf( " 8 Read overrun: %s\n",
ioreq->io_Status & 0x0100 ? "On" : "Off" );
printf( " 9 Break sent: %s\n",
ioreq->io_Status & 0x0200 ? "On" : "Off" );
printf( " 10 Break received: %s\n",
ioreq->io_Status & 0x0400 ? "On" : "Off" );
printf( " 11 Transmit X-OFF: %s\n",
ioreq->io_Status & 0x0800 ? "On" : "Off" );
printf( " 12 Receive X-OFF %s\n",
ioreq->io_Status & 0x1000 ? "On" : "Off" );
printf( " 13 Reserved: %s\n",
ioreq->io_Status & 0x2000 ? "On" : "Off" );
printf( " 14 Reserved: %s\n",
ioreq->io_Status & 0x4000 ? "On" : "Off" );
printf( " 15 Reserved: %s\n",
ioreq->io_Status & 0x8000 ? "On" : "Off" );
printf( "----------------------------------------\n" );
/* Check number of characters left in the input buffer: */
printf( "Characters left: %ld\n", ioreq->IOSer.io_Actual );
printf( "----------------------------------------\n" );
}
else
printf( "Could not get any information from the device!\n" );
return( error );
}
/* SerReset() will reset the serial device. All commands that */
/* are qued to the device will be removed, commands that are */
/* currently executed will be aborted, the internal input */
/* buffer will be cleared and reallocated to the default size */
/* and finally all serial flags are resetted. */
/* */
/* Synopsis: error = SerReset( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized */
/* serial request block. */
UBYTE SerReset( struct IOExtSer *ioreq )
{
/* We want to reset the serial device: */
ioreq->IOSer.io_Command = CMD_RESET;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerStop() will temporary stop the serial communication. */
/* */
/* Synopsis: error = SerStop( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized */
/* serial request block. */
UBYTE SerStop( struct IOExtSer *ioreq )
{
/* We want to start serial communication again: */
ioreq->IOSer.io_Command = CMD_STOP;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}
/* SerStart() will restart the serial communication, which has */
/* previously been halted by a SerStop() call. */
/* */
/* Synopsis: error = SerStart( io ); */
/* */
/* error: (UBYTE) The function returns 0 if everything was */
/* OK, else an error number is returned. */
/* */
/* io: (struct IOExtSer *) Pointer to an initialized */
/* serial request block. */
UBYTE SerStart( struct IOExtSer *ioreq )
{
/* We want to start serial communication again: */
ioreq->IOSer.io_Command = CMD_START;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: */
return( (UBYTE) DoIO( ioreq ) );
}