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

📄 counter.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 4 页
字号:
// ############################################################################
// ****************************************************************************
//              Copyright (C) 2000-2, Advantech Automation Corp.
//      THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY
//         INFORMATION WHICH IS THE PROHERTY OF ADVANTECH AUTOMATION CORP.
// 
//   ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM
//             ADVANTECH AUTOMATION CORP., IS STRICTLY PROHIBITED
// ****************************************************************************
// ############################################################################
// 
// File:        COUNTER.c
// Created:     08/19/2002
// Description: Demo program for event counting
// ----------------------------------------------------------------------------

#include <windows.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "..\..\..\..\include\driver.h"
#include "resource.h"

// ------------------------------------------------------------------------
// CONSTANT DEFINITION
// ------------------------------------------------------------------------
#define     MAX_DEVICES     100
#define     MAX_CNT_NUM      8
#define     ADS_EVT_NUM      9

// ------------------------------------------------------------------------
// GLOBAL VARIABLES
// ------------------------------------------------------------------------
HANDLE      hInst;                      // current instance
HWND        hMainWnd;                   // main window handle

static      DEVFEATURES          DevFeatures;    // structure for device features
static      PT_CounterEventStart ptCounterEventStart;
static      PT_CounterEventRead  ptCounterEventRead;
static      PT_DeviceGetFeatures ptDevFeatures;
static      PT_CounterConfig     ptCounterConfig;
static      PT_EnableEvent       ptEnableEvent;
static      PT_CheckEvent        ptCheckEvent;      // Check event 

char        szErrMsg[80];               // Use for MESSAGEBOX function
LRESULT     ErrCde;                     // Return error code

//
// used for fixed memory for the installed devices
//
DEVLIST     DeviceList[MAX_DEVICES];
DEVLIST     SubDeviceList[MAX_DEVICES];

LONG        DriverHandle = (LONG)NULL;          // driver handle
BOOL        bRun = FALSE;                       // flag for running
USHORT      gwDevice = 0, gwSubDevice = 0;      // device index
USHORT      gwChannel = 0;                      // input channel
USHORT      gwOverflow  = 0;                    // counter over 32 bit flag
ULONG       gdwReading  = 0;                    // read counter data
SHORT       gnNumOfDevices, gnNumOfSubdevices;  // number of installed devices
LONG        g_usIniVal =1;                      // number of the initial value of the counter
USHORT      gwScanTime = 1000;                  // scan time
DWORD       gdwStartTime;                       // start time
DWORD       gdwElapseTime = 0;                  // elapse time
USHORT      gwOverrunCount = 0;                 // overrun count
USHORT      gwStopFlag = 0;                     // stop button flag
FARPROC     lpfnConfigDlgProc;                  // config. dialog procedure
FARPROC     lpfnScanDlgProc;                    // scan dialog procedure
static  USHORT gwIntFlag  = 0;                  // interrupt enable(0)
static  USHORT gwCntEdge  = 0;		            // Count edge 0-rising, 1-falling
static  USHORT gwOutputEnable = 0x0004;         // count output enable (0-disable)
static  USHORT gwCntDirect    = 0x08;           // count direction up (0-down)
static  USHORT gwGatePolarity =0;               // 0-high level(rising edge)
                                                // 1-low level(falling edge)
static  USHORT gwOutputMode  = 0;               // defautl output mode 
static  USHORT gwCounterMode = 0;               // default per-define coutner mode 
static  USHORT gwGateSrc     = 0;               // default gate source
static  USHORT gwClkSrc      = 0;               // default count source

USHORT  gwGateSrcIndex       = 1;               // index of gate source
USHORT  gwCounterModeIndex   = 0;               // index of counter mode
USHORT  gwClkSrcIndex        = 1;               // index of count source
USHORT  gwOutputModeIndex    = 0;               // index of output mode

DWORD   dwBordID = BD_PCI1780;                           //BordID
USHORT  usMaxCntNumber = 0;                     //Max Counter Number

