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

📄 dadma.c

📁 研华da采集例程
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
 ************************************************************************
 *  Program     : DADMA.C                                               *
 *  Description : Demo program for Windows with DMA data transfer       *
 *  Revision    : 1.00                                                  *
 *  Date        : 12/10/1997                  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
 *
 *   WaveDlgProc() - Process the dialog box messages for waveform setting
 */


#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 "..\..\..\include\os.h"
#include "resource.h"
#include "dadma.h"

typedef struct {
    int wWaveform;
    float fMagnitude;
    float fOffset;
    int wPeriod;
} SWAVE, FAR *LPSWAVE;

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

static      DEVFEATURES        DevFeatures;       // structure for device features
static      PT_DeviceGetFeatures  ptDevFeatures;  // Devfeatures table
static      PT_FAODmaStart     ptFAODmaStart;     // FAODMAStart table
static      PT_FAOLoad         ptFAOLoad;         // FAOLoad table
static      PT_FAOScale        ptFAOScale;        // FAOScale table
static      PT_FAOCheck        ptFAOCheck;        // FAOCheck table
static      PT_EnableEvent     ptEnableEvent;     // Enable event
static      PT_CheckEvent      ptCheckEvent;      // Check event
static      PT_AllocateDMABuffer  ptAllocateDMABuffer;  // buffer table

HANDLE      hEvent;
HANDLE      hThreadHandle;
DWORD       dwThreadID;

char        szErrMsg[80];               // Use for MESSAGEBOX function
char        szBuffer[40];               // Temperatory buffer
char        szDescript[100];            // Description device
LRESULT     ErrCde;                     // Return error code

static  char far szWaveform[3][10] = {"SINE","TRIANGLE","SQUARE"};

static  LONG   DriverHandle = (LONG)NULL;         // driver handle
static  ULONG  dwDeviceNum;                       // Device number
static  USHORT gwSelectDevice = FALSE;  // have never selected "Select Device" button
static  USHORT gwDevice = 0;            // device index
static  DWORD  gdwPacerRate = 10000;    // pacer rate
static  ULONG  gulConvNum = 10000;      // conversion number
static  USHORT gwStartChl = 0, gwStopChl = 0;  // scan channels
static  USHORT gwExtTrig = 0;           // external or internal trigger
static  USHORT gwCyclicMode = 1;        // cyclic or non-cylic mode
static  USHORT gwBufferMode = 0;        // buffer: single or double
static  USHORT gwEvtFlag = 1;           // event enable(0)

static  USHORT gwActiveBuf = 0;     // return by FAOCheck
static  USHORT gwOverrun   = 0;     // return by FAOCheck
static  USHORT gwStopped   = 0;     // return by FAOCheck
static  ULONG gulRetrieved = 0;     // return by FAOCheck
static  USHORT gwHalfReady = 0;     // return by FAOCheck

static  ULONG  gwActualBufSize = 0; // actual allocate buffer size

HGLOBAL hBuf, hVoltageBuf, hBinaryBuf;
FLOAT  far * lpVoltageBuf;
USHORT far * lpBinaryBuf;
USHORT far * lpBuf;

static  SWAVE sWaveA = {0,          // output waveform A index
                        2.00f,      // the magnitude of output waveform A
                        2.00f,      // the offset of output waveform A
                        500},       // the points of one period
              sWaveB = {1,          // output waveform B index
                        1.00f,      // the magnitude of output waveform B
                        3.00f,      // the offset of output waveform B
                        500},       // the points of one period
              sWaveC = {2,          // output waveform C index
                        2.00f,      // the magnitude of output waveform C
                        2.00f,      // the offset of output waveform C
                        500};       // the points of one period

static  USHORT gwRepeatCount = 0;  // number of dma terminal count
static  HWND hwndStopButton, hwndStatusButton;
static  HANDLE  hInstance ;
HMENU hMenu;
LONG pBuffer;

BOOL InitApplication(HANDLE hInstance);
BOOL InitInstance(HANDLE hInstance, int nCmdShow);
long FTYPE MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int  FTYPE WinMain(HANDLE hInstance, HANDLE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow);
static  FARPROC lpfnConfigDlgProc, lpfnWaveDlgProc;
void SetRealBuffer(float far *lpBuf, long num, LPSWAVE lpWave);

void UserThread();
void adInterruptEvent();
void adBufChangeEvent();
void adOverrunEvent();
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 : Analog Output with 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          */
{

    switch (message)
    {
        case WM_INITDIALOG :

            if (!gwSelectDevice)
            {
                // Step 1: Select Device
                ErrCde = DRV_SelectDevice(hDlg, FALSE, &dwDeviceNum, szDescript);
                if (ErrCde != SUCCESS)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                    return 0;
                }

                gwSelectDevice = TRUE;
            }

			// Show device description to Button text
            SendDlgItemMessage(hDlg, IDC_DeviceSelect, WM_SETTEXT,
               50, (LPARAM)(LPSTR)szDescript);



            // Initialize Conversion Number
            ltoa(gulConvNum, szBuffer, 10);
            SendDlgItemMessage(hDlg, IDC_CONVERSION, EM_REPLACESEL, 0,
                            (LPARAM)((LPSTR)szBuffer));

            // Initialize Pacer Rate
            ltoa(gdwPacerRate, szBuffer, 10);
            SendDlgItemMessage(hDlg, IDC_PACER, EM_REPLACESEL, 0,
                            (LPARAM)((LPSTR)szBuffer));

            if (gwExtTrig)
               EnableWindow(GetDlgItem(hDlg, IDC_PACER), FALSE);
            else
               EnableWindow(GetDlgItem(hDlg, IDC_PACER), TRUE);

            // Initialize Start Channel
            itoa(gwStartChl, szBuffer, 10);
            SendDlgItemMessage(hDlg, IDC_ESTART, EM_REPLACESEL, 0,
                            (LPARAM)((LPSTR)szBuffer));

            // Initialize Triggering
            CheckRadioButton(hDlg, IDC_INTTRIG, IDC_EXTTRIG,

⌨️ 快捷键说明

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