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

📄 counter.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 3 页
字号:
        WM_CREATE     - create window
        WM_COMMAND    - application menu (About dialog box)
        WM_DESTROY    - destroy window
****************************************************************************/

long FTYPE MainWndProc
(
   HWND        hWnd,                          // window handle
   unsigned    message,                       // type of message
   WPARAM      wParam,                        // additional information
   LPARAM      lParam                         // additional information
)
{
   static  HANDLE  hInstance ;
   HMENU           hMenu;
   HDC             hdc;
   char            szBuffer[120];
   RECT            rect;
   DWORD           dwCurrentTime;
   DWORD           dwErrorTime;
   BOOL            bTimerOverrun;
   DWORD           dwExitCode;

   switch (message)
   {
      case WM_CREATE:
         //
         // initialize Menu
         //

         hMenu = GetMenu(hWnd);
         EnableMenuItem(hMenu, IDM_STOP, MF_BYCOMMAND | MF_GRAYED);

         hInstance = ((LPCREATESTRUCT) lParam)->hInstance;

         lpfnConfigDlgProc = MakeProcInstance (ConfigDlgProc, hInstance);
         lpfnScanDlgProc = MakeProcInstance (ScanDlgProc, hInstance);

         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_SCAN :
               DialogBox (hInstance, MAKEINTRESOURCE(IDD_SCAN),
                        hWnd, lpfnScanDlgProc);
               return 0;

            case IDM_STOP :
               
			         if ( bThreadloop == TRUE )
                  {
				         bThreadloop = FALSE;
                     GetExitCodeThread( hThreadHandle, &dwExitCode);
                     if ( dwExitCode == STILL_ACTIVE )
                     {
                        TerminateThread( hThreadHandle, dwExitCode);
                     }
                  }

				      if (bRun == TRUE)
                  {
                     KillTimer(hWnd, 1);
                     bRun = FALSE;
                     // counter reset
                     if( (ErrCde = DRV_CounterReset(DriverHandle,
                       (LPARAM)gwChannel)) != 0)
                     {
                         DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                         MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                     }
 	
                     DRV_DeviceClose((LONG far *)&DriverHandle);
                  }
                  
                  EnableMenuItem(hMenu, IDM_STOP, MF_BYCOMMAND | MF_GRAYED);
                  EnableMenuItem(hMenu, IDM_START, MF_BYCOMMAND | MF_ENABLED);

               return 0;
               //break;


            case IDM_START:

               //
               // Open Device
               //

               if (gnNumOfSubdevices == 0)
                   ErrCde = DRV_DeviceOpen(
                            DeviceList[gwDevice].dwDeviceNum,
                                (LONG far *)&DriverHandle);
               else
                  ErrCde = DRV_DeviceOpen(
                           SubDeviceList[gwSubDevice].dwDeviceNum,
                                (LONG far *)&DriverHandle);

               if (ErrCde != SUCCESS)
               {
                   strcpy(szErrMsg,"Device open error !");
                   MessageBox(hWnd,(LPCSTR)szErrMsg,"Device Open",MB_OK);
                   return 0;
               }

                    //
                    // get number of counter channels
                    //

                    ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
                    ptDevFeatures.size = sizeof(DEVFEATURES);
                    if ((ErrCde = DRV_DeviceGetFeatures(DriverHandle,
                        (LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS)
                    {
                        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                        MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                        DRV_DeviceClose((LONG far *)&DriverHandle);
                        return 0;
                    }

                    // check number of counter channels
                    if (DevFeatures.usMaxTimerChl == 0)
                    {
                        MessageBox(hWnd,(LPCSTR)"No Counter Channel",
                            "Driver Message",MB_OK);
                        DRV_DeviceClose((LONG far *)&DriverHandle);
                        return 0;
                    }

                    //
                    // start counter                    
                    //			  
                    ptCounterConfig.usCounter     = gwChannel;
						  ptCounterConfig.usClkSrc      = gwClkSrc;
 						  ptCounterConfig.usInitValue   = (USHORT)g_usIniVal;
						  ptCounterConfig.usOutputMode  = gwOutputMode;
						  ptCounterConfig.usGateSrc     = gwGateSrc;

						  if((ErrCde = DRV_CounterConfig(DriverHandle,
                         (LPT_CounterConfig)&ptCounterConfig)) != 0)
                    {
                        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                        MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                        return TRUE;
                    }

						  
                    ptCounterEventStart.counter = gwChannel;

                    if((ErrCde = DRV_CounterEventStart(DriverHandle,
                         (LPT_CounterEventStart)&ptCounterEventStart)) != 0)
                    {
                        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                        MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                        return TRUE;
                    }
            
                    ptEnableEvent.EventType = ADS_EVT_TERMINATE_CNT0;                    
		              ptEnableEvent.Enabled   = gwIntFlag;
	                 ptEnableEvent.Count     = gwCount;
	                 if ( ErrCde = DRV_EnableEvent( DriverHandle, 
						     (LPT_EnableEvent)&ptEnableEvent ) != 0)
                    {
                       DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                       MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                       return TRUE;
                    } 

			           if ( gwIntFlag )
                    {
                        hThreadHandle = CreateThread(0, 0,
                        (LPTHREAD_START_ROUTINE) UserThread,
                         0, 0, (LPDWORD)&dwThreadID);
			
			               bThreadloop = TRUE;
                    }
                    
                    //
                    // enable Stop Menu item and disable the others
                    //

                    EnableMenuItem(hMenu, IDM_STOP, MF_BYCOMMAND | MF_ENABLED);
                    EnableMenuItem(hMenu, IDM_START, MF_BYCOMMAND | MF_GRAYED);

                    //
                    // Set the new timer type and start it up.
                    //

                    if (SetTimer(hWnd, 1, gwScanTime, NULL) == (UINT)NULL)
                    {
                        MessageBox(hWnd, "Couldn't create timer.", "Notice!",
                            MB_OK);
                        DRV_DeviceClose((LONG far *)&DriverHandle);
                        return 0;
                    }

                    bRun = TRUE;

                    gdwStartTime = GetTickCount();
                    gwOverrunCount = 0;

                    return 0;
            }
            return 0;

        case WM_TIMER:

            //
            // Timer set to dispatch a WM_TIMER message has gone off.
            //

            //
            // check if timer is overrun
            //

            dwCurrentTime = GetTickCount();

            gdwElapseTime = gdwElapseTime + (DWORD)gwScanTime;
            dwErrorTime = dwCurrentTime - gdwElapseTime - gdwStartTime;
            if (dwErrorTime > (DWORD)40)        // maximum 40 ms scan time
            {
                gdwElapseTime = dwCurrentTime - gdwStartTime;
                gwOverrunCount ++;
                if (gwOverrunCount >= 10)
                {
                    bTimerOverrun = TRUE;
                    gwOverrunCount = 0;
                }
                else
                    bTimerOverrun = FALSE;
            }
            else
            {
                bTimerOverrun = FALSE;
                gwOverrunCount = 0;
            }


            //
            // read count
            //

            ptCounterEventRead.counter = gwChannel;
            ptCounterEventRead.overflow = (USHORT far *)&gwOverflow;
            ptCounterEventRead.count = (ULONG far *)&gdwReading;

            if ((ErrCde = DRV_CounterEventRead(DriverHandle,
                (LPT_CounterEventRead)&ptCounterEventRead)) != 0)
            {
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                KillTimer(hWnd, 1);
                MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
                DRV_DeviceClose((LONG far *)&DriverHandle);
                return TRUE;
            }

            //
            // Display Data
            //

            hdc = GetDC(hWnd);
            GetClientRect(hWnd, &rect);
            InvalidateRect(hWnd, NULL, TRUE);
            UpdateWindow(hWnd);
            wsprintf((LPSTR)szBuffer, "Counter Reading = %8lu", gdwReading);
            //sprintf((LPSTR)szBuffer, "Counter Reading = %8d", gdwReading);
            TextOut( hdc, 300, 200, szBuffer, 26 );

            sprintf( szBuffer, "Terminate Interrupt Count  = %4d\n", gwIntCount );
            TextOut( hdc, 300, 250, szBuffer, 33 );

            ReleaseDC(hWnd, hdc);

            return 0;

        case WM_DESTROY:
			{
			   if ( bThreadloop == TRUE )
			   {
				   bThreadloop = FALSE;
			   }
				if (bRun == TRUE)
			   {
                  KillTimer(hWnd, 1);

                  // counter reset
                  if( (ErrCde = DRV_CounterReset(DriverHandle,
                    (LPARAM)gwChannel)) != 0)
				  {
                      DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                      MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
				  }

                DRV_DeviceClose((LONG far *)&DriverHandle);
				}
                PostQuitMessage(0);
			}
            break;

        default:
            return (DefWindowProc(hWnd, message, wParam, lParam));
    }

    return ((LONG)NULL);
}

// ------------------------------------------------------------------
// UserThread
//
// -------------------------------------------------------------------
void UserThread()
{
   USHORT usEventType;
   LONG  ErrCde;

   gwIntCount = 0;

   while(bThreadloop)
   {
      // Check message
      ptCheckEvent.EventType = &usEventType;
      ptCheckEvent.Milliseconds = INFINITE;

      if ((ErrCde = DRV_CheckEvent(DriverHandle,
          (LPT_CheckEvent)&ptCheckEvent)) != 0)
      {
          MessageBox(hMainWnd, "Check Event Error !", "Thread Message", MB_OK);
		    bThreadloop = FALSE;
          return;
      }

      // Process interrupt event
      if ( usEventType == ADS_EVT_TERMINATE_CNT0 )
      {
         gwIntCount ++;  
		   //MessageBox( hMainWnd, "Counter Terminal Event Occured !","Interrupt Message",MB_OK);
      }
   }
}

⌨️ 快捷键说明

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