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

/* demo 3 : Test RS-232/422 port of two VXCard (32-chars)  */

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

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

/* step 2 : run DEMO3.EXE				                        */

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

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



#include "..\VxCard.h"

#include "..\RS232.H"



WORD wConfigAddr, wConfigAddr_0, wConfigAddr_1;



int main()

{

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

   char  szCardName[ 20 ];

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

   DWORD wCardType;

   WORD  wPortAddr[ 4 ], wPortAddr_0[ 4 ], wPortAddr_1[ 4 ];

   WORD  t0, t1, tt0, tt1;

   WORD  wT = 2;



   clrscr();

   /* step 1: find address-mapping of VXCards */

   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 */ 

   }

   

   VXC_GetCardInfo( 0, &wConfigAddr_0, &wCardType, &wCardSwitchID, &wNumberOfPorts, &wUartStride, &wIrq ); /* card 0 */

   VXC_GetCardInfo( 1, &wConfigAddr_1, &wCardType, &wCardSwitchID, &wNumberOfPorts, &wUartStride, &wIrq ); /* card 1 */

   

   /* step 3: select Port  */

   VXC_GetPortInfo( 0, *( &wPortAddr_0 ) ); /* RS-232 of card 0 */

   VXC_GetPortInfo( 1, *( &wPortAddr_1 ) ); /* RS-232 of card 1 */  



   k = 0;

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



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

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

   

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

  

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

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



   getch();

   char_k = 0x55; char_a = 0xaa;

   

   for ( ; ; )

   {

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

      

      for ( ; ; )

      {

         if ( t0 < 32 ) 

         {

            if ( 0 == VXC_SendChar( 0, char_k, wT ) )

               t0++;

         }



         if ( t1 < 32 ) 

         {

            if ( 0 == VXC_SendChar( 1, char_a, wT ) )

               t1++;

         }



         if ( 0 == VXC_ReceiveChar( 1, &char_k2, wT ) )

         {

            if ( char_k2 != char_k ) 

            {

               printf( "[ Err1, %2x, %2x ]", char_k2, char_k );

               return 0;

            }



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

            tt1++;

         }



         if ( 0 == VXC_ReceiveChar( 0, &char_k2, wT ) )

         {

            if ( char_k2 != char_a ) 

            {

               printf( "[ Err1, %2x, %2x ]", char_k2, char_a);

               return 0;

            }



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

            tt0++;

         }



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

               break;

      }



      char_k++; char_k = ( char_k & 0xff );

      char_a++; char_a = ( char_a & 0xff );



      if ( kbhit() != 0 ) 

      {

         getch();

         break;

      }



      printf( "\n[ %2x, %2x ]", char_k, char_a );

   }



   VXC_DriverClose();

   return 0;

}

