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

/* demo 2 : control the 7000 modules in RS-485/RS-232 port     */

/* step 1 : connect a 7000 module, address=0x01 to RS_485/232  */

/* step 2 : run DEMO2.EXE			                   	       */

/* step 3 : test $012, $01M, $01F commands		               */

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



#include "..\PIO.H"

#include "..\VxCard.h"



WORD wBase,wIrq;

long TIMEOUT=32000L, speed;



int main()

{

	int   i,j;

	char  c;

	char  cCmd[50];

	int   iRet,iComPort;

	DWORD dd;

	WORD  wSubVendor,wSubDevice,wSubAux,wSlotBus,wSlotDevice;

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

	WORD  wPortAddr;



	clrscr();



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

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

	printf("\nThere are %d VXC-142i 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);



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

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

	Vxc_SetConfig(wBase, VXC_142_CONFIG); /* for VXC-142/142i/182i */



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

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

	speed=9600L;



	/* step 3: select Port1 or Port2 			  */

	printf("\nSelect Port1=1, Port2=other key");

	c=getch();

	if (c=='1') wPortAddr = wBase+VXC_PORT1;

	else	    wPortAddr = wBase+VXC_PORT2;



	printf("\nspeed=%ld",speed);

	iRet=Vxc_OpenCom(wPortAddr, speed, "8N1");	 /* default */

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

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



	printf(", wPortAddr=%x",wPortAddr);

	strcpy(cCmd,"$012");

	printf("\n(2) Send=$012, ");

	iRet=Vxc_SendCmd(wPortAddr, cCmd, TIMEOUT, 0);

	if (iRet==0) printf("SEND OK");

	else         printf("iRET=%d",iRet);



	iRet=Vxc_ReceiveCmd(wPortAddr, cCmd, TIMEOUT, 0);

	if (iRet==0) printf(" , Receive=%s",cCmd);

	else if (iRet==VXC_Timeout) printf(" , Receive=Timeout");

	else if (iRet==VXC_ChecksumError) printf(" , Receive=chksum error");



	strcpy(cCmd,"$01M");

	printf("\n(3) Send=$01M, ");

	iRet=Vxc_SendCmd(wPortAddr, cCmd, TIMEOUT, 0);

	if (iRet==0) printf("SEND OK");

	else         printf("iRET=%d", iRet);



	iRet=Vxc_ReceiveCmd(wPortAddr, cCmd, TIMEOUT, 0);

	if (iRet==0) printf(" , Receive=%s",cCmd);

	else if (iRet==VXC_Timeout) printf(" , Receive=Timeout");

	else if (iRet==VXC_ChecksumError) printf(" , Receive=chksum error");



	strcpy(cCmd,"$01F");

	printf("\n(4) Send=$01F, ");

	iRet=Vxc_SendCmd(wPortAddr, cCmd, TIMEOUT, 0);

	if (iRet==0) printf("SEND OK");

	else         printf("iRET=%d",iRet);



	iRet=Vxc_ReceiveCmd(wPortAddr, cCmd, TIMEOUT, 0);

	if (iRet==0) printf(" , Receive=%s",cCmd);

	else if (iRet==VXC_Timeout) printf(" , Receive=Timeout");

	else if (iRet==VXC_ChecksumError) printf(" , Receive=chksum error");

	getch();

	//printf("\nwBase=%x",wBase);

	PIO_DriverClose();

	return 0;

}

