
/*****************************************************************************
 *								 readrs.c    *
 *****************************************************************************
 * DESCRIPTION: This program reads data from the spectrum over the RS232 link*
 *									     *
 * REVISIONS:   18 AUG 91 - HDG - Initial setup.			     *
 *****************************************************************************/

#include		<stdio.h>
#include		<signal.h>
#include		"ibmcom.h"
#include		"patch.h"

STARTPATCH
int PORT=1;
ENDPATCH

#define SPEED   9600

FILE * sid;

void catch();

get_key()
{
	int		 c;
	if ((c = getch())!=0) return c;
	else return getch() + 256;
}

main(argc,argv) 
int argc;
char *argv[];
{
	int status;
	int i;

	SETUP;

	if(argc != 2)
	{
		fprintf(stderr,"Usage: readrs <file>\n");
		return 1;
	}
	
	if ((status = com_install(PORT))!=0)
	{
		printf("com_install() error: %d\n", status);
		return 1;
	}

	signal(SIGINT, catch);

	sid = fopen(argv[1],"wb");
	if(sid == NULL)
	{
		perror(argv[1]);
		return 1;
	}
	
	com_set_speed(SPEED);
	com_set_parity(COM_NONE, 1);
	com_raise_dtr();
	printf("RS232 to file %s -- ",argv[1]);
	printf("Stop with ALT-X\n\n");
	
	while (1)
	{
		if (kbhit()) 
		{
			status = get_key();
			if (status == 301 /* Alt-X */) 
			{
				fclose(sid);
				com_deinstall();
				return 1;
			}
		}
		if ((status = com_rx()) != -1)
		{
			fputc((char) status,sid);
		}
	}
}

void catch()
{
	printf("\nBREAK - program terminated\n");
	fclose(sid);
	com_deinstall();
	exit(1);
}

void setup()
{
	printf("Setup\n\n");
	printf("Port    Address    IRQ\n");
	printf("COM1      3f8       4\n");
	printf("COM2      2f8       3\n");
	printf("COM3      3e8       4\n");
	printf("COM4      2e8       3\n\n");
	printf("Attention if you want to use COM3 or COM4:\n\n");
	printf("COM1 and COM3 cannot be used simultaneously\n");
	printf("COM2 and COM4 cannot be used simultaneously\n");
	printf("If your mouse is connected to COM1 you should not use COM3\n");
	printf("If your mouse is connected to COM2 you should not use COM4\n\n");
	printf("If your modem is connected to COM1 you can use COM3 if this program\n");
	printf("and the communication program are not running at the same time\n\n");
	printf("If your modem is connected to COM2 you can use COM4 if this program\n");
	printf("and the communication program are not running at the same time\n\n");
	printf("Give comport number: ");
	fflush(stdout);
	scanf("%d",&PORT);
	printf("\n\nSetup finished\n\n");
}
