
/**************************************************************************/
/*  Program	: q4500.C						  */
/*  Description	: Sends	a string from COM1 to COM2 then	reads it back	  */
/*		  from COM2 and	displays it on the screen. It use direct  */
/*		  register control.					  */
/*									  */
/*  (RS485)				COM1		      COM2	  */
/*  Jumper				DATA0+	<------>      DATA1+	  */
/*  Setting &	  JP1  - 485		DATA0-	<------>      DATA1-	  */
/*  Signal								  */
/*  Wiring								  */
/*									  */
/**************************************************************************/

#include <dos.h>
#include <io.h>
#include <stdio.h>
#include <conio.h>
#define	TIME_OUT	1000000
static	int base0=0x3f8;		/* Port	1 base address (COM1) */
static	int base1=0x2f8;		/* Port	2 base address (COM2) */
static	char rec[20];			/* Receive buffer */
static	char cmd[20];			/* Command buffer */

void main()
{
	int i;
	int timeout;				/* counter for timeout */
	char flag;

	/* Set up Port 1 (COM1)	*/
	outport((base0+2),0x01);		/* enable FIFO */
	outp(base0+3,0x80);			/* set DLAB=1 */
	outp(base0  ,0x01); outp(base0+1,0x0);	/* set buad = 115200 */
	outp(base0+3,0x03);			/* set data=8 stop=1 no	parity */
	outp(base0+1,0x00);			/* disable COM1	interrupt */


	/* Set up Port 2 (COM2)	*/
	outport((base1+2),0x01);		/* enable FIFO */
	outp(base1+3,0x80);			/* set DLAB=1 */
	outp(base1  ,0x01); outp(base1+1,0x0);	/* set buad = 115200 */
	outp(base1+3,0x03);			/* set data=8 stop=1 no	parity */
	outp(base1+1,0x00);			/* disable COM2	interrupt */


	printf("\nEnter string (max 15 char) or Q to quit:");
	gets(cmd);

	while (cmd[0] != 'q' &&	cmd[0] != 'Q')
	{
		/* Send	string on Port 1 (COM1)	*/
		i=0;
		cmd[strlen(cmd)] = 0x0d;
		flag=1;
		while (flag)
		{
			outportb(base0,cmd[i]);		/* Send	data */
			if (cmd[i] == 0x0d)
				flag=0;
			i++;
		}

		/* Receive data	on Port	2 (COM2) */
		i=0;
		flag=1;
		timeout=TIME_OUT;
		while (flag)
		{
			/* Check for received data on port 2 */
			if ((inportb(base1+5) &	1) !=0)
			{
				rec[i]=inportb(base1);	/* Receive data	*/
				if (rec[i] == 0x0d)
				{
					rec[i+1]='\0';
					flag=0;
					printf("\nReceived data : %s\n",rec);
				}
				i++;
			}
			else
			{	/* Check timeout */
				timeout--;
				if (timeout == 0)
				{
					flag = 0;
					printf("\nTimeout error\n");
				}
			}
		}	/* End of receive data while() */

		printf("\nEnter string (max 15 char) or Q to quit:");
		gets(cmd);
	}	/* End of "Enter string" while() */
}	/* End of main() */

 
