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

📄 didma.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
 ******************************************************************************
 *  Program     : DIBMDMA.C                                                   *
 *  Description : Demo program for Windows with DMA data transfer             *
 *  Revision    : 2.0B                                                        *
 *  Date        : 04/18/2003                  Advantech Beijing R&D Center    *
 ******************************************************************************
 *
 * FUNCTIONS:
 *
 *   InitApplication() - Initializes window data and registers window class
 *
 *   InitInstance()    - Saves instance handle and creates main window
 *
 *   MainWndProc()     - Process main window messages
 *
 *   ShowDataDlgProc() - Process the dialog box messages for acquired data
 *
 *   DisplayDlgProc()  - Process the dialog box messages for display range
 *
 *   ConfigDlgProc()   - Process the dialog box messages for configuration
 */

#include <windows.h>            /* required for all Windows applications */
#include <stdlib.h>
#include <windowsx.h>
#include <commdlg.h>
#include <dos.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "..\..\..\Include\driver.h"
#include "resource.h"
#include "DIdmaBm.h"

// ----------------------------------------------------------------------------
// CONSTANT DEFINITION
// ----------------------------------------------------------------------------
#define     MAX_DEVICES     100
#define     Mega            0x100000


// ----------------------------------------------------------------------------
// GLOBAL VARIABLES
// ----------------------------------------------------------------------------
HANDLE      hInst;                      // current instance
HWND        hWnd;                       // main window handle
HDC         hdc;
RECT        rect;

static      DEVFEATURES           DevFeatures;       // structure for device features
static      PT_DeviceGetFeatures  ptDevFeatures;  // Devfeatures table

static      PT_EnableEvent     ptEnableEvent;     // Enable event
static      PT_CheckEvent      ptCheckEvent;      // Check event

HANDLE      hThreadHandle;
DWORD       dwThreadID;

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

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

BOOL        bThreadflag = FALSE;
BOOL        bThreadloop = FALSE;

static  ULONG  dwDeviceNum;                       // Device number
char        szDeviceName[80];				           // Temporary buffer

static  LONG   DriverHandle = (LONG)NULL;         // driver handle
static  USHORT gwDevice = 0, gwSubDevice = 0;     // device index
static  SHORT  gnNumOfDevices, gnNumOfSubdevices; // number of installed devices
static  ULONG  gdwStartPt = 0, gdwStopPt = 99;    // display points

// global 
DWORD  gdwBoardId;
DWORD  gdwDataWidth;
DWORD  gdwOperationMode;
DWORD  gdwPatternMatchValue;
DWORD  gdwDIStartMode;
DWORD  gdwDITriggerSource;
DWORD  gdwDIStopMode;
DWORD  gdwCounterValue[3];
DWORD  gdwDmaMode = 0;
DWORD  gdwCount;
DWORD  gdwUserBufferSize = 1;
SHORT  gdwCyclicMode = 0;    // cyclic or non-cyclic mode
HGLOBAL hDisplayBuf;
LPVOID  hUserBuf;

BOOL InitApplication(HANDLE hInstance);
BOOL InitInstance(HANDLE hInstance, int nCmdShow);
long FTYPE MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int  FTYPE WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
static FARPROC lpfnConfigDlgProc, lpfnShowDataDlgProc, lpfnDisplayDlgProc,
    lpfnGainListDlgProc;

static  HWND hwndStopButton, hwndStatusButton;
static  HANDLE  hInstance;
HMENU hMenu;

ULONG gdwDIBufferChangeCounter = 0;  // counter by UserThread
ULONG gdwDIOverRunCounter = 0;
ULONG gdwDITerminateCounter = 0;
BOOL  gbConfigured = FALSE;

ULONG gulOverrunTime  = 10000;  // 0 = INFINITE
char  szCounter[40];

void UserThread();
void adTerminateEvent();

/******************************************************************************
    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(NULL, IDI_APPLICATION);
    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 : FDI Busmaster DMA Data Transfer" ,
        /* 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(hDlg, message, wParam, lParam)
HWND hDlg;                              /* window handle                   */
unsigned message;                       /* type of message                 */
WPARAM wParam;                          /* additional information          */
LPARAM lParam;                          /* additional information          */
{
    int     i;
    char    szBuffer[80];
    char    szTemp[80];
	 BOOL bEnable;

    switch (message)
    {
    case WM_INITDIALOG :
       
        // Step 1: Select Device
		if(!gbConfigured)
		{
			ErrCde = DRV_SelectDevice( hDlg, FALSE, &dwDeviceNum, szDeviceName);
			if (ErrCde != SUCCESS)
			{
				DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
				MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
				return 0;
			}
		}
		SetDlgItemText(hDlg,IDC_DEVICE_NAME,szDeviceName);

        // Step 2: Open Device
        ErrCde = DRV_DeviceOpen(dwDeviceNum, (LONG far *)&DriverHandle);
        if (ErrCde != SUCCESS)
        {
            DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
            MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
            return 0;
        }


        // Step 3: 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(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
            DRV_DeviceClose((LONG far *)&DriverHandle);
            return 0;
        }

        if ( !gbConfigured )
        {

            gdwCount = sizeof(DWORD);
            // Get board ID
            ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_BoardID, &gdwBoardId, &gdwCount);
		      // 32DI(0)/32DO(1)/ 16 DIO(2) / 8DIO(3)
		      ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DioFdioDirection, &gdwDataWidth, &gdwCount);
		      if ( gdwDataWidth == 1 )
		      {
			      gdwDataWidth = 0; // 32DI
		      }
			   // Get the Operation Mode Normal(0) 8255(1) Burst(2)
		      ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DiOperationMode, &gdwOperationMode, &gdwCount);	
		      // Software(1), External trigger(2) Pattern Match(3)
		      ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DiStartMethod, &gdwDIStartMode, &gdwCount);	
            // Get pattern match value
            ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DiPatternMatchValue, &gdwPatternMatchValue, &gdwCount);
		      // 30MHz(1), 15Mhz(2), 10MHz(3), Counter 0 OUT (4), External (5).
		      ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DiPacerSource, &gdwDITriggerSource, &gdwCount);
		      // Software(1), External trigger(2) Pattern Match(3)
		      ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DiStopMethod, &gdwDIStopMode, &gdwCount);
            if( BD_MIC3755 == gdwBoardId )
            {
               // Slave(0), master(1)
               ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DiTransferRequestMode, &gdwDmaMode, &gdwCount);
            }
		      // 82C54 Counter 0-2 value
		      gdwCount = sizeof(gdwCounterValue);
		      ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_CounterCountValue, &gdwCounterValue, &gdwCount);
   
		      if ( ErrCde != SUCCESS )
            {
               DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
               MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
               DRV_DeviceClose((LONG far *)&DriverHandle);
               return 0;
            }
            DRV_DeviceClose((LONG far *)&DriverHandle);
            gbConfigured = TRUE;
        }


        // Step 4: Initialize Input Range List Combobox with device features

		  // initialize Operation mode
		switch(gdwOperationMode)
		{
		case 0:

⌨️ 快捷键说明

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