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

/* demo 5 : test RS-232 port (single VXC-112A + PC-COM1)       */

/* step 1 : RS-232 port of vxc112A connect to COM1 of PC       */

/* step 2 : run DEMO5.EXE	                                   */

/* NOTE   : using polling  (TxD==>RxD)			               */

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



#include "..\PIO.H"

#include "..\VxCard.h"



#define COM1	  0x3f8

#define COM2	  0x2f8

#define COM3	  0x3e8

#define COM4	  0x2e8



WORD wBase,wIrq,wBase0,wBase1;

WORD wT=32000;



int main()

{

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

    char  c, bSpeedMode;

    char  cCmd[50];

    int   iRet,iComPort;

    DWORD dd;

	WORD  wSubVendor,wSubDevice,wSubAux,wSlotBus,wSlotDevice;

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



	clrscr();

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

	wRetVal=PIO_DriverInit(&wBoards,0x1D61,0x00,0x00);  /* for VXC-112A */

	printf("\nThrer are %d VXC-112A 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);



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

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

    Vxc_SetConfig(wBase0, VXC_112A_CONFIG); /* for VXC-112A */

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

    Vxc_SetSpeedMode(wBase0,1,VXC_LOW_SPEED);		   /* low speed    */



    printf("Please select 1:Port1, other keys:Port2\n");

    c = getch();

	/* step 3: select Port1 or Port2 			   */

	wA0 = wBase0 + (c=='1'? VXC_PORT1 : VXC_PORT2);



    iRet=Vxc_OpenCom(wA0, 115200L, "8N1");   /* RS-232 of VXC-112A=115200	   */

    if (iRet==0) printf("\n\n(1) Open card_0 OK");

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



	iRet=Vxc_OpenCom(COM1, 115200L, "8N1");   /* RS_232 of PC-COM1=115200	   */

    if (iRet==0) printf("\n\n(1) Open PC COM OK");

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



    i=0x55; j=0xaa;

    for (;;)

    {

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

        for (;;)

	   {

			if ( t0<32 ) {

				if ( 0 == Vxc_SendChar(COM1, i, 2) ) /* PC-send=polling */

					t0++;

			}



			if ( t1<32 ) {

				if ( 0 == Vxc_SendChar(wA0, j, 2) ) /* vxc-112A-send=polling */

					t1++;

			}



			if ( 0 == Vxc_ReceiveChar(wA0, &k, 2) ){ /* vxc-112A-receive=polling */

				if (k!=i) {

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

					return 0;

				}

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

				tt1++;

			}

			if ( 0 == Vxc_ReceiveChar(COM1, &k, 2) ){ /* pc-receive=polling */

				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;

}



