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

/* demo 3 : Test RS-232/422 port of two VXC-182i boards (32-chars) */

/*          ** For full-duplex communication test.             */

/*          ** 2-Wire half-duplex RS-485 will not work.        */

/* step 1 : Connect  Port1 of Card0  to Port1 of card1         */

/* step 2 : run DEMO4.EXE				                       */

/* NOTE   : can test 115.2K max.			                   */

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



#include "..\PIO.H"

#include "..\VxCard.h"



WORD wBase,wIrq,wBase0,wBase1;

WORD wT=32000;



int main()

{

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

	int   iRet;

	WORD  wSubVendor,wSubDevice,wSubAux,wSlotBus,wSlotDevice;

	WORD  wBoards,wRetVal,t1,t2,t3,t4,t5,t0,tt0,tt1;

	WORD  wPortAddr0, wPortAddr1;



	clrscr();

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

	wRetVal=PIO_DriverInit(&wBoards,0x161,0x01,0x00);  /* for VXC-182I */

	printf("\nThrer are %d VXC-182i 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,&wBase0,&wIrq,&t1,&t2,&t3,&t4,&t5); /* card 0 */

	PIO_GetConfigAddressSpace(1,&wBase1,&wIrq,&t1,&t2,&t3,&t4,&t5); /* card 1 */



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

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

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



	Vxc_SetConfig(wBase0, VXC_142_CONFIG);

	Vxc_SetConfig(wBase1, VXC_142_CONFIG);

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

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



	/* step 3: select Port1  */

	wPortAddr0=wBase0+VXC_PORT1; /* RS-422 of card 0 */

	wPortAddr1=wBase1+VXC_PORT1; /* RS_422 of card 1 */



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

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

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



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

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

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



	getch();



	i=0x55; j=0xaa;

	for (;;)

	{

		t0=0; t1=0; tt0=0; tt1=0;

		for (;;)

		{

			if ( t0<32 ) {

				if ( 0 == Vxc_SendChar(wPortAddr0, i, 2) )

					t0++;

			}



			if ( t1<32 ) {

				if ( 0 == Vxc_SendChar(wPortAddr1, j, 2) )

					t1++;

			}



			if ( 0 == Vxc_ReceiveChar(wPortAddr1, &k, 2) ){

				if (k!=i) {

					printf("[Err1, %2x, %2x]", k, i);

					return 0;

				}

				printf("[tt1=%d]",tt1);

				tt1++;

			}



			if ( 0 == Vxc_ReceiveChar(wPortAddr0, &k, 2) ){

				if (k!=j) {

					printf("[Err1, %2x, %2x]", k, j);

					return 0;

				}

				printf("[tt0=%d]",tt0);

				tt0++;

			}



			if ((tt0>=32) && (tt1>=32))

				break;

		}

		i++; i=i&0xff;

		j++; j=j&0xff;



		if (kbhit()!=0) {

			getch();

			break;

		}



		printf("\n[%2x, %2x]",i,j);

	}



	PIO_DriverClose();

	return 0;

}

