[comp.protocols.tcp-ip] Asynchronous Framing Technique Software: TEST2.C

n2dsy@hou2d.UUCP (G.BEATTIE) (04/08/88)

/*
	This is a test program for the AFT system.

            Version 1.0, by John Howell (N2FVN), Copyright August 7, 1987
               
            This software is free for non-commercial use.  All commercial
            rights are retained by the author or his designees.  Commercial 
            use without the explicit written permission of the author is
            prohibited.  This package is provided on an 'as is' basis 
            without warranty.

	Send and receive frames.  Machine dependant for the PC with
	async driver installed.  Requires Microsoft C 4.0.
*/

#include "stdio.h"
#include "conio.h"
#include "dos.h"
#include "fcntl.h"
#include <sys\types.h>
#include <sys\stat.h>
#include "io.h"
#include "stdlib.h"

int async_handle;

main()
{
	char ebdt, c, level, suff_str[10], suff_len;
	char in_str[256], out_str[256];
	int out_busy;

	int rx_max, in_len, out_len, i, match, tc;

	printf("AFT Test Program 2\n");
	printf("Send and receive frames over COM1: using ASYNC driver.\n");

	async_handle = open("ASYNC1", O_RDWR | O_BINARY);

	if (async_handle == -1) {
		printf("Open of async driver failed. errno=%d\n", errno);
		printf("Make sure ASYNC.BIN is included in CONFIG.SYS.\n");
		exit(1);
	}

	printf("Eight-bit data transparency (y/n)?");
	scanf("%1s",&c);
	ebdt = c == 'y' || c == 'Y';

	printf("Transparency level (0-2)?");
	scanf("%d",&level);

	printf("Suffix string (HEX)?");
	get_array(suff_str, &suff_len);

	rx_max = sizeof(in_str) - 2;

	aft_options(ebdt, level, suff_len, suff_str, rx_max);
	aft_rx_start(in_str);
	out_len = 0;
	out_busy = 0;

	printf("Enter characters to fill transmit buffer.  Return to send.\n");
	printf("Enter empty line to exit.\n\n");

	while (1) {

		if (async_char_waiting()) {

			if (aft_rx_char(async_get_char())) {
				in_len = aft_rx_complete();

				printf("Frame received. Length=%d data:",
					in_len);

				for (i=0; i<in_len; i++)
					printf("%c",in_str[i]);
				printf("\n");

				aft_rx_start(in_str);
			}
		}

		if (kbhit() && !out_busy) {

			if ((c = getch()) == 0x0d) {
				if (out_len == 0)
					exit(0);

				printf("--  Sending.  Length=%d\n",
					out_len);

				aft_tx_start(out_len, out_str);
				out_busy = 1;
			}
			else {
				if (out_len == sizeof(out_str)) {
					printf("\007");
				}
				else {
					out_str[out_len++] = c;
					printf("%c",c);
				}
			}
		}

		if (out_busy && async_output_ready()) {
			
			if ((i = aft_tx_char()) < 256) {
				async_put_char(i);
			}
			else {
				out_busy = 0;
				out_len = 0;
			}
		}
	}
}


get_array(buffer, len)
char *buffer;
int *len;
{
	char c;

	c = getc(stdin);
	*len = 0;
	while (1) {
		c = getc(stdin);
		ungetc(c, stdin);

		if (c == '\n')
			return;

		if (!scanf("%2x", buffer++)) 
			return;

		(*len)++;
	}
}


async_char_waiting()
{
	union REGS inregs, outregs;

	inregs.h.ah = 0x44;	/* I/O control function */
	inregs.h.al = 0x06;	/* Input status */
	inregs.x.bx = async_handle;

	intdos(&inregs, &outregs);

	return outregs.x.ax != 0;
}


async_output_ready()
{
	union REGS inregs, outregs;

	inregs.h.ah = 0x44;	/* I/O control function */
	inregs.h.al = 0x07;	/* Output status */
	inregs.x.bx = async_handle;

	intdos(&inregs, &outregs);

	return outregs.x.ax != 0;
}


async_get_char()
{
	char c;

	if (read(async_handle, &c, 1) != 1) {
		printf("No data waiting for read!!!\n");
		return 0x7e;
	}

	return c;
}

async_put_char(c)
char c;
{
	if (write(async_handle, &c, 1) != 1)
		printf("Write failed!!!\n");
}