

/**************************************************************************/
/*  Program	:example3.c						  */
/*  Description	:Read digital input data from ADAM-4050	then write it	  */
/*		 to ADAM-4050 digital output				  */
/*  ADAM-4050	: address 01						  */
/*									  */
/*  (RS485)			   ADAM-4500(COM1)	  ADAM-4050	  */
/*  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	2500000
static	int base0=0x3f8;
static	char rec[30];
static	char cmd[30]={'$','0','1','6',0x0d};

void main()
{
static	unsigned long int timeout,timeout1;
static	char flag,aflag;
static int i,j,k;
	outport((base0+2),0xc9);	/*	   enable COM1 FIFO */

	outp(base0+3,0x80);			/* set DLAB=1 */
	outp(base0  ,0x0C); outp(base0+1,0x0);	/* set buad = 9600   */
	outp(base0+3,0x03);			/* set data=8 stop=1 no	parity */
	outp(base0+1,0x00);			/* disable COM1	interrupt */

	while(1)
	{
		i=0;
		flag=1;
		while (flag)
		{
			outportb(base0,cmd[i]);		/* Send	data */
			while( (inportb(base0+5)&0x40)	!=0x40);
			if (cmd[i] == 0x0d)
				flag=0;
			i++;
		}


		i=0;
		flag=1;
		timeout=TIME_OUT;
		while (flag)
		{
			/* Check receiver data ready? */
			if ((inportb(base0+5) &	1) !=0)
			{
				rec[i]=inportb(base0);	/* Receive data	*/
				if (rec[i] == 0x0d)
				{
					outportb(base0,'#');	     /*	Send data */
					outportb(base0,'0');	     /*	Send data */
					outportb(base0,'1');	     /*	Send data */
					outportb(base0,'0');	     /*	Send data */
					outportb(base0,'0');	     /*	Send data */
					outportb(base0,rec[3]);		/* Send	data */
					outportb(base0,rec[4]);		/* Send	data */
					outportb(base0,0x0d);	      /* Send data */

					timeout1=TIME_OUT;
					aflag=1;
					while (aflag)
					{
						/* Check receiver data ready? */
						if ((inportb(base0+5) &	1) !=0)
						{
							if (inportb(base0) == 0x0d)
								aflag=0;
						}
						else
						{
							timeout1--;
							if (timeout1 ==	0)
							{
								aflag =	0;
								printf("\nTimeout1 error\n");
							}
						}
					}

					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");
				}
			}
		}
	}
}

