home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
dev
/
cmanual-3.0.lha
/
CManual
/
Devices
/
SerialDevice
/
Example2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-12
|
23KB
|
561 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Serial 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 program demonstrates how you can use the Serial Device */
/* to read and write information from and to the Serial Port. */
/* Since I do not know what you have connected to your serial */
/* port, this program does not do very much. However, the */
/* functions are easy to call, so it should not be hard for */
/* you to change this program into a nice serial communication */
/* package. */
/* */
/* This example is rather similar to Example 1, but this time */
/* we do not wait for the serial 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/serial.h> /* Serial Device */
/* Size of our data buffer: */
#define MY_BUFFER_SIZE 200
/* Size of the Serial Device's own input buffer: (Must be */
/* at least 512 bytes, but more is recommended.) */
#define INPUT_BUFFER_SIZE 1024
/* Read 8 bits/character: */
#define READ_LENGTH 8
/* Write 8 bits/character: */
#define WRITE_LENGTH 8
/* Sen 1 stop bit between each character: */
#define STOP_LENGTH 1
/* Break time in microseconds: (1/2 sec) */
#define BREAK_TIME 500000
/* Baud rate: */
#define BAUD_RATE 9600
/* Serial flags: (Parity enabled, checking for end of file characters.) */
#define SERIAL_FLAGS SERF_PARTY_ON|SERF_EOFMODE
/* Additional flags: (Mark-space parity not used.) */
#define ADDITIONAL_FLAGS 0
/* Declare a pointer to our reply port: */
struct MsgPort *replymp = NULL;
/* Declare a pointer to our serial request block: */
struct IOExtSer *serial_req = NULL;
/* Store the serial device error here: */
UWORD serial_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 serial parameters: */
UBYTE SetSerParams(
struct IOExtSer *ioreq,
ULONG buffer_length,
ULONG baud_rate,
ULONG break_time,
UBYTE read_length,
UBYTE write_length,
UBYTE stop_length,
UBYTE serial_flags,
ULONG extended_flags,
UBYTE *eof_chars
);
/* Prints some information about the error: */
void SerError( UBYTE error );
/* Sends data to the Serial Port without going to sleep: */
void SerWriteNoWait(
struct IOExtSer *ioreq,
BYTE *data,
ULONG length
);
/* Reads data from the Serial Port without going to sleep: */
void SerReadNoWait(
struct IOExtSer *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 IOExtSer *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 serial request block: */
serial_req = (struct IOExtSer *)
CreateExtIO( replymp, sizeof( struct IOExtSer ) );
if( !serial_req )
clean_up( 0, "Not enough memory for the serial request block!" );
/* Open the Serial Device: */
serial_dever = OpenDevice( SERIALNAME, 0, serial_req, 0 );
if( serial_dever )
clean_up( 0, "Could not open the Serial Device!" );
error = (UBYTE) SetSerParams(
serial_req, /* Pointer to our serial request block. */
INPUT_BUFFER_SIZE, /* Size of the Serial Device's own input buffer. */
BAUD_RATE, /* Baud rate (read and write). [112 - 292000] */
BREAK_TIME, /* Break time in microseconds. */
READ_LENGTH, /* # of bits/char (1-8). Parity not included. */
WRITE_LENGTH, /* # of bits/char (1-8). Parity not included. */
STOP_LENGTH, /* # of stop bits between the characters. (1-2) */
SERIAL_FLAGS, /* Serial 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 serial parameters!" );
/* These two bytes will turn on the lamp on a Sharp JX-100 */
/* color scanner, if it happens to be connected to your */
/* serial port: (To make this example simple we do not */
/* turn it off.) */
buffer[ 0 ] = 0x4C;
buffer[ 1 ] = 0x31;
/* Send 2 bytes to the serial port and return immediately: */
SerWriteNoWait( serial_req, buffer, 2 );
/* 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 IOExtSer *) CheckIO( serial_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->IOSer.io_Message.mn_Node) );
/* Check if everything is OK? */
if( serial_req->IOSer.io_Error )
SerError( error ); /* ERROR */
else
printf( "Lamp OFF!\n" ); /* OK */
/* 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 )
SerError( error );
/* Close the Serial Device: */
if( !serial_dever )
CloseDevice( serial_req );
/* Deallocate the serial request block: */
if( serial_req )
DeleteExtIO( serial_req, sizeof( struct IOExtSer ) );
/* Remove the replyport: */
if( replymp )
DeletePort( replymp);
/* Print the message: */
printf( "\n%s\n", text );
/* Quit: */
exit( 0 );
}
/* 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 eve