/* ----------------------------------------------------------- */

/* demo 3 : Test 2 RS-422/485 ports of 1 VXC-142 (1-char)      */

/* step 1 : Port1 connect to Port2 	                           */

/* step 2 : run DEMO3.EXE				                       */

/* ----------------------------------------------------------- */

#include "..\PIO.H"

#include "..\VxCard.h"



WORD wBase, wIrq, wPortAddr1, wPortAddr2;

WORD wT=62000;



int main()

{

	int   t,tt,i,j,ii,jj;

	int   iRet;

	WORD  wSubVendor,wSubDevice,wSubAux,wSlotBus,wSlotDevice;

	WORD wBoards,wRetVal,t1,t2,t3,t4,t5;



	clrscr();

	/* step 1: find address-mapping of PIO/PISO cards		  */

	wRetVal=PIO_DriverInit(&wBoards,0x61,0x00,0x00);  /* for VXC-142  */

	printf("\nThere are %d VXC-142 Cards in this PC, Ret=%d",wBoards, wRetVal);

	if (wBoards==0) return 1;



	printf("\n--------------- The Configuration Space ---------------");

	for(i=0; i<wBoards; i++)

	{

		PIO_GetConfigAddressSpace(i,&wBase,&wIrq,&wSubVendor,&wSubDevice,

								&wSubAux,&wSlotBus,&wSlotDevice);

		printf("\nCard_%d: wBase=%x,wIrq=%x,subID=[%x,%x],SlotID=[%x,%x]",i,wBase,wIrq,wSubVendor,wSubDevice,wSlotBus,wSlotDevice);

		printf(" --> ");

		ShowPioPiso(wSubVendor,wSubDevice,wSubAux);

	}



	PIO_GetConfigAddressSpace(0,&wBase,&wIrq,&t1,&t2,&t3,&t4,&t5); /* card 0 */



	/* step 2: enable all D/I/O port				  */

	outportb(wBase,1);				   /* enable D/I/O */



	Vxc_SetConfig(wBase, VXC_142_CONFIG);

	Vxc_SetSpeedMode(wBase, 0, VXC_LOW_SPEED);		/* high/low speed control */

	Vxc_SetSpeedMode(wBase, 1, VXC_LOW_SPEED);		/* high/low speed control */

	wPortAddr1 = wBase + VXC_PORT1;

	wPortAddr2 = wBase + VXC_PORT2;



	/* step 3: select Port0 ,Port1   */

	iRet=Vxc_OpenCom(wPortAddr1, 115200L, "8N1");

	if (iRet==0) printf("\n\n(1) Open Port1:%x OK", wPortAddr1);

	else         printf("\niRET=%d",iRet);



	iRet=Vxc_OpenCom(wPortAddr2, 115200L, "8N1");

	if (iRet==0) printf("\n\n(1) Open Port2:%x OK", wPortAddr2);

	else         printf("\niRET=%d",iRet);



	i=0x55; j=0xaa; tt=0;



	// Clear Rx buffer

	while (Vxc_ReceiveChar(wPortAddr1, &ii, wT)==0) ; // Got data

	while (Vxc_ReceiveChar(wPortAddr2, &ii, wT)==0) ;



	for (;;)

	{

		if (0 != Vxc_SendChar(wPortAddr1, i, wT)     ) break;  // Error

		if (0 != Vxc_ReceiveChar(wPortAddr2, &ii, wT)) break;

		if (0 != Vxc_SendChar(wPortAddr2, j, wT)     ) break;

		if (0 != Vxc_ReceiveChar(wPortAddr1, &jj, wT)) break;



		printf("\n(%2x, %2x) (%2x,%2x)",i,ii,j,jj);



		if (i!=ii) break;

		if (j!=jj) break;



		i++; i=i&0xff;

		j++; j=j&0xff;

		if (kbhit()!=0) {getch(); break;}

		tt++;

	}



	PIO_DriverClose();

	return 0;

}

