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

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

/*          Note, a I-7520 RS-232/485 converter is required.   */

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

/* step 2 : run DEMO2.EXE			                   	       */

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

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



#include "..\VxCard.h"

#include "..\RS232.H"



WORD wConfigAddr;

long TIMEOUT=32000L, speed;



int main()

{

   int   iRet, i, j, k;

   char  c, szCardName[ 20 ], cCmd[ 50 ];

   WORD  wCardNo, wCardSwitchID, wNumberOfPorts, wUartStride, wRetVal, wIrq, wCOM_Handle, wPortAddr[4];

   DWORD wCardType;





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

   wRetVal=VXC_DriverInit( &wCardNo ); /* for VxCard */

   printf( "\n\nThere are %d  VXCard( s ) in  this  PC, RetVal = %d", wCardNo, wRetVal );



   if ( wCardNo == 0 ) return 5;



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

	

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

   {

      VXC_GetCardInfo( i, &wConfigAddr, &wCardType, &wCardSwitchID, &wNumberOfPorts, &wUartStride, &wIrq );

      VXC_GetPortInfo( i, *( &wPortAddr ) );

	  VXC_GetCardName( wCardType, *( &szCardName ) );

      printf( "\nCardNo_%d: %s___CardID = %x, ConfigAddress = %x\n", i, szCardName, wCardSwitchID, wConfigAddr );

      

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

      /*  enable all D/I/O port                                               */

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

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

      speed=9600L;  

   }



   printf(" \nSelect your card No --> ");



   if ( getch() == 48 )

   {

      i = 0;

   }

   else

   {

      i = 1;

   }

   

   VXC_GetCardInfo( i, &wConfigAddr, &wCardType, &wCardSwitchID, &wNumberOfPorts, &wUartStride, &wIrq );

   VXC_GetPortInfo( i, *( &wPortAddr ) );

   VXC_GetCardName( wCardType, *( &szCardName ) );

   printf( "%s\n", szCardName );

   	  

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

/*  select Port1 or Port2                                               */

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

      

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

      c = getch();

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

	  

      if ( c == '1' )

      {

         k = 0;		 

         iRet = VXC_OpenCom( wPortAddr[ k ], speed, "8N1", &wCOM_Handle ); /* default */

	  }	  

      else

      {

         k = 1;	 

         iRet = VXC_OpenCom( wPortAddr[ k ], speed, "8N1", &wCOM_Handle ); /* default */

	  }



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

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

	  

	  printf( ",  wPortAddr  = %x", wPortAddr[ k ] );

      strcpy( cCmd, "$012" );

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

      iRet = VXC_SendCmd( *( &wCOM_Handle ), cCmd, 0, TIMEOUT );

	  

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

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

	  

      iRet = VXC_ReceiveCmd( *(&wCOM_Handle), cCmd, 0, TIMEOUT );



      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( *( &wCOM_Handle ), cCmd, 0, TIMEOUT );

	  

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

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

	  

      iRet = VXC_ReceiveCmd( *( &wCOM_Handle ), cCmd, 0, TIMEOUT );

	  

      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( *( &wCOM_Handle ), cCmd, 0, TIMEOUT );

	  

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

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

	  

      iRet = VXC_ReceiveCmd( *( &wCOM_Handle ), cCmd, 0, TIMEOUT );

	  

      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();

      VXC_DriverClose();

      return  0;

}