UINT        gwCount       = 1;                  // number of interrupt event count

HANDLE      hThreadHandle = NULL;
DWORD       dwThreadID = 0x00;
BOOL        bThreadflag = FALSE;
BOOL        bThreadloop = FALSE;
void UserThread();
// ------------------------------------------------------------------------
// FUNCTION DECLARATION
// ------------------------------------------------------------------------
BOOL InitApplication(HANDLE);
BOOL InitInstance(HANDLE, int);
long FTYPE MainWndProc(HWND, UINT, WPARAM, LPARAM);
int  FTYPE WinMain(HANDLE, HANDLE, LPSTR, int);


/***************************************************************************
    FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)

    PURPOSE: calls initialization function, processes message loop
****************************************************************************/

int FTYPE WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
HANDLE hInstance;                          /* current instance             */
HANDLE hPrevInstance;                      /* previous instance            */
LPSTR lpCmdLine;                           /* command line                 */
int nCmdShow;                              /* show-window type (open/icon) */
{
    MSG msg;                               /* message                      */

    if (!hPrevInstance)                /* Other instances of app running?  */
      if (!InitApplication(hInstance)) /* Initialize shared things         */
          return (FALSE);              /* Exits if unable to initialize    */

    // Perform initializations that apply to a specific instance
    if (!InitInstance(hInstance, nCmdShow))
        return (FALSE);

    // Acquire and dispatch messages until a WM_QUIT message is received.
    while (GetMessage(&msg,      /* message structure                      */
        (HWND)NULL,              /* handle of window receiving the message */
        (UINT)NULL,              /* lowest message to examine              */
        (UINT)NULL))             /* highest message to examine             */
    {
        TranslateMessage(&msg);  /* Translates virtual key codes           */
        DispatchMessage(&msg);   /* Dispatches message to window           */
    }
    return (msg.wParam);         /* Returns the value from PostQuitMessage */
}

/****************************************************************************
    FUNCTION: InitApplication(HANDLE)

    PURPOSE: Initializes window data and registers window class
****************************************************************************/

BOOL InitApplication(hInstance)
HANDLE hInstance;                            /* current instance           */
{
    WNDCLASS  wc;

    // Fill in window class structure with parameters that describe the
    // main window.
    wc.style = CS_HREDRAW | CS_VREDRAW; /* Class style(s).                 */
    wc.lpfnWndProc = (WNDPROC)MainWndProc;   /* window process procedure   */
    wc.cbClsExtra = 0;                  /* No per-class extra data.        */
    wc.cbWndExtra = 0;                  /* No per-window extra data.       */
    wc.hInstance = hInstance;           /* Application that owns the class.*/
    wc.hIcon = LoadIcon(hInstance, "IDI_ICON1");
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName =  "MyMenu";   /* Name of menu resource in .RC file.  */
    wc.lpszClassName = "MyClass";  /* Name used in call to CreateWindow. */

    // Register the window class and return success/failure code.
    return (RegisterClass(&wc));

}

/***************************************************************************
    FUNCTION:  InitInstance(HANDLE, int)

    PURPOSE:  Saves instance handle and creates main window
****************************************************************************/

BOOL InitInstance
(
    HANDLE          hInstance,          // Current instance identifier.
    int             nCmdShow            // Param for first ShowWindow() call.
)
{
    // Save the instance handle in static variable, which will be used in
    // many subsequence calls from this application to Windows.
    hInst = hInstance;

    // Create a main window for this application instance.
    hMainWnd = CreateWindow(
        "MyClass",                 /* See RegisterClass() call.           */
        "Advantech Driver Demo : Event Counter" , /* Window title bar  */
        WS_OVERLAPPEDWINDOW,        /* Window style.                       */
        CW_USEDEFAULT,              /* Default horizontal position.        */
        CW_USEDEFAULT,              /* Default vertical position.          */
        CW_USEDEFAULT,              /* Default width.                      */
        CW_USEDEFAULT,              /* Default height.                     */
        NULL,                       /* Overlapped windows have no parent.  */
        NULL,                       /* Use the window class menu.          */
        hInstance,                  /* This instance owns this window.     */
        NULL                        /* Pointer not needed.                 */
        );

    // If window could not be created, return "failure"
    if (!hMainWnd)
        return (FALSE);

    // Make the window visible; update its client area; and return "success"

    ShowWindow(hMainWnd, nCmdShow);   // Show the window
    UpdateWindow(hMainWnd);           // Sends WM_PAINT message
    return (TRUE);                // Returns the value from PostQuitMessage
}

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

    PURPOSE:  Processes dialog box messages for configuration
****************************************************************************/

BOOL FTYPE ConfigDlgProc
(
    HWND        hDlg,                          // window handle
    unsigned    message,                       // type of message
    WPARAM      wParam,                        // additional information
    LPARAM      lParam                         // additional information
)
{
    int         i;
    DWORD       dwIndex;
    char        szBuffer[40];
    int         nOutEntries;
    DWORD       dwExitCode;

    switch (message)
    {
    case WM_INITDIALOG :

        // --------------------------------
        // Initialize Device List Combobox
        // --------------------------------
       if (hThreadHandle != NULL){
          
          GetExitCodeThread(hThreadHandle, &dwExitCode );
          if( dwExitCode == STILL_ACTIVE ){
             TerminateThread( hThreadHandle, dwExitCode );
             hThreadHandle = NULL;
          }
       }

        if (DriverHandle!=0){
          DRV_DeviceClose(&DriverHandle);
          DriverHandle = 0;
       }

        // get number of the installed devices
        if ((ErrCde = DRV_DeviceGetNumOfList((SHORT far *)&gnNumOfDevices)) !=
            SUCCESS)
        {
             DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
             MessageBox(hMainWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
             return TRUE;
        }


        // ----------------------------------------------------------------
        // used for dynamic allocation memory for these installed devices
        //
        // if ((hDeviceList = GlobalAlloc(GHND, gnNumOfDevices *
        //      sizeof(DEVLIST))) == NULL)
        // {
        //    MessageBox(hMainWnd, (LPCSTR)"Not Enough Memory",
        //        "Memory Allocation", MB_OK);
        //    return TRUE;
        // }
        // DeviceList = (LPDEVLIST)GlobalLock(hDeviceList);
        // ----------------------------------------------------------------


        if (gnNumOfDevices > MAX_DEVICES)
            gnNumOfDevices = MAX_DEVICES;

        // retrieve the information of all installed devices

        if ((ErrCde = DRV_DeviceGetList((DEVLIST far *)&DeviceList[0],
            (SHORT)gnNumOfDevices, (SHORT far *)&nOutEntries)) != (LONG)SUCCESS)
        {
            DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
            MessageBox(hMainWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
            return TRUE;
        }

        // initialize the Device List Combobox with the retrieved information

        for (i = 0; i < gnNumOfDevices; i ++)
            SendDlgItemMessage(hDlg, IDC_DEVICE, CB_ADDSTRING, 0,
                        (LPARAM)((LPSTR)DeviceList[i].szDeviceName));

        SendDlgItemMessage(hDlg, IDC_DEVICE, CB_SETCURSEL,(WPARAM)gwDevice,
                        (LPARAM)0);


        // -----------------------------------------------------------
        // Initialize Module List Combobox for COM port or CAN devices
        // -----------------------------------------------------------

        // check if there is any device attached on this COM port or CAN

        gnNumOfSubdevices = DeviceList[gwDevice].nNumOfSubdevices;
        if (gnNumOfSubdevices > MAX_DEVICES)
            gnNumOfSubdevices = MAX_DEVICES;


        // --------------------------------------------------------------
        // used for dynamic allocation memory for these installed devices
        //
        // if ((hSubDeviceList = GlobalAlloc(GHND, gnNumOfSubdevices *
        //    sizeof(DEVLIST))) == NULL)
        // {
        //    MessageBox(hMainWnd, (LPCSTR)"Not Enough Memory",
        //        "Memory Allocation", MB_OK);
        //    return TRUE;
        // }
        //
        // SubDeviceList = (LPDEVLIST)GlobalLock(hSubDeviceList);
        // --------------------------------------------------------------


        // retrieve the information of all installed devices
        if (gnNumOfSubdevices != 0)
        {
            if ((ErrCde = DRV_DeviceGetSubList(
                (DWORD)DeviceList[gwDevice].dwDeviceNum,
                (DEVLIST far *)&SubDeviceList[0],
                (SHORT)gnNumOfSubdevices,
                (SHORT far *)&nOutEntries)) != (LONG)SUCCESS)
            {
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                MessageBox(hMainWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                return TRUE;
            }


            // initialize the Module List Combobox with the retrieved
            // information

            EnableWindow(GetDlgItem(hDlg, IDC_MODULE), TRUE);
            SendDlgItemMessage(hDlg, IDC_MODULE, CB_RESETCONTENT, 0,
                    (LPARAM)((LPSTR)0));

            for (i = 0; i < gnNumOfSubdevices; i++)
                SendDlgItemMessage(hDlg, IDC_MODULE, CB_ADDSTRING, 0,
                        (LPARAM)((LPSTR)SubDeviceList[i].szDeviceName));

            SendDlgItemMessage(hDlg, IDC_MODULE, CB_SETCURSEL,
					(WPARAM)gwSubDevice, (LPARAM)0);
        }
        else
        {
            EnableWindow(GetDlgItem(hDlg, IDC_MODULE), FALSE);
            SendDlgItemMessage(hDlg, IDC_MODULE, CB_RESETCONTENT, 0,
                    (LPARAM)((LPSTR)0));
        }

        //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(hDlg,(LPCSTR)szErrMsg,"Device Open",MB_OK);
           return 0;
        }
        
        
        //
        // get device features
        //
        
        ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
        ptDevFeatures.size = sizeof(DEVFEATURES);
        if ((ErrCde = DRV_DeviceGetFeatures(DriverHandle,
           (LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS)
        {
           DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
           MessageBox(hDlg,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
           DRV_DeviceClose((LONG far *)&DriverHandle);
           return 0;
        }
        

        dwBordID = DevFeatures.dwBoardID;
        usMaxCntNumber = DevFeatures.usMaxTimerChl;
        DRV_DeviceClose(&DriverHandle);
		  // initial counter channel
        for ( i = 0; i< usMaxCntNumber; i++ )
		  {
           _itoa( i, szBuffer, 10 );
           SendDlgItemMessage( 
		     hDlg, 
		     IDC_ECHANNEL,
		     CB_ADDSTRING, 
		     0, 
		     ( LPARAM )( ( LPSTR )szBuffer ) );
		  }
        SendDlgItemMessage(hDlg,IDC_ECHANNEL,CB_SETCURSEL,gwChannel,(LPARAM)0); 
        
		  // initial counter value		 
        itoa(g_usIniVal, szBuffer, 10);
        SendDlgItemMessage(hDlg, IDC_CntIniVal, EM_REPLACESEL, 0,
            (LPARAM)((LPSTR)szBuffer));

		  // initial count output 		          
 		  if ( gwOutputEnable )
		  {
           CheckRadioButton( hDlg,IDC_OutputEnable,IDC_OutputDisable,IDC_OutputEnable );
		  }
        else
		  {
           CheckRadioButton( hDlg,IDC_OutputEnable,IDC_OutputDisable,IDC_OutputDisable );
           EnableWindow( GetDlgItem( hDlg,IDC_OUTMODE ), FALSE );
		  }
		  // initial output mode
        SendDlgItemMessage(
	        hDlg,
			  IDC_OUTMODE,
	        CB_ADDSTRING,
		     0,		     
			  (LPARAM)((LPSTR)"ACTIVE HIGH PULSE"));

        SendDlgItemMessage(
	        hDlg,
	        IDC_OUTMODE,

⌨️ 快捷键说明

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