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

📄 qcounter.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 2 页
字号:
        case WM_COMMAND :
             switch (LOWORD(wParam))
             {
			 case IDOK :
				 
				 //
				 // get device selection
				 //
				 if ((dwIndex = SendDlgItemMessage(hDlg, IDC_DEVICE,
					 CB_GETCURSEL, 0, 0)) != CB_ERR)
					 wDriver = (WORD)dwIndex;
				 
				 // get channel number
				 if(SendDlgItemMessage(hDlg, IDC_ECHANNEL,
					 EM_GETMODIFY, 0, 0))
				 {
                        SendDlgItemMessage(hDlg, IDC_ECHANNEL,
                                WM_GETTEXT, 10, (LPARAM)(LPSTR)szBuffer) ;
                        wChannel = atoi(szBuffer);
                        SendDlgItemMessage(hDlg, IDC_ECHANNEL,
                                  EM_SETMODIFY, FALSE, 0) ;
                  }

                  // get usInputMode number
                  usInputMode = (USHORT)SendDlgItemMessage(hDlg, IDC_INPUTMODE,
                      CB_GETCURSEL, 0, (LPARAM)0);

                  // get usLatchSource number
                  usLatchSource = (USHORT)SendDlgItemMessage(hDlg, IDC_LATCHSOURCE,
                      CB_GETCURSEL, 0, (LPARAM)0);

                  usCheck = (int)SendDlgItemMessage(hDlg, IDC_CHECKFREERUN,
                                                    BM_GETCHECK,0,0);
                  if(usCheck == 1)
                     usFreeRun = 1;
                  else
                     usFreeRun = 0;

                  usCheck = (int)SendDlgItemMessage(hDlg, IDC_CHECKRESET,
                                                    BM_GETCHECK,0,0);
                  if(usCheck == 1)
                     usResetAfterLatch = 1;
                  else
                     usResetAfterLatch = 0;

                  // get reset value entry
                  if(IsDlgButtonChecked(hDlg,IDC_RESETVALUE0))
                      usResetValue = 0;
                  else
                      usResetValue = 1;
				  
				  // get digital filter flag and clock
				  if (SendDlgItemMessage(hDlg, IDC_DIGFILTERENABLE,BM_GETCHECK,0,0) == 1)
					  usDigtalFilter = 1;
				  else
					  usDigtalFilter = 0;
				  usDigFilterClock = (ULONG)SendDlgItemMessage(hDlg, IDC_DIGFILTERCLOCK,
					  CB_GETCURSEL, 0, (LPARAM)0);
				  
				  // get lock flag
				  if (SendDlgItemMessage(hDlg, IDC_OVERLOCK,BM_GETCHECK,0,0) == 1)
					  usOverflowLock = 1;
				  else
					  usOverflowLock = 0;
				  
				  // get index reset flag
				  if (SendDlgItemMessage(hDlg, IDC_OVERLOCK,BM_GETCHECK,0,0) == 1)
					  usIndexReset = 1;
				  else
					  usIndexReset = 0;
				  
				  EndDialog(hDlg, 0);
                   return TRUE;

                case IDCANCEL :
                     EndDialog(hDlg, 0);
                     return TRUE;

				case IDC_DEVICE:
					if (HIWORD(wParam) == CBN_SELCHANGE)
					{
						wDriver = SendDlgItemMessage(hDlg, IDC_DEVICE, CB_GETCURSEL, 0, 0);

						// Device Open and Get board ID
						if((ErrCde = DRV_DeviceOpen((ULONG)wDriver,(LONG far *)&DriverHandle)) != 0)
						{
							DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
							MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
							return TRUE;
						}

						i = SendDlgItemMessage(hDlg, IDC_LATCHSOURCE, CB_GETCOUNT, 0, 0);

						ulSize = sizeof(ULONG);
						ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_BoardID, &dwBoardID, &ulSize);
						if (dwBoardID == BD_PCI1784)
						{
							
							EnableWindow(GetDlgItem(hDlg, IDC_CHECKRESET), FALSE);
							EnableWindow(GetDlgItem(hDlg, IDC_CHECKFREERUN), FALSE);
							EnableWindow(GetDlgItem(hDlg, IDC_DIGFILTERENABLE), TRUE);
							EnableWindow(GetDlgItem(hDlg, IDC_DIGFILTERCLOCK), TRUE);
							EnableWindow(GetDlgItem(hDlg, IDC_OVERLOCK), TRUE);
							EnableWindow(GetDlgItem(hDlg, IDC_UNDERLOCK), TRUE);
							EnableWindow(GetDlgItem(hDlg, IDC_INDEXRESET), TRUE);
							SetWindowText(GetDlgItem(hDlg, IDC_RESETVALUE0), "0000 0000 H");
							SetWindowText(GetDlgItem(hDlg, IDC_RESETVALUE8), "8000 0000 H ");
							if (i == 5)
							{
								SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[5]));
								SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[6]));
							}
						}
						else
						{
							EnableWindow(GetDlgItem(hDlg, IDC_CHECKRESET), TRUE);
							EnableWindow(GetDlgItem(hDlg, IDC_CHECKFREERUN), TRUE);
							EnableWindow(GetDlgItem(hDlg, IDC_DIGFILTERENABLE), FALSE);
							EnableWindow(GetDlgItem(hDlg, IDC_DIGFILTERCLOCK), FALSE);
							EnableWindow(GetDlgItem(hDlg, IDC_OVERLOCK), FALSE);
							EnableWindow(GetDlgItem(hDlg, IDC_UNDERLOCK), FALSE);
							EnableWindow(GetDlgItem(hDlg, IDC_INDEXRESET), FALSE);
							SetWindowText(GetDlgItem(hDlg, IDC_RESETVALUE0), "00 0000 H");
							SetWindowText(GetDlgItem(hDlg, IDC_RESETVALUE8), "80 0000 H");
							if (i == 7)
							{
								SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_DELETESTRING,6,0);
								SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_DELETESTRING,5,0);
							}
						}
						
					}
					break;
             }
             break;
    }
    return FALSE ;
}

/***************************************************************************
    FUNCTION: MainWndProc(HWND, unsigned, WPARAM, LPARAM)

    PURPOSE:  Processes messages

    MESSAGES:

        WM_CREATE     - create window
        WM_COMMAND    - application menu (About dialog box)
        WM_DESTROY    - destroy window
****************************************************************************/

long FTYPE MainWndProc(HWND hWnd, unsigned message, WPARAM wParam, LPARAM lParam)
{
    static  HANDLE  hInstance ;
    HMENU           hMenu;
    PT_QCounterConfig  ptQCounterConfig;
    PT_QCounterStart   ptQCounterStart;
	LONG            pBuffer[5];
	float           pfClock[5];
	ULONG           ulSize;

    switch (message)
      {
       case WM_CREATE:
            hMenu = GetMenu(hWnd);
            hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
            lpfnConfigDlgProc = MakeProcInstance (ConfigDlgProc, hInstance);
            EnableMenuItem(hMenu, IDM_STOP, MF_BYCOMMAND | MF_GRAYED);

            return 0 ;

       case WM_COMMAND:     /* message: from application menu */
           hMenu = GetMenu(hWnd);

          switch (LOWORD(wParam))
          {
           case IDM_SETTING :
              DialogBox (hInstance, MAKEINTRESOURCE(IDD_SETTING),
                         hWnd, lpfnConfigDlgProc) ;
              return 0;

           case IDM_RUN :
              // Open Driver
             
			      if((ErrCde = DRV_DeviceOpen((ULONG)wDriver,(LONG far *)&DriverHandle)) != 0)
              	{
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                    return TRUE;
              	}

				if (dwBoardID == BD_PCI1784)
				{
					ulSize = sizeof(LONG) * 5;
					ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_CntrDigitalFilter, pBuffer, &ulSize);
					pBuffer[wChannel] = usDigtalFilter;
					ErrCde = DRV_DeviceSetProperty(DriverHandle, CFG_CntrDigitalFilter, pBuffer, ulSize);
					
					ulSize = sizeof(float) * 5;
					ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_CntrClockFrequency, pfClock, &ulSize);
					switch(usDigFilterClock)
					{
					case 0:
						pfClock[0] = 8000000.0;
						break;
					case 1:
						pfClock[0] = 4000000.0;
						break;
					case 2:
						pfClock[0] = 2000000.0;
						break;
					case 3:
						pfClock[0] = 1000000.0;
						break;
					default:
						pfClock[0] = 8000000.0;
					}
					ErrCde = DRV_DeviceSetProperty(DriverHandle, CFG_CntrClockFrequency, pfClock, ulSize);
					
					// lock
					ulSize = sizeof(LONG) * 5;
					ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_CntrCounterLockControl, pBuffer, &ulSize);
					pBuffer[wChannel] = 0;
					if (usOverflowLock == 1)
						pBuffer[wChannel] = 1;
					if (usUnderflowLock == 1)
						pBuffer[wChannel] = 2;
					if (usOverflowLock && usUnderflowLock)
						pBuffer[wChannel] = 3;
					ErrCde = DRV_DeviceSetProperty(DriverHandle, CFG_CntrCounterLockControl, pBuffer, ulSize);
					
					// index reset
					ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_CntrIndexReset, pBuffer, &ulSize);
					if (usIndexReset)
						pBuffer[0] |= 0x1 << wChannel;
					else
						pBuffer[0] &= ~(0x1 << wChannel);
					ErrCde = DRV_DeviceSetProperty(DriverHandle, CFG_CntrIndexReset, pBuffer, ulSize);
				}

                ptQCounterConfig.counter       = wChannel;
                ptQCounterConfig.LatchSrc      = usLatchSource;
                ptQCounterConfig.LatchOverflow = usFreeRun;
                ptQCounterConfig.ResetOnLatch  = usResetAfterLatch;
                ptQCounterConfig.ResetValue    = usResetValue;

                // Execute counter config
                if((ErrCde = DRV_QCounterConfig(DriverHandle,
                         (LPT_QCounterConfig)&ptQCounterConfig)) != 0)
			  	{
                   DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                   MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                   return TRUE;
			  	}

                ptQCounterStart.counter   = wChannel;
                ptQCounterStart.InputMode = usInputMode;
 
                // Execute counter start
                if((ErrCde = DRV_QCounterStart(DriverHandle,
                         (LPT_QCounterStart)&ptQCounterStart)) != 0)
			  	{
                   DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                   MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                   return TRUE;
			  	}
                
                EnableMenuItem(hMenu, IDM_RUN, MF_GRAYED);
                EnableMenuItem(hMenu, IDM_STOP, MF_ENABLED);
               
              usStart = 1;

              hStopEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
              hReadCounter = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)ReadCounter, &wChannel,0, &dwThreadId);

            break;
            
           case IDM_STOP:
              SetEvent(hStopEvent);
              WaitForSingleObject(hReadCounter, INFINITE);
              // counter reset
              if( (ErrCde = DRV_CounterReset(DriverHandle,wChannel)) != 0)
              {
                 DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                 MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                 return TRUE;
              }
              
              // Close Driver
              if( (ErrCde = DRV_DeviceClose((LONG far *)&DriverHandle)) != 0)
              {
                 DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                 MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                 return TRUE;
              }
              EnableMenuItem(hMenu, IDM_STOP, MF_BYCOMMAND | MF_GRAYED);
              EnableMenuItem(hMenu, IDM_RUN, MF_BYCOMMAND | MF_ENABLED);
              
              usStart = 0;
              break;

        }

        return 0;
  
       case WM_DESTROY:

			if (usStart != 0)
			{
               SetEvent(hStopEvent);
               WaitForSingleObject(hReadCounter, INFINITE);
               // counter reset
               if( (ErrCde = DRV_CounterReset(DriverHandle,wChannel)) != 0)
               {
                   DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                   MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                   return TRUE;
               }

               // Close Driver
               if( (ErrCde = DRV_DeviceClose((LONG far *)&DriverHandle)) != 0)
               {
                   DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                   MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                   return TRUE;
               }
			}
           PostQuitMessage(0);
           break;
      default:
           return (DefWindowProc(hWnd, message, wParam, lParam));
      }

   return ((LONG)NULL);
}

void ReadCounter(int* cnt)
{
   HDC     hdc;
   char    szBuffer[100];
   RECT    rect;
   PT_QCounterRead    ptQCounterRead;
   ptQCounterRead.counter = *cnt;
   ptQCounterRead.overflow = (USHORT far *)&usOverflow;
   ptQCounterRead.LoCount  = (ULONG  far *)&ulLoCount;
   ptQCounterRead.HiCount  = (ULONG  far *)&ulHiCount;
   
   
   while(WAIT_TIMEOUT == WaitForSingleObject(hStopEvent, 0)){
  
         // Execute read counter
   
         if((ErrCde = DRV_QCounterRead(DriverHandle,
            (LPT_QCounterRead)&ptQCounterRead)) != 0)
         {
            DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
            MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
            return;
         }
   
         //Display Input Data
         hdc = GetDC(hWnd);
         GetClientRect(hWnd, &rect);
         InvalidateRect(hWnd, &rect, TRUE);
         sprintf(szBuffer, "LoCNT = %10lu(Dec) %8X(Hex)   HiCNT = %10lu(Dec) %8X(Hex)   Overflow = %d",
            ulLoCount, ulLoCount, ulHiCount, ulHiCount, usOverflow);
         DrawText(hdc, szBuffer, -1, &rect,
            DT_SINGLELINE | DT_CENTER | DT_VCENTER);
         ReleaseDC(hWnd, hdc);
         Sleep(100);
         
   }
}

⌨️ 快捷键说明

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