From: mbs@adastra.cvl.va.us (Michael B. Smith)
Newsgroups: comp.sys.amiga.programmer
Subject: A serial.device example (LONG) (was Re: SERIAL.DEVICE
HEEEEELLLLLPPPPPPP)
Distribution: world
X-NewsSoftware: GRn 1.16f (beta) by Mike Schwartz & Michael B. Smith
Date: 1 Sep 92 19:00:10 EDT
Organization: Private UUCP Node

In article <Vic.00yf@angband.delfax.OCunix.On.Ca>
Vic@angband.delfax.OCunix.On.Ca (Vic Lewington) writes:
> In article <u895762.715081138@bruny> u895762@bruny.cc.utas.edu.au (Paul S.E.)
writes:
> > I have a problem that has been driving me mad for the past 5 hours.
> > I am trying to read data from serial.device, one character at a time. (i.e.
in real time), but the 512 byte buffer prevents me from doing it.
> > HELP, anybody?!
>
>     Having just spent a few hours going nuts myself, here, try this...

[example deleted]

Apologies for posting this to a non-source group. However, I spent a couple
of days looking for a simple, working example, before writing this, and it
wasn't to be found. This may do someone else some good...

Here is a piece of working code that comes with the next release of GRn. No
warranties, and you get what you pay for. It was written to be clear, not
extremely efficient. This requires 2.0+, due to the use of PrintFault().
Remove that, and it should work on 1.3.

This is DICE code, but the only place it should matter is the declaration of
InputHandler(), which is currently:
             __geta4 void InputHandler (void);
I think SAS/C will require this to be:
             void __saveds InputHandler (void);
but I'm not sure...

Flames to NIL:

/*
        GRn-Term.c

        GRn - Gadtools Read News

        Copyright 1992 by Michael B. Smith.

        All Rights Reserved.

        No warranties, express or implied, are available. Use at your own risk.
        The authors have taken pains to attempt to produce quality software,
        but by no means are we liable for anything it does to your system.

        Feel free to modify the source for your own use, but do NOT distribute
        unauthorized sources or binaries as part of GRn. You may distribute
        the source and binaries in unmodified form.
*/

#define _GRNTERM_

#include <stdlib.h>
#include <stdarg.h>

#include <exec/types.h>
#include <devices/serial.h>
#include <dos/dosextens.h>
#include <dos/dostags.h>
#include <clib/dos_protos.h>

#ifndef _GRNTERM_
extern void Abort (void);
extern void panic (const char *, ...);
#endif

static struct MsgPort *NNTP_in_mp  = NULL;
static struct MsgPort *NNTP_out_mp = NULL;

static int NNTP_in_ip  = 0; // input  IO in-process
static int NNTP_out_ip = 0; // output IO in-process

struct IOExtSer *NNTP_in_io  = NULL;
struct IOExtSer *NNTP_out_io = NULL;

int serial_error = 0;

void ShutDownSerialPort (void);
void KillSerialPort     (const int, const char *);
void SetupSerialPort    (const int, const char *, const int);
int reads               (struct IOExtSer *, char *, int);
int writes              (struct IOExtSer *, char *, int);

void ShutDownSerialPort (void)
{
        static int in_sdsp = 0;  // use as a semaphore

        if (in_sdsp)
                return;

        in_sdsp = 1;

        if (NNTP_out_io && NNTP_out_ip) {
                AbortIO (NNTP_out_io);
                WaitIO (NNTP_out_io);
                NNTP_out_ip = 0;
        }
        if (NNTP_in_io && NNTP_in_ip) {
                AbortIO (NNTP_in_io);
                WaitIO (NNTP_in_io);
                NNTP_in_ip = 0;
        }

        if (NNTP_in_io && !serial_error)
                CloseDevice (NNTP_in_io);

        if (NNTP_out_io) {
                DeleteExtIO (NNTP_out_io, sizeof (struct IOExtSer));
                NNTP_out_io = NULL;
        }

        if (NNTP_in_io) {
                DeleteExtIO (NNTP_in_io, sizeof (struct IOExtSer));
                NNTP_in_io = NULL;
        }

        if (NNTP_out_mp) {
                DeletePort (NNTP_out_mp);
                NNTP_out_mp = NULL;
        }

        if (NNTP_in_mp) {
                DeletePort (NNTP_in_mp);
                NNTP_in_mp = NULL;
        }

        in_sdsp = 0;
        return;
}

