⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 di_int_scan.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            if ( ( pDev->pDiIntSupportedChannel[i] & ( 0x1<<j ) ) != 0 )
            {
               printf(" %d  ", i * 8 + j );
            }
         }
         
      }
   }
   
   return TRUE;
}
/**********************************************************************
* Function:   Setup DI Interrupt function
*             
* Paramaters: pDevInfo[in] -- pointer to DEV_INFO struct.
*             
* return:     TRUE for SUCCESS, FALSE for failure.             
**********************************************************************/
BOOL SetupDiInterrupt( LPDEV_INFO pDev )
{
   ULONG chan;
   ULONG scanStart;
   ULONG scanCount;

   BYTE * pDiIntEnabledChannel;

   DWORD dwErrCde;
   
   if ( pDev->lDevHandle == NULL )
   {
      PrintErrorMsg(NULL,"Invalid device handle!");
      return FALSE;
   }
   
   printf("\n\n**********************************************************");
   printf("\nSetup DI Interrupt, Please Input:");
   
   //Get channel number which DI Interrupt function will be enabled.
   printf("\nChannel Number( to enable its DI Interrupt ): ");
   scanf("%d", &chan);
   
   //Is a valid channel number
   if ( chan >= pDev->ulDiChannelCount )
   {
      PrintErrorMsg(NULL,"Invalidate Channel Number(Channel Number is ZERO base)!");
      return FALSE;
   }
   //Does channel user selected support DI Interrupt?
   if ( ( pDev->pDiIntSupportedChannel[ chan / 8 ] & (0x1 << (chan %8) ) ) == 0 )
   {
      PrintErrorMsg(NULL,"The channel selected doesn't support DI Interrupt function!");
      return FALSE;
   }
   
   //Get start port which will be scaned when DI Interrupt occurs.
   printf("\nStart port to scan: ");
   scanf("%d", &scanStart);
   
   //Is a valid port number
   if ( scanStart >= pDev->ulDiPortCount )
   {
      PrintErrorMsg(NULL,"Invalid port number(Port number is ZERO based)!");
      return FALSE;
   }
   
   //Get port count scaned
   printf("\nPort count to scan: ");
   scanf("%d", &scanCount);
   
   //Is a valid port count
   if ( ( scanCount > pDev->ulDiPortCount )
      || ( scanStart + scanCount > pDev->ulDiPortCount ) )
   {
      char msg[64];
      scanCount = pDev->ulDiPortCount - scanStart;
      sprintf(msg,"Invalid port count. Port count will be set to %d", scanCount );
      PrintErrorMsg(NULL, msg);
   }
   
   //Allocate memory for CFG_DiInterruptTriggerOnRising. 
   //pDiIntEnabledChannel has the same size and meanings with
   //pDev->pDiIntSupportedChannel.
   pDiIntEnabledChannel = (BYTE*)malloc( pDev->ulDiPortCount );
   if ( pDiIntEnabledChannel == NULL )
   {
      PrintErrorMsg(NULL,"Not enough memory!");
      return FALSE;
   }
   
   memset( pDiIntEnabledChannel, 0, pDev->ulDiPortCount );
      
   // Set the bit to 1 corresponding with the user selected channel
   pDiIntEnabledChannel[ chan / 8 ] = 0x1 << (chan % 8);
   
   // set the channel's DI Interrupt to "Rising trigger"
   // It is not necessary if you had configured the hardware with
   // Advantech Device Manager.
   dwErrCde = DRV_DeviceSetProperty( 
                  pDev->lDevHandle, 
                  CFG_DiInterruptTriggerOnRisingEdge,
                  pDiIntEnabledChannel,
                  pDev->ulDiPortCount );

   //Free the memory 
   free( pDiIntEnabledChannel );

   if ( dwErrCde != SUCCESS )
   {
      ErrorHandler(dwErrCde);
      return FALSE;
   }
   
   // Step 6: Enable DI Interrupt event by AdxDioEnableEventAndSpecifyDiPorts
   dwErrCde = AdxDioEnableEventAndSpecifyDiPorts( 
                  pDev->lDevHandle, 
                  ADS_EVT_DI_INTERRUPT0 + chan,
                  scanStart,
                  scanCount );
   if ( dwErrCde != SUCCESS )
   {
      ErrorHandler(dwErrCde);
      return FALSE;
   }

   //Save the user setting
   pDev->ulDiIntEnabledChannel = chan;
   pDev->ulScanStart           = scanStart;
   pDev->ulScanCount           = scanCount;
   
   return TRUE;
}
/**********************************************************************
* Function:   CheckDiInterrupt
*             Check DI Interrupt event and Read DI Interrupt Data
* Paramaters: pDev[in] -- pointer to device information
*             
* return:     none             
**********************************************************************/
BOOL CheckDiInterrupt( LPDEV_INFO pDev )
{
   PT_CheckEvent ptCheckEvent;
   DWORD         dwEventCount;
   BYTE *        pDiIntData = NULL; 
   USHORT        usEventType;
   DWORD         dwErrCde;
   ULONG         i;
   
   if ( pDev->lDevHandle == NULL )
   {
      PrintErrorMsg( NULL, "Invalid device handle!");
      return FALSE;
   }
   
   //Allocate memory to read DI Interrupt Data
   if ( pDev->ulScanCount != 0 )
   {
      pDiIntData = (BYTE*)malloc( pDev->ulScanCount );
      if ( pDiIntData == NULL )
      {
         PrintErrorMsg(NULL,"Not enough memory!");
         return FALSE;
      }
   }
   // Check event by DRV_CheckEvent in while loop, exit at pressing
   // any key. If DRV_CheckEvent returns successful, increases  dwEventCount
   ptCheckEvent.EventType = &usEventType;
   ptCheckEvent.Milliseconds = 300;
   
   dwEventCount = 0;        
   
   printf("\nWaiting DI interrupt signal. Press any key to exit the waiting loop....\n");
   while (!kbhit())
   {
      dwErrCde = DRV_CheckEvent( pDev->lDevHandle, &ptCheckEvent );
      
      //Check function calling successful 
      if (dwErrCde == SUCCESS)
      {
         //Check desired event generation
         if ( usEventType == ADS_EVT_DI_INTERRUPT0 + pDev->ulDiIntEnabledChannel )
         {   
            dwEventCount++;
            printf("Event Count = %lu", dwEventCount);
            
            //Read DI Interrupt Data
            if ( pDev->ulScanCount != 0 )
            {
               AdxDioGetLatestEventDiPortsState(
                  pDev->lDevHandle, 
                  usEventType, 
                  pDiIntData,
                  pDev->ulScanCount );
               
               //Display DI Interrupt Data. Only display the first 10 data max.
               printf(", Event Data: ");
               for( i = 0; i < pDev->ulScanCount && i < 10; ++i )
               {
                  printf("%x ", pDiIntData[i] );
               }
            }
            printf("\r");
         }
      }
   }
   
   printf("\n");

   if ( kbhit() )
   {
      getch();
   }

   if ( pDiIntData != NULL )
   {
      free( pDiIntData );
   }
   return TRUE;
   
}

/**********************************************************************
* Function:   FreeResource
*             Free all resource allocated.
* Paramaters: pDev[in] -- pointer to device information
*             
* return:     none             
**********************************************************************/
void FreeResource( LPDEV_INFO pDev )
{
   if ( pDev->lDevHandle  == NULL )
   {
      return;
   }
   
   //Disable the DI Interrupt which had been enabled.
   if ( pDev->ulDiIntEnabledChannel != -1 )
   {
       AdxDioDisableEvent( 
          pDev->lDevHandle, 
          ADS_EVT_DI_INTERRUPT0 + pDev->ulDiIntEnabledChannel );
       
       pDev->ulDiIntEnabledChannel = -1;
   }
   
   if ( pDev->pDiIntSupportedChannel != NULL )
   {
      free( pDev->pDiIntSupportedChannel );
      pDev->pDiIntSupportedChannel = NULL;
   }
   
   //Close device handle
   DRV_DeviceClose( &pDev->lDevHandle );
   pDev->lDevHandle = NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -