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

/* demo 2 : Test 2 ports of 1 VXCard ( 1 - char )      */

/* step 1 : Port1 connect to Port2 	                           */

/* step 2 : run DEMO2.EXE				                       */

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



#include "..\VxCard.h"

#include "..\RS232.H"



WORD wConfigAddr;



int main()

{

   int   iRet, t, i, char_k, char_k2, char_a, char_a2, k;

   char  szCardName[ 20 ];

   WORD  wCardNo, wCardSwitchID, wNumberOfPorts, wUartStride, wRetVal, wIrq, wCOM_Handle;

   DWORD wCardType;

   WORD  wPortAddr[4];

   WORD  wT=62000;

  



   /* step 1: find address-mapping of PIO/PISO 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 );

      

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

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

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

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

   }



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



	

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

/*  Send / Receive  Char                                                */

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

/* step 3: select Port0, Port1 */

	

   k = 0;

   iRet = VXC_OpenCom( wPortAddr[ k ], 115200L, "8N1", &wCOM_Handle );

   

   if ( iRet == 0 ) printf( "\n\n( 1 ) Open Port1: %x OK", wPortAddr[ k ] );

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



   iRet = VXC_OpenCom( wPortAddr[ k + 1 ], 115200L, "8N1", &wCOM_Handle );

   

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

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



   char_k = 0x55; char_a = 0xaa; t = 0;



   // Clear Rx buffer

   while ( VXC_ReceiveChar( 0, &char_k2, wT ) == 0 ); // Got data

   while ( VXC_ReceiveChar( 1, &char_k2, wT ) == 0 );



   for ( ; ; )

   {

      if ( 0 != VXC_SendChar( 0, char_k, wT ) ) break; // Error

	  

      if ( 0 != VXC_ReceiveChar( 1, &char_k2, wT ) ) break;

	  

      if ( 0 != VXC_SendChar( 1, char_a, wT ) ) break;

	  

      if ( 0 != VXC_ReceiveChar( 0, &char_a2, wT ) ) break;



      printf( "\n( %2x, %2x )  ( %2x, %2x )", char_k, char_k2, char_a, char_a2 );



      if ( char_k != char_k2 ) break;



      if ( char_a != char_a2 ) break;



      char_k++; char_k = char_k & 0xff;

      char_a++; char_a = char_a & 0xff;

	  

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

      t++;

   }



   VXC_DriverClose();

   return  0;

}