void KillSerialPort (const int errno, const char *msg)
{
#ifdef _GRNTERM_
        if (errno)
                PrintFault (errno, msg);
        else {
                PutStr (msg);
                PutStr ("\n");
        }
        ShutDownSerialPort ();
        exit (20);
#else
        ShutDownSerialPort ();
        panic (msg);
        Abort ();
#endif
        /* NOTREACHED */
        return;
}

int SendQuery (struct IOExtSer *iorequest)
{
        int error;

        iorequest->IOSer.io_Command = SDCMD_QUERY;
        if (error = DoIO (iorequest)) {
                PrintFault (error, "SendQuery error");
                return -1;
        }

        return iorequest->IOSer.io_Actual;
}

void SiphonIO (struct IOExtSer *iorequest)
{
        int i;
        char buf [16];

        while ((i = SendQuery (iorequest)) > 0) {
                iorequest->IOSer.io_Command = CMD_READ;
                iorequest->IOSer.io_Data    = buf;
                iorequest->IOSer.io_Length  = i > 16 ? 16 : i;
                iorequest->IOSer.io_Flags   = IOF_QUICK;

                if (i = DoIO (iorequest))
                        PrintFault (i, "SiphonIO error");
        }
        return;
}

void SetupSerialPort (const int eofmode, const char *device, const int unit)
{
        int errno;

        if(!(NNTP_in_mp = (struct MsgPort *) CreatePort (NULL, 0)))
                KillSerialPort (0, "cannot create msgport for input");

        if (!(NNTP_out_mp = (struct MsgPort *) CreatePort (NULL, 0)))
                KillSerialPort (0, "cannot create msgport for output");

        if (!(NNTP_in_io = (struct IOExtSer *) CreateExtIO (NNTP_in_mp, sizeof
(struct IOExtSer))))
                KillSerialPort (0, "cannot createExtIO() for input");

        if (!(NNTP_out_io = (struct IOExtSer *) CreateExtIO (NNTP_out_mp,
sizeof (struct IOExtSer))))
                KillSerialPort (0, "cannot createExtIO() for output");

        NNTP_in_io->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
        if (eofmode) {
                NNTP_in_io->io_SerFlags |= SERF_EOFMODE;
                NNTP_in_io->io_TermArray.TermArray0 = 0x1615120c;
                NNTP_in_io->io_TermArray.TermArray1 = 0x0a030000;
        }

        if (serial_error = OpenDevice (device, unit, NNTP_in_io, 0)) {
                printf ("device %s unit %ld IoErr = %ld\n", device, unit, IoErr
());
                KillSerialPort (serial_error, "OpenDevice() failed");
        }

        SendQuery (NNTP_in_io);

        NNTP_in_io->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
        if (eofmode) {
                NNTP_in_io->io_SerFlags |= SERF_EOFMODE;
                NNTP_in_io->io_TermArray.TermArray0 = 0x1615120c;
                NNTP_in_io->io_TermArray.TermArray1 = 0x0a030000;
        }
        if (NNTP_in_io->io_RBufLen < 32768)
                NNTP_in_io->io_RBufLen = 32768;

        NNTP_in_io->IOSer.io_Command  = SDCMD_SETPARAMS;

        if (errno = DoIO (NNTP_in_io))
                KillSerialPort (errno, "setparams error");

        SiphonIO (NNTP_in_io);

        memcpy ((void *) NNTP_out_io, (void *) NNTP_in_io, sizeof (struct
IOExtSer));
        NNTP_out_io->IOSer.io_Message.mn_ReplyPort = NNTP_out_mp;

        return;
}

int reads (struct IOExtSer *iorequest, char *buf, int len)
{
        int errno;
        int mask;
        //
        // reads:   Get input from "iorequest". We DO NOT HANG. If
        //          no bytes are available, we immediately return.
        //          This is not an error. The caller should deal
        //          with this.
        //
        //          Otherwise, we read UP TO len bytes.
        //
        //          Note that efficiency and high transfer rates will
        //          require len to be much greater than 1!!!
        //
        //          Note that if len == -1, serial.device will build
        //          the result string based on the TermArrays passed
        //          when it was opened.
        //
        if (len >= 0) {
                if ((errno = SendQuery (iorequest)) == 0)
                        return 0;
                if (len > errno)
                        len = errno;
        }

        iorequest->IOSer.io_Command = CMD_READ;
        iorequest->IOSer.io_Data    = buf;
        iorequest->IOSer.io_Length  = len;
        iorequest->IOSer.io_Flags   = IOF_QUICK;
        iorequest->IOSer.io_Actual  = 0;

        BeginIO (iorequest); // in amiga.lib

        if ((iorequest->IOSer.io_Flags & IOF_QUICK) == 0) {

                NNTP_in_ip = 1;