[comp.sources.amiga] v90i091: serial - example of use of serial.device, Part01/01

Amiga-Request@cs.odu.edu (Amiga Sources/Binaries Moderator) (03/04/90)

Submitted-by: Simon Raybould <S.J.Raybould@fulcrum.bt.co.uk>
Posting-number: Volume 90, Issue 091
Archive-name: examples/serial

I have seen a few requests for info about the serial.device so here is an
example program to show how to set the baud rate, parity e.t.c. and then
issue reads and writes

Please don't flame me about the code it is just an example as to how to set
up the serial device and issue writes and reads. I think all of the type
casts are correct but this really isn't the issue here.

For more information you should see the Rom Kernel Reference manual includes
and autodocs and Libs and devices.

I havn't been able to get hold of a copy of Libs&Devs in this country yet so
all the info is from includes and autodocs. But as soon as I see a copy I
shall buy it.

Cheers,
      Simon Raybould

#!/bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 1 (of 1)."
# Contents:  serial.c
# Wrapped by tadguy@xanth on Sat Mar  3 19:36:05 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'serial.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'serial.c'\"
else
echo shar: Extracting \"'serial.c'\" \(3318 characters\)
sed "s/^X//" >'serial.c' <<'END_OF_FILE'
X/*
X *
X *  Description : Example of how to set up and use the serial device.
X *                This simple example reads chars from the serial device
X *                and echoes them back to the serial device. It also maps
X *                CR to NL + CR just for a bit of fun. It is not intended
X *                to do anything wizzo, just outline the basic steps needed
X *                to setup and use the serial.device on the Amiga.
X *
X *  Author      : Simon Raybould     (sie@fulcrum.bt.co.uk)
X *
X *  Date        : 14th Feb 1990
X *
X */
X
X
X#include <exec/types.h>
X#include <devices/serial.h>
X#include <libraries/dos.h>
X
Xstruct MsgPort	*CreatePort();
Xstruct IORequest	*CreateExtIO();
Xvoid DeletePort();
Xvoid DeleteExtIO();
X
Xstruct MsgPort	*serialport;
Xstruct IOExtSer	*serialIO;
Xshort openerror;
X
Xmain()
X{
X	void write(), cleanexit(), SetUpSerial();
X	char c=0, read();
X
X	serialport = CreatePort(0L, 0L);
X	if(!serialport)
X		cleanexit(RETURN_FAIL);
X
X
X	serialIO=(struct IOExtSer *)
X		CreateExtIO(serialport,(long)sizeof(struct IOExtSer));
X	if(!serialIO)
X		cleanexit(RETURN_FAIL+1);
X	if(openerror=OpenDevice("serial.device",0L,serialIO,0L))
X		cleanexit(RETURN_FAIL+2);
X	
X
X	serialIO->IOSer.io_Message.mn_ReplyPort = serialport;
X
X	SetUpSerial();	/* Set baud rate, parity, stop bits  e.t.c. */
X
X	while(c != 'q' && c != 'Q') {
X		c = read();
X		if(c == '\r')
X			write('\n');
X		write(c);
X	}
X
X	cleanexit(RETURN_OK);
X}
X
X/*
X * Function: read() - blocks and aits for the next char on the serial
X *                    devide. It then returns the next char received.
X *
X * Author: Simon Raybould.
X *
X * Date  : 14th Feb 1990.
X */
Xchar read()
X{
X	char c=0;
X
X	serialIO->IOSer.io_Command = CMD_READ;
X	serialIO->IOSer.io_Length = 1;
X	serialIO->IOSer.io_Data = (APTR)&c;
X	
X	DoIO(serialIO);
X	return c;
X}
X
X/*
X * Function: write() - writes the char passed to the seial device.
X *
X * Author: Simon Raybould.
X *
X * Date  : 14th Feb 1990.
X */
X
Xvoid write(c)
Xchar c;
X{
X	serialIO->IOSer.io_Command = CMD_WRITE;
X	serialIO->IOSer.io_Length = 1;
X	serialIO->IOSer.io_Data = (APTR)&c;
X	
X	DoIO(serialIO);
X}
X
X/*
X * Function: SetUpSerial() - Set BAUD, PARITY, STOP BITS e.t.c.
X *                           this is hard coded in this example just to 
X *                           show where these values should go.
X *
X * Author: Simon Raybould.
X *
X * Date  : 14th Feb 1990.
X */
X
Xvoid SetUpSerial()
X{
X	serialIO->IOSer.io_Command = SDCMD_SETPARAMS;
X	serialIO->io_Baud = 1200;	/* Set this to the baud rate */
X	/*
X	 * Set mode to odd parity 
X	 * remove the SERF_PARTY_ODD for EVEN parity
X	 * remove the SERF_PARTY_ON for no parity i.e set to 0 for none
X	 */
X	serialIO->io_SerFlags = SERF_PARTY_ON | SERF_PARTY_ODD;
X	serialIO->io_ExtFlags = 0;	/* If not used must be set to 0 !!!! */
X	serialIO->io_StopBits = 1;
X
X	DoIO(serialIO);
X}
X
X/*
X * Function: Cleanexit() - Graceful exit routine.
X *
X * Author: Taken from RKM 1.3 Includes and Autodocs (SECTION B First page).
X *         And yes it realy does say that reading legal mush can turn your
X *         brain to gucamole!!!
X *
X */
X
Xvoid cleanexit(returncode)
Xint returncode;
X{
X	if(returncode!=RETURN_OK)
X		printf("Failed to open device\n");
X
X	if(!openerror)	CloseDevice(serialIO);
X	if(serialIO)	DeleteExtIO(serialIO,(long)sizeof(struct IOExtSer));
X	if(serialport)	DeletePort(serialport);
X
X	exit(returncode);
X}
END_OF_FILE
if test 3318 -ne `wc -c <'serial.c'`; then
    echo shar: \"'serial.c'\" unpacked with wrong size!
fi
# end of 'serial.c'
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have the archive.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0
-- 
Mail submissions (sources or binaries) to <amiga@cs.odu.edu>.
Mail comments to the moderator at <amiga-request@cs.odu.edu>.
Post requests for sources, and general discussion to comp.sys.amiga.