From: thecure@mullian.ee.mu.OZ.AU (matthew aardvarkious)
Subject: Help with this C code please. (console/serial)
Organization: Computer Science, University of Melbourne, Australia
Date: Sat, 10 Oct 1992 05:20:23 GMT

*sigh*, I thought this shoudl work...

Someone take a look, as it compiles, and crashes. 

I'm probably doing something *terribly* wrong, but just let me know.


#include <stdio.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <devices/serial.h>

struct IntuitionBase *IntuitionBase;
struct Window *FirstWindow;
struct IntuiMessage *message;

struct MsgPort  *serial_read_mp, *serial_write_mp,
                *console_write_mp, *console_read_mp = NULL;
struct IOStdReq *console_write_msg, *console_read_msg = NULL;
struct IOExtSer *serial_read_msg, *serial_write_msg = NULL;
struct NewWindow ConsoleWindow =
        { 0,0,640,101,2,3,CLOSEWINDOW,WINDOWDRAG|WINDOWCLOSE|WINDOWSIZING|WINDOWDEPTH|SMART_REFRESH,
        NULL,NULL,(UBYTE *)"Da window",NULL,NULL,640,50,640,200,WBENCHSCREEN,};

UWORD serial_dever,console_dever = TRUE;
UBYTE serbuffer[80],conbuffer[80];


main()
{
        long intuition_mask,serial_mask,console_mask,mask,serial_length;
        Open_Serial();
        Open_Console();
        IO_Read(console_read_msg,&conbuffer[0]);
        IO_Read(serial_read_msg,&serbuffer[0]);
        intuition_mask = 1 << FirstWindow->UserPort->mp_SigBit;
        serial_mask = 1 <<serial_read_mp->mp_SigBit;
        console_mask = 1 << console_read_msg->io_Message.mn_ReplyPort->mp_SigBit;
        for(;;)
        {
                mask = Wait(intuition_mask|console_mask|serial_mask);
                if (mask & intuition_mask)
                {
                        while (message = (struct IntuiMessage *) GetMsg(FirstWindow->UserPort))
                        {
                                switch(message->Class)
                                {
                                        case CLOSEWINDOW:
                                                Close_All();
                                                exit(0);
                                }
                        ReplyMsg(message);
                        }
                }
                if (mask & console_mask)
                {
                        if (CheckIO(console_read_mp))
                        {
                                WaitIO(console_read_mp);
                                switch (conbuffer[0])
                                {
                                        case 0x0d:
                                                IO_Write(console_write_msg,&conbuffer[0]);
                                                IO_Write(console_write_msg,0x0a);
                                                break;
                                        default:
                                                IO_Write(console_write_msg,&conbuffer[0]);
                                                break;
                                }
                                IO_Read(console_read_msg,&conbuffer[0]);
                        }
                }
                if (mask & serial_mask)
                {
                        if (CheckIO(serial_read_mp))
                        {
                                if(WaitIO(serial_read_mp))
                                {
					SetSignal(0,(1<< serial_read_mp -> mp_SigBit));
					IO_Read(serial_read_msg,&serbuffer[0]);
				}
				else
				IO_Write(console_write_msg,serbuffer);
				do
				{
					serial_write_msg->IOSer.io_Command=SDCMD_QUERY;
					DoIO(serial_write_msg);
					if (serial_length = serial_write_msg->IOSer.io_Actual)
					{
						if (serial_length>32768)
							serial_length=32768;
						serial_read_msg->IOSer.io_Command = CMD_READ;
						serial_read_msg->IOSer.io_Data = serbuffer;
						serial_read_msg->IOSer.io_Length = serial_length;

						if (!DoIO(serial_read_msg))
						{
							console_write_msg->io_Command=CMD_WRITE;
							console_write_msg->io_Data=serbuffer;
							console_write_msg->io_Length=serial_length;
							DoIO(console_write_msg);
						}
					}
				
				}
				while(serial_length);
				SetSignal(0,(1<< serial_read_mp -> mp_SigBit));
				IO_Read(serial_read_msg);
                        }
                }
        }
}

IO_Write(request,output)

struct IOStdReq *request;
char output[];
{
        request->io_Command = CMD_WRITE;
        request->io_Data = (APTR)output;
        request->io_Length = 1;
        DoIO(request);
}

IO_Read(request,input)

struct IOStdReq *request;
char input[];
{
        request->io_Command = CMD_READ;
        request->io_Data = (APTR)input;
        request->io_Length = 1;
        SendIO(request);
}

Open_Console()
{
struct Library *OpenLibrary();
struct Window *OpenWindow();

IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0);
FirstWindow = (struct Window *) OpenWindow(&ConsoleWindow);

console_write_mp = (struct MsgPort *) CreatePort( NULL,0);
console_write_msg = (struct IOStdReq *) CreateStdIO(console_write_mp);

console_read_mp = (struct MsgPort *) CreatePort( NULL,0);
console_read_msg = (struct IOStdReq *) CreateStdIO(console_read_mp);

console_write_msg->io_Data = (APTR)FirstWindow;
console_write_msg->io_Length = sizeof (*FirstWindow);

console_dever = OpenDevice( "console.device", 3,console_write_msg, 0);

console_read_msg->io_Device = console_write_msg->io_Device;
console_read_msg->io_Unit = console_write_msg->io_Unit;
}

SetFlags(struct IOExtSer *request)
{

request->io_Baud = 9600;
request->io_BrkTime = 25000;
request->io_ReadLen = 8;
request->io_WriteLen = 8;
request->io_StopBits = 1;
request->io_ExtFlags &= ~(SEXTF_MSPON|SEXTF_MARK);
request->io_SerFlags &= ~(SERF_PARTY_ON|SERF_PARTY_ODD|SERF_7WIRE|SERF_RAD_BOOGIE|SERF_XDISABLED);
}

Open_Serial()
{

serial_read_mp = (struct MsgPort *) CreateMsgPort(NULL,0);
serial_read_msg = (struct IOExtSer *) CreateIORequest(serial_read_mp, sizeof( struct IOExtSer ) );

SetFlags(serial_read_msg);

serial_read_msg->io_RBufLen = 32768;

serial_dever = OpenDevice("serial.device", 0, serial_read_msg, 0);

serial_write_mp = (struct MsgPort *) CreateMsgPort(NULL,0);
serial_write_msg = (struct IOExtSer *) CreateIORequest(serial_write_mp, sizeof( struct IOExtSer ) );

CopyMem(console_read_msg,console_write_msg,sizeof(struct IOExtSer));

serial_write_msg->IOSer.io_Message.mn_ReplyPort=serial_write_mp;
serial_write_msg->IOSer.io_Command=SDCMD_SETPARAMS;

SetFlags(serial_write_msg);

DoIO(serial_write_msg);

serial_read_msg->IOSer.io_Command = CMD_READ;
serial_read_msg->IOSer.io_Data = serbuffer;
serial_read_msg->IOSer.io_Length = 1;

SetSignal(0,(1<< serial_read_mp -> mp_SigBit));         /* dangerous? Ha. We laugh at autodocs. 8^) */

SendIO(serial_read_msg);
}

Close_All()
{
CloseDevice(serial_write_msg);
CloseDevice(serial_read_msg);
CloseDevice(console_write_msg);
DeleteStdIO(serial_write_msg);
DeleteStdIO(serial_read_msg);
DeleteStdIO(console_write_msg);
DeleteStdIO(console_read_msg);
DeletePort(serial_read_mp);
DeletePort(serial_write_mp);
DeletePort(console_write_mp);
DeletePort(console_read_mp);
CloseWindow(FirstWindow);
CloseLibrary(IntuitionBase);
}
--
These are my personal views and do not reflect those of Melbourne University.
Anything I may say that may be deemed offensive and/or damaging should NOT
be redirected to administration, as they have had nothing to do with it. 
Please direct any/all complaints/enquiries to thecure@ee.mu.oz.au