📄 qcounter.c
字号:
/*
***********************************************************************
* Program : QCOUNTER.C *
* Description : Demo program for QCounter function *
* Revision : 1.00 *
* Date : 9/5/1996 Advantech Co., Ltd. *
***********************************************************************
*
* FUNCTIONS:
*
* InitApplication() - Initializes window data and registers window class
*
* InitInstance() - Saves instance handle and creates main window
*
* MainWndProc() - Process main window messages
*
* ConfigDlgProc() - Process the dialog box messages for configuration
*/
#include <windows.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "..\..\..\include\driver.h"
#include "..\..\..\include\os.h"
#include "resource.h"
#define MAX_DEVICES 100
HANDLE hInst; /* Current instance */
HWND hWnd;
char szErrMsg[80]; /* Use for MESSAGEBOX function */
LRESULT ErrCde; /* Return error code */
SHORT gnNumOfDevices, gnNumOfSubdevices; // number of installed devices
DEVLIST DeviceList[MAX_DEVICES];
DEVLIST SubDeviceList[MAX_DEVICES];
static int wDriver = 0; /* Driver index */
static int wChannel = 0; /* Input channel */
static USHORT usOverflow = 0; /* counter over 32 bit flag */
static ULONG ulLoCount = 0; /* low byte of counter */
static ULONG ulHiCount = 0; /* high byte of counter */
static USHORT usStart = 0; /* Start counter flag */
static LONG DriverHandle = (LONG)NULL; /* Driver Handle */
static USHORT usResetAfterLatch = 0; /* Reset after latch flag */
static USHORT usFreeRun = 0; /* Free run flag */
static USHORT usResetValue = 0; /* Reset Value */
static USHORT usLatchSource = 0; /* Latch source flag */
static USHORT usInputMode = 0; /* Input mode flag */
static USHORT usDigtalFilter = 0; /* digital filter flag */
static USHORT usDigFilterClock = 0; /* digital filter clock */
static USHORT usOverflowLock = 0; /* overflow lock flag */
static USHORT usUnderflowLock = 0; /* underflow lcok flag */
static USHORT usIndexReset = 0; /* index reset flag */
static ULONG dwBoardID; /* board id */
static HANDLE hReadCounter = NULL;
static HANDLE hStopEvent = NULL;
DWORD dwThreadId = 0;
static char *szInputMode[] = {"DISABLE ",
"AB PHASE X1",
"AB PHASE X2",
"AB PHASE X4",
" 2 PULSE IN",
" 1 PULSE IN"};
static char *szLatchSource[] = {"S/W LATCH ",
"ZIN LATCH ",
"DI0 LATCH ",
"DI1 LATCH ",
"TIMER LATCH",
"DI2 LATCH ",
"DI3 LATCH"};
static char *szDigFilterClock[] = {"8 MHz ",
"4 MHz ",
"2 MHz ",
"1 MHz "};
BOOL InitApplication(HANDLE hInstance);
BOOL InitInstance(HANDLE hInstance, int nCmdShow);
long FTYPE MainWndProc(HWND, UINT, WPARAM, LPARAM);
int FTYPE WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow);
static FARPROC lpfnConfigDlgProc;
void ReadCounter(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(hInstance, nCmdShow)
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.
hWnd = CreateWindow(
"MyClass", /* See RegisterClass() call. */
"Advantech Driver Demo : 3-Axis Quadrature Encoder" , /* 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 (!hWnd)
return (FALSE);
// Make the window visible; update its client area; and return "success"
ShowWindow(hWnd, nCmdShow); /* Show the window */
UpdateWindow(hWnd); /* 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, unsigned message, WPARAM wParam, LPARAM lParam)
{
char szBuffer[40];
USHORT usCheck;
ULONG ulSize;
int nOutEntries;
int i;
ULONG dwIndex;
switch (message)
{
case WM_INITDIALOG :
// if usStart !=0 then do close driver
if (usStart != 0)
{
if( (ErrCde = DRV_DeviceClose((LONG far *)&DriverHandle)) != 0)
{
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
return TRUE;
}
}
// Initialize Start counter
usStart = 0;
// --------------------------------
// Initialize Device List Combobox
// --------------------------------
// get number of the installed devices
if ((ErrCde = DRV_DeviceGetNumOfList((SHORT far *)&gnNumOfDevices)) !=
SUCCESS)
{
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
MessageBox(hWnd,(LPCSTR)szErrMsg, "Driver Message", MB_OK);
return TRUE;
}
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(hWnd,(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)wDriver,
(LPARAM)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;
}
ulSize = sizeof(ULONG);
ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_BoardID, &dwBoardID, &ulSize);
// Initialize Input Channel
itoa(wChannel, szBuffer, 10);
SendDlgItemMessage(hDlg, IDC_ECHANNEL, EM_REPLACESEL, 0,
(LPARAM)((LPSTR)szBuffer));
// Create string list for Input Mode Reference ComoBox
SendDlgItemMessage(hDlg,IDC_INPUTMODE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szInputMode[0]));
SendDlgItemMessage(hDlg,IDC_INPUTMODE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szInputMode[1]));
SendDlgItemMessage(hDlg,IDC_INPUTMODE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szInputMode[2]));
SendDlgItemMessage(hDlg,IDC_INPUTMODE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szInputMode[3]));
SendDlgItemMessage(hDlg,IDC_INPUTMODE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szInputMode[4]));
SendDlgItemMessage(hDlg,IDC_INPUTMODE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szInputMode[5]));
// Create string list for Latch Source Reference ComoBox
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[0]));
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[1]));
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[2]));
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[3]));
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[4]));
if (dwBoardID == BD_PCI1784)
{
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[5]));
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szLatchSource[6]));
}
if (usDigtalFilter == 0) // Digital filter disable
SendDlgItemMessage(hDlg,IDC_DIGFILTERENABLE,BM_SETCHECK,0,0);
else
SendDlgItemMessage(hDlg,IDC_DIGFILTERENABLE,BM_SETCHECK,1,0);
// Create string list for digital filter clock Reference ComoBox
SendDlgItemMessage(hDlg,IDC_DIGFILTERCLOCK,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szDigFilterClock[0]));
SendDlgItemMessage(hDlg,IDC_DIGFILTERCLOCK,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szDigFilterClock[1]));
SendDlgItemMessage(hDlg,IDC_DIGFILTERCLOCK,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szDigFilterClock[2]));
SendDlgItemMessage(hDlg,IDC_DIGFILTERCLOCK,CB_ADDSTRING,0,(LPARAM)((LPCSTR)szDigFilterClock[3]));
if (usOverflowLock == 0) // overflow lock
SendDlgItemMessage(hDlg,IDC_OVERLOCK,BM_SETCHECK,0,0);
else
SendDlgItemMessage(hDlg,IDC_OVERLOCK,BM_SETCHECK,1,0);
if (usUnderflowLock == 0) // underflow lock
SendDlgItemMessage(hDlg,IDC_UNDERLOCK,BM_SETCHECK,0,0);
else
SendDlgItemMessage(hDlg,IDC_UNDERLOCK,BM_SETCHECK,1,0);
if (usIndexReset == 0) // index reset
SendDlgItemMessage(hDlg,IDC_INDEXRESET,BM_SETCHECK,0,0);
else
SendDlgItemMessage(hDlg,IDC_INDEXRESET,BM_SETCHECK,1,0);
if (usResetAfterLatch == 0)
SendDlgItemMessage(hDlg,IDC_CHECKRESET,BM_SETCHECK,0,0);
else
SendDlgItemMessage(hDlg,IDC_CHECKRESET,BM_SETCHECK,1,0);
if (usFreeRun == 0)
SendDlgItemMessage(hDlg,IDC_CHECKFREERUN,BM_SETCHECK,0,0);
else
SendDlgItemMessage(hDlg,IDC_CHECKFREERUN,BM_SETCHECK,1,0);
if (dwBoardID == BD_PCI1784)
{
EnableWindow(GetDlgItem(hDlg, IDC_CHECKRESET), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_CHECKFREERUN), FALSE);
SetWindowText(GetDlgItem(hDlg, IDC_RESETVALUE0), "0000 0000 H");
SetWindowText(GetDlgItem(hDlg, IDC_RESETVALUE8), "8000 0000 H");
}
else
{
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");
}
// Reset value
if (usResetValue == 0) // reset value = 0
CheckRadioButton(hDlg,IDC_RESETVALUE0,IDC_RESETVALUE8,IDC_RESETVALUE0);
else
CheckRadioButton(hDlg,IDC_RESETVALUE0,IDC_RESETVALUE8,IDC_RESETVALUE8);
// Set InputMode
SendDlgItemMessage(hDlg,IDC_INPUTMODE,CB_SELECTSTRING,(WPARAM)-1,
(LPARAM)((LPSTR)szInputMode[usInputMode]));
// Set LatchSource
SendDlgItemMessage(hDlg,IDC_LATCHSOURCE,CB_SELECTSTRING,(WPARAM)-1,
(LPARAM)((LPSTR)szLatchSource[usLatchSource]));
// Set digital filter
SendDlgItemMessage(hDlg,IDC_DIGFILTERCLOCK,CB_SELECTSTRING,(WPARAM)-1,
(LPARAM)((LPSTR)szDigFilterClock[usDigtalFilter]));
return TRUE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -