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

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

/* step 1 : COM port of VXCard connects to COM1 of PC       */

/* step 2 : run DEMO4.EXE	                                   */

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

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



#include "..\VxCard.h"

#include "..\RS232.H"



// #define COM1	  0x3f8

// #define COM2	  0x2f8

// #define COM3	  0x3e8

// #define COM4	  0x2e8



WORD wIrq,wConfigAddr, wConfigAddr_0, wConfigAddr_1;



int main()

{

   int   iRet, i, char_k, char_a, char_t;

   char  cSelectPort, szCardName[ 20 ];

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

   WORD  wPortAddr[ 4 ], PC_SendPolling, VXC_SendPolling, PC_ReceivePolling, VXC_ReceivePolling;

   DWORD dwCardType;



   clrscr();

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

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

   printf("\nThrer are %d VXCard( s ) in this PC; Ret=%d",wCardNo, wRetVal);



   if ( wCardNo == 0 ) return 5;



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

    

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

   {

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

      VXC_GetPortInfo( i, *( &wPortAddr ) );

      VXC_GetCardName( dwCardType, *( &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;

   }



   if ( getch() == 49 )

   {

      i = 1;

   }

   

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

   VXC_GetPortInfo( i, *( &wPortAddr ) );

   VXC_GetCardName( dwCardType, *( &szCardName ) );

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

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



   if ( getch() == 49 )

   {

      cSelectPort = 0;

   }

   else

   {

      cSelectPort = 1;   

   }   

   

   /* step 3: select Port1 or Port2 */

   iRet = VXC_OpenCom( wPortAddr[ cSelectPort ], 115200L, "8N1", &wCOM_Handle ); /* 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", &wCOM_Handle ); /* RS_232 of PC-COM1 = 115200 */



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

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



   char_k = 0x55; char_a = 0xaa;

   

   for ( ; ; )

   {

      PC_SendPolling = 0; VXC_SendPolling = 0; PC_ReceivePolling = 0; VXC_ReceivePolling = 0;

        

      for ( ; ; )

      {

         if ( PC_SendPolling < 32 )

         {

            if ( 0 == VXC_SendChar( 1, char_k, 2 ) ) /* PC-Send=polling */

               PC_SendPolling++;

         }



         if ( 0 == VXC_ReceiveChar( 0, &char_t, 2 ) ) /* VxCard-Receive = polling */

         { 

            if ( char_t != char_k ) 

            {

               printf( "[ Err0, %2x, %2x ]", char_t, char_k );

               return 0;

            }

				

            printf( "[ VXC_Receive = %d ]", VXC_ReceivePolling );

            VXC_ReceivePolling++;

         }	 

	 

         if ( VXC_SendPolling < 32 ) 

         {

            if ( 0 == VXC_SendChar( 0, char_a, 2 ) ) /* VxCard-Send = polling */

               VXC_SendPolling++;

         }



         if ( 0 == VXC_ReceiveChar( 1, &char_t, 2 ) ) /* PC-Receive = polling */

         { 

            if ( char_t != char_a ) 

            {

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

               return 0;

            }



            printf( "[ PC_Receive = %d ]", PC_ReceivePolling );

            PC_ReceivePolling++;

         }	 

			

         if ( ( PC_ReceivePolling >= 32 ) && ( VXC_ReceivePolling >= 32 ) )

            break;

	    

         printf( "\n" ); 

      }



      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;

}



