home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fred Fish Collection 1.5
/
ffcollection-1-5-1992-11.iso
/
ff_disks
/
300-399
/
ff319.lzh
/
CNewsSrc
/
uupc.lzh
/
uupc
/
serialio.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-16
|
9KB
|
338 lines
/*
* SerialIO.c
* Created: June 1986 by J.A. Lydiatt
*
* Amiga Serial I/O Library
*
* Note: This set of routines will not work with Lattice 4.0
* since the Lattice stub for SER_SETPARAM is incorrect.
* It does work with Manx, however.
* Note:
* A Task may only open the serial port once.
*
* $Id: serialio.c,v 1.2 90/01/16 10:27:19 crash Exp Locker: crash $
*/
#ifndef lint
static char RCSid[] = "$Id: serialio.c,v 1.2 90/01/16 10:27:19 crash Exp Locker: crash $";
#endif /* lint */
#include <exec/types.h>
#include <exec/memory.h>
#include <stdio.h>
#include <devices/serial.h>
#ifdef MCH_AMIGA
# include <functions.h> /* Manx */
#else
# include <proto/exec.h> /* Lattice */
#endif
/* Allowable Mode Values */
#define MODEHALF 0
#define MODEFULL 1
#define MODEECHO 2
/* Default starting values */
#define STARTBAUD 2400 /* default baud rate */
#define STARTMODE MODEFULL
#define SERFLAGS (SERF_SHARED | SERF_XDISABLED | SERF_QUEUEDBRK)
#define CTLCHAR 0x11130501
static int Mode = STARTMODE;
static int amClosing = FALSE;
/* declarations for the serial stuff */
typedef struct IORequest * IO_CAST;
static struct IOExtSer *Read_Request = NULL;
static UBYTE rs_in[2];
static struct IOExtSer *Write_Request = NULL;
static UBYTE rs_out[2];
/* stack to save Serial Port flags */
#define MAXSTACK 10
static int stackSize = 0;
static int modeStack[ MAXSTACK ];
static UBYTE flagStack[ MAXSTACK ];
/*------------------------------------------------------------*/
/* GetSerialSigBit: return Read_Request's Signal Bit */
/*------------------------------------------------------------*/
int GetSerialSigBit()
{
return Read_Request->IOSer.io_Message.mn_ReplyPort->mp_SigBit;
}
/*------------------------------------------------------------*/
/* CheckSerIO : return TRUE if serial port has a character */
/*------------------------------------------------------------*/
BOOL CheckSerIO()
{
return (BOOL)( CheckIO( (IO_CAST) Read_Request ) != NULL);
}
/*------------------------------------------------------------*/
/* FlushSerIO: Flush the receive buffer */
/*------------------------------------------------------------*/
void FlushSerIO()
{
register struct IOExtSer *r = Read_Request;
AbortIO( (IO_CAST) r ); /* abort it */
AbortIO( (IO_CAST) Write_Request );
r->IOSer.io_Command = CMD_FLUSH;
DoIO( (IO_CAST) r ); /* flush all IO requests */
r->IOSer.io_Command = CMD_CLEAR;
DoIO( (IO_CAST) r); /* flush receive buffer */
if ( !amClosing ) {
r->IOSer.io_Command = CMD_READ;
r->IOSer.io_Length = 1;
BeginIO( (IO_CAST) r ); /* start receive request */
}
}
/*------------------------------------------------------------*/
/* PushSerState: save current io flags */
/*------------------------------------------------------------*/
void PushSerState()
{
register struct IOExtSer *r = Read_Request;
if ( stackSize < MAXSTACK )
{
/* Save the current Mode */
modeStack[ stackSize ] = Mode;
/* Get the current flags */
AbortIO( (IO_CAST) r );
flagStack[ stackSize++ ] = r->io_SerFlags;
BeginIO( (IO_CAST) r );
}
}
/*------------------------------------------------------------*/
/* PullSerState: restore last saved flag state */
/*------------------------------------------------------------*/
void PullSerState()
{
register struct IOExtSer *r = Read_Request;
if ( stackSize > 0 )
{
/* Reset the Mode */
Mode = modeStack[ --stackSize ];
/* Set the old flags */
AbortIO( (IO_CAST) r );
r->io_SerFlags = flagStack[ stackSize ];
r->IOSer.io_Command = SDCMD_SETPARAMS;
DoIO( (IO_CAST) r );
r->IOSer.io_Command = CMD_READ;
BeginIO( (IO_CAST) r );
}
}
/*-------------------------------------------------------------*/
/* CloseSerialIO: Close the serial port */
/*-------------------------------------------------------------*/
void CloseSerialIO()
{
register struct IOExtSer *r = Read_Request;
register struct IOExtSer *w = Write_Request;
if ( r != NULL ) {
amClosing = TRUE;
CloseDevice( r );
DeletePort( r->IOSer.io_Message.mn_ReplyPort );
FreeMem( r, (long)sizeof( struct IOExtSer ) );
Read_Request = NULL;
}
if ( w != NULL ) {
#ifndef FJE
CloseDevice( w );
#endif
DeletePort( w->IOSer.io_Message.mn_ReplyPort );
FreeMem( w, (long)sizeof( struct IOExtSer ) );
Write_Request = NULL;
}
}
/*-------------------------------------------------------------*/
/* InitSerialIO: Open serial IO - return read Port */
/*-------------------------------------------------------------*/
struct IOExtSer *InitSerialIO()
{
register struct IOExtSer *r, *w;
if ( Read_Request != NULL ) {
fprintf( stderr, "Error: Serial Port already open for read.\n");
return NULL;
}
r = (struct IOExtSer *) AllocMem( (long)sizeof(struct IOExtSer),
(long)(MEMF_PUBLIC|MEMF_CLEAR));
if (r == NULL)
return NULL;
Read_Request = r;
r->io_SerFlags = SERFLAGS;
r->io_CtlChar = CTLCHAR;
r->IOSer.io_Message.mn_ReplyPort = CreatePort("Read_RS",NULL);
if(OpenDevice(SERIALNAME,NULL,r,NULL)) {
fprintf( stderr, "Can't open Read device\n");
goto q4;
} else
printmsg( 20, "Opened read device '%s'\n", SERIALNAME);
r->io_SerFlags = SERFLAGS;
r->io_CtlChar = CTLCHAR;
r->io_Baud = STARTBAUD;
r->io_ReadLen = 8;
r->io_WriteLen = 8;
r->IOSer.io_Command = SDCMD_SETPARAMS;
DoIO((IO_CAST) r);
r->IOSer.io_Command = CMD_READ;
r->IOSer.io_Length = 1;
r->IOSer.io_Data = (APTR)&rs_in[0];
if ( Write_Request != NULL ) {
fprintf( stderr, "Error: Serial Port already open for writing.\n");
goto q3;
}
w = (struct IOExtSer *)AllocMem( (long)sizeof( struct IOExtSer ),
(long)MEMF_PUBLIC|MEMF_CLEAR);
if (w == NULL) goto q3;
Write_Request = w;
w->io_SerFlags = SERFLAGS;
w->io_CtlChar = CTLCHAR;
w->IOSer.io_Message.mn_ReplyPort = CreatePort("Write_RS",NULL);
if(OpenDevice(SERIALNAME,NULL,w,NULL)) {
fprintf( stderr, "Can't open Write device\n");
goto q1;
} else
printmsg( 20, "Opened write device '%s'\n", SERIALNAME);
w->io_SerFlags = SERFLAGS;
w->io_CtlChar = CTLCHAR;
w->io_Baud = STARTBAUD;
w->io_ReadLen = 8;
w->io_WriteLen = 8;
w->IOSer.io_Command = SDCMD_SETPARAMS;
DoIO((IO_CAST) w);
w->IOSer.io_Command = CMD_WRITE;
w->IOSer.io_Length = 1;
w->IOSer.io_Data = (APTR)&rs_out[0];
BeginIO( (IO_CAST) r );
stackSize = 0;
return r;
q1: DeletePort( w->IOSer.io_Message.mn_ReplyPort );
q2: FreeMem( w, (long)sizeof( *w) );
q3: CloseDevice( r );
q4: DeletePort( r->IOSer.io_Message.mn_ReplyPort );
q5: FreeMem( r, (long)sizeof( *r) );
return NULL;
}
/*----------------------------------------------------------*/
/* SetXonMode: set Xon On or Off */
/*----------------------------------------------------------*/
void SetXonMode( status )
BOOL status;
{
register UBYTE flags;
register struct IOExtSer *r = Read_Request;
/* Get the current flags */
AbortIO( (IO_CAST) r );
flags = r->io_SerFlags;
if ( status )
flags &= ~(SERF_XDISABLED);
else
flags |= SERF_XDISABLED;
r->io_SerFlags = flags;
r->IOSer.io_Command = SDCMD_SETPARAMS;
DoIO( (IO_CAST) r );
r->IOSer.io_Command = CMD_READ;
BeginIO( (IO_CAST) r );
}
/*----------------------------------------------------------*/
/* SetSerBaud: set Serial Baud Rate */
/*----------------------------------------------------------*/
void SetSerBaud( baud )
int baud;
{
register struct IOExtSer *r = Read_Request;
/* Get the current flags */
AbortIO( (IO_CAST) r );
r->io_Baud = baud;
r->IOSer.io_Command = SDCMD_SETPARAMS;
DoIO( (IO_CAST) r );
r->IOSer.io_Command = CMD_READ;
BeginIO( (IO_CAST) r );
}
/*----------------------------------------------------------*/
/* SetSerMode: set the Serial Mode */
/*----------------------------------------------------------*/
void SetSerMode( mode )
int mode;
{
Mode = mode;
}
void SerIOBreak(count)
long count;
{
Write_Request->IOSer.io_Command = SDCMD_BREAK;
Write_Request->io_BrkTime = count;
DoIO( Write_Request );
}
/*----------------------------------------------------------*/
/* SerIOWrite: Write a byte out the serial port (no echo) */
/*----------------------------------------------------------*/
void SerIOWrite( c )
register UBYTE c;
{
register int err;
*rs_out = c;
if (err = DoIO( (IO_CAST) Write_Request ))
printmsg( 0, "\nerror %d during write", err);
}
/*----------------------------------------------------------*/
/* SerIORead: read a byte from the serial port (no echo) */
/*----------------------------------------------------------*/
UBYTE SerIORead()
{
register struct IOExtSer *r = Read_Request;
register UBYTE c;
WaitIO( (IO_CAST) r );
c = *rs_in;
BeginIO( (IO_CAST) r );
return c;
}