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

📄 cycpmcheck.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
//----------------------------------------------------------------------------------
// Module:  FdiCheck
//          Fast DI transferring and using polling check the transferring status.
//         
//          Notice, 
//          1. It will be carefull for not make driver interrupted so frequency 
//             that will hange the system for querring too many IRQ.
//          2. Smaller input buffer and higher sampling rate will generate interrupt 
//             more rapidly.
//          3. If the sampling rate is very slow, then it will need to wait more 
//             meanful duration for Device to fill data to HF.
//----------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\Include\driver.h"

#define ESC 0x1b

//******************************
//* Local function declaration 
//******************************
void ErrorHandler(DWORD dwErrCde);
void ErrorThenStop(long*, DWORD);

void main()
{
  DWORD   dwErrCde, dwData, dwCount;
  ULONG   lDevNum;
  long    lDrvHandle;
  ULONG   dwFdiStatus, dwRetrieved,  i;
  int     iRunning, iNextChecking;

  ULONG   dwBuffer[8192*2];      //Fast DI buffer allocation
  USHORT  wCyclic = 1;           //Cyclic sampling(1) / non-cyclic sampling(0)
  ULONG   dwPmValue = 0x0;       //PatternMatch value

  ULONG   dwDoBuffer[5000];      //Fast DO buffer allocation
  USHORT  wDoCyclic = 1;         //Cyclic sampling(1) / non-cyclic sampling(0)
  ULONG   dwDoCount;


  HANDLE hConsole;
  COORD  posCursor, posStart;
  CONSOLE_SCREEN_BUFFER_INFO tConssoleInfo;

  //Step 1: Display hardware and software settings for running this example
  printf("Before running this example, please\n");
  printf("use the device installation utility to add the device.\n");

  //Step 2: Input parameters
  printf("\nPlease input parameters:");
  printf("\nDevice Number (check the device installation utility): ");
  scanf("%d", &lDevNum);

  iNextChecking = 1;
  while(iNextChecking)
  {
    memset( dwBuffer, 0xff, sizeof(dwBuffer));
  
    //Step 3: Open device
    dwErrCde = DRV_DeviceOpen(lDevNum, &lDrvHandle);   
    ErrorThenStop(&lDrvHandle, dwErrCde);

    // Set new PatternMatch Value
    DRV_DeviceSetProperty(lDrvHandle, CFG_DiPatternMatchValue, &dwPmValue, sizeof(dwPmValue));

    //
    // FDI functions 
    // 
    // Get current DIO data width setting
    dwCount = sizeof(dwData);
    DRV_DeviceGetProperty(lDrvHandle, CFG_DioFdioDirection, &dwData, &dwCount);

    // 32DI(0)/32DO(1)/ 16 DIO(2) / 8DIO(3)
    switch( dwData )
    {
    case 3:
      dwCount = sizeof(dwBuffer);

      dwDoCount = sizeof(dwDoBuffer);
      for(i=0; i< dwDoCount; i++)
      {
        ((PUCHAR)dwDoBuffer)[i] = (UCHAR)(i & 0xff);
      }
      break;

    case 2:
      dwCount = sizeof(dwBuffer) / 2;
      dwDoCount = sizeof(dwDoBuffer) / 2;
      for(i=0; i < dwDoCount; i++)
      {
        ((PUSHORT)dwDoBuffer)[i] = (USHORT)i;
      }
      break;

    default:
      dwCount = sizeof(dwBuffer) / 4;
      dwDoCount = sizeof(dwDoBuffer) / 4;
      for(i=0; i<dwDoCount; i++)
      {
        dwDoBuffer[i] = i;
      }

      break;
    }

    dwErrCde = DRV_FDIStart(
      lDrvHandle,   //Device handle
      wCyclic,      //Cyclic(1)/non cyclic(0)
      dwCount,      //Data counts 
      dwBuffer      //Data buffer.
      );
    ErrorThenStop(&lDrvHandle, dwErrCde);

    dwErrCde = DRV_FDOStart(
      lDrvHandle,   //Device handle
      wDoCyclic,    //Cyclic(1)/non cyclic(0)
      dwDoCount,    //Data counts 
      dwDoBuffer    //Data buffer.
      );
    ErrorThenStop(&lDrvHandle, dwErrCde);

    //Check the Fast DI conversion status.
    printf("\n");
    iRunning = 1;

    //
    // Console setting
    //
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &tConssoleInfo);
    posCursor.X = 0;
    posCursor.Y = tConssoleInfo.dwCursorPosition.Y;
    posStart.Y = posCursor.Y;

    while(iRunning)
    {
      //
      // Check status:
      // bit 0: Running(1) / Stopped(0)
      // bit 4: Overrun(1) / No overrun(0)
      // bit 9,8: No buffer read(0x), Low buffer ready(10), Hi buffer ready(11)
      //
      if(posCursor.Y >= tConssoleInfo.dwMaximumWindowSize.Y-1)
        posCursor.Y = posStart.Y;

      SetConsoleCursorPosition(hConsole, posCursor);
      dwErrCde = DRV_FDICheck( lDrvHandle, &dwFdiStatus, &dwRetrieved);
      if( dwErrCde != SUCCESS)
      {
        ErrorHandler(dwErrCde);
        break;
      } 

      // Running?
      if( dwFdiStatus & 1)
        printf("Running, ");
      else 
        printf("\nStopped, ");

      // Ready buffer ?
      if(dwFdiStatus & 0x200)
      {
        if( dwFdiStatus & 0x100)
        {
          printf("Hi  buffer ready,");
          posCursor.Y++;
        }
        else
        {
          printf("Low buffer ready,");
          posCursor.Y++;
        }

        DRV_ClearFlag( lDrvHandle, ADS_EVT_DI_OVERRUN);
      }
      else
        printf("No  buffer ready,");

      //Overrun ?
      if(dwFdiStatus & 0x10)
      {
        printf("    Overrun, ");      
        DRV_ClearFlag( lDrvHandle, ADS_EVT_DI_OVERRUN);
        posCursor.Y++;
      }
      else
        printf(" No overrun, ");

      //Retrieved data count
      printf("Retrieved=%d\n", dwRetrieved);

      if(!(dwFdiStatus & 0x1))
      {
        printf("Driver terminate FDI running.\n");
        iRunning = 0;
      }

      if(kbhit())
      {
        // Stop the fast DI function
        dwErrCde = DRV_FDIStop(lDrvHandle);
        ErrorHandler(dwErrCde);

        if( getch() == ESC)
          iRunning = 0;
      }
    }

    // Stop DO action.
    dwErrCde =  DRV_FDOStop(lDrvHandle);
    ErrorHandler(dwErrCde);


    // Find Pattern Match position
    dwCount = sizeof(dwPmValue);
    DRV_DeviceGetProperty(lDrvHandle, CFG_DiPatternMatchValue, &dwPmValue, &dwCount);

    dwCount = sizeof(dwData);
    DRV_DeviceGetProperty(lDrvHandle, CFG_DioFdioDirection, &dwData, &dwCount);
    switch( dwData )
    {
    case 3:   //8 DIO
      printf("PatternMatch value = %lxh\n", dwPmValue); 
      for(i=0; i<sizeof(dwBuffer); i++)
      {
        if( (((PUCHAR)dwBuffer)[i] == (dwPmValue & 0xff)) || 
            ( ((PUCHAR)dwBuffer)[i] == 0xff ) )
        {
          printf("dwBuffer[%d]=%xh\n", i/4-1, dwBuffer[i/4-1]);
          printf("dwBuffer[%d]=%xh\n", i/4, dwBuffer[i/4]);
          printf("dwBuffer[%d]=%xh\n", i/4+1, dwBuffer[i/4+1]);
          printf("dwBuffer[%d]=%xh\n", i/4+2, dwBuffer[i/4+2]);
          printf("dwBuffer[%d]=%xh\n", i/4+3, dwBuffer[i/4+3]);
          break;
        }
      }
      break;

    case 2:   //16 DIO
      printf("PatternMatch value = %lxh\n", dwPmValue); 
      for(i=0; i<sizeof(dwBuffer)/sizeof(short); i++)
      {
        if(( ((PUSHORT)dwBuffer)[i] == (dwPmValue & 0xffff) ) || 
           ( ((PUSHORT)dwBuffer)[i] == 0xffff )  )
        {
          printf("dwBuffer[%d]=%xh\n", i/2-1, dwBuffer[i/2-1]);
          printf("dwBuffer[%d]=%xh\n", i/2, dwBuffer[i/2]);
          printf("dwBuffer[%d]=%xh\n", i/2+1, dwBuffer[i/2+1]);
          printf("dwBuffer[%d]=%xh\n", i/2+2, dwBuffer[i/2+2]);
          printf("dwBuffer[%d]=%xh\n", i/2+3, dwBuffer[i/2+3]);
          break;
        }
      }
      break;

    case 1:   //32 DO
    case 0:   //32 DI
      printf("PatternMatch value = %lxh\n", dwPmValue); 
      for(i=0; i<sizeof(dwBuffer)/sizeof(ULONG); i++)
      {
        if( (dwBuffer[i] == dwPmValue) || (dwBuffer == 0) )
        {
          printf("dwBuffer[%d]=%lxh\n", i-1, dwBuffer[i-1]);
          printf("dwBuffer[%d]=%lxh\n", i, dwBuffer[i]); 
          printf("dwBuffer[%d]=%lxh\n", i+1, dwBuffer[i+1]);
          break;
        }
      }
      break;
    }

    // Step 6: Close device
    dwErrCde = DRV_DeviceClose(&lDrvHandle);    
    ErrorHandler(dwErrCde);

    do{
      printf("\nNext checking? (y/n):");
      iRunning = getch();
      if(iRunning == 'n' || iRunning == 'N')
      {
        iNextChecking = 0;
        break;
      }
    }while( (iRunning != 'y') && (iRunning != 'Y'));

    dwPmValue++;
  }

  puts("");
  printf("Test porgram finish, Press any key to exit....                       \n");
  getch();
  posCursor.Y = tConssoleInfo.dwMaximumWindowSize.Y-1;
  SetConsoleCursorPosition(hConsole, posCursor);
  puts("");
}//main


//**********************************************************************
//* Function: ErrorHandler
//*           Show the error message for the corresponding error code
//* input:    dwErrCde, IN, Error code
//* return:   none
//**********************************************************************
void ErrorHandler(DWORD dwErrCde)
{
  char szErrMsg[180];

  if( dwErrCde != SUCCESS)
  {
    DRV_GetErrorMessage(dwErrCde, szErrMsg);
    printf("\nError(%d): %s\n", dwErrCde & 0xffff, szErrMsg);
  }
}//ErrorHandler

//**********************************************************************
//* Function:   ErrorThenStop
//*             Release all resource and terminate program if error occurs 
//* Paramaters: pDrvHandle, IN/OUT, pointer to Driver handle
//*             dwErrCde, IN, Error code.
//* return:     none             
//**********************************************************************/
void ErrorThenStop(long *pDrvHandle, DWORD dwErrCde)
{
  //Error message 
  if( dwErrCde != SUCCESS)
  {
    ErrorHandler(dwErrCde);
    printf("Program terminated!\n");
    
    //Close device
    DRV_DeviceClose(pDrvHandle);
    exit(1);
  }
}//ErrorThenStop

⌨️ 快捷键说明

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