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

📄 dodmabm.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
******************************************************************************
*  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 "..\..\..\Include\os.h"
#include "resource.h"
#include "DOdmaBm.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;
DWORD dwExitCode;

static  ULONG  dwDeviceNum;// Device number
char   szDeviceName[100] ;                      

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  gdwOperationMode; 
DWORD  gdwDigitalOut;
DWORD  gdwDataWidth;
DWORD  gdwDOStartMode;
DWORD  gdwDOTriggerSource;
DWORD  gdwDOStopMode;
DWORD  gdwCounterValue[3];
DWORD  gdwDmaMode = 1;
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 gdwDOBufferChangeCounter = 0;  // counter by UserThread
ULONG gdwDOUnderRunCounter = 0;
ULONG gdwDOTerminateCounter = 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 : FDO 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[40];
	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);
			//Get the Operation Mode Normal(0) 8255(1) Burst(2)
			ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DoOperationMode, &gdwOperationMode, &gdwCount);	
			// 32DI(0)/32DO(1)/ 16 DIO(2) / 8DIO(3)
			ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DioFdioDirection, &gdwDataWidth, &gdwCount);
			if ( gdwDataWidth == 0 )
			{
				gdwDataWidth = 1; // 32DO
			}
			// Software(1), External trigger(2)
			ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DoStartMethod, &gdwDOStartMode, &gdwCount);	
			if( gdwDOStartMode == 3 )               // pattern match
			{
				gdwDOStartMode = 1;                 // software trigger
			}	
			// 30MHz(1), 15Mhz(2), 10MHz(3), Counter 0 OUT (4), External (5).
			ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DoPacerSource, &gdwDOTriggerSource, &gdwCount);
			// Software(1), External trigger(2)
			ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DoStopMethod, &gdwDOStopMode, &gdwCount);
         if( BD_MIC3755 == gdwBoardId )
         {
            // Slave(0), master(1)
            ErrCde = DRV_DeviceGetProperty(DriverHandle, CFG_DoTransferRequestMode, &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:
		    CheckRadioButton(hDlg, IDC_NORMAL, IDC_BURST,IDC_NORMAL);
          if( BD_MIC3755 == gdwBoardId )
          {
             EnableWindow(GetDlgItem(hDlg, IDC_DMA_SLAVE), FALSE);
             EnableWindow(GetDlgItem(hDlg, IDC_DMA_MASTER), FALSE);
          } 
			break;
		case 1:
		    CheckRadioButton(hDlg, IDC_NORMAL, IDC_BURST,IDC_8255);
          if( BD_MIC3755 == gdwBoardId )
          {
             EnableWindow(GetDlgItem(hDlg, IDC_DMA_SLAVE), FALSE);
             EnableWindow(GetDlgItem(hDlg, IDC_DMA_MASTER), FALSE);
          } 
			break;
		case 2:
		    CheckRadioButton(hDlg, IDC_NORMAL, IDC_BURST,IDC_BURST);
          if( BD_MIC3755 == gdwBoardId )
          {
             EnableWindow(GetDlgItem(hDlg, IDC_DMA_SLAVE), TRUE);
             EnableWindow(GetDlgItem(hDlg, IDC_DMA_MASTER), TRUE);
          } 
			break;
		default:
			break;
		}

		// initialize the Digital output value 
		sprintf(szBuffer,"%X",gdwDigitalOut);
		SetDlgItemText(hDlg,IDC_DIGITALOUT,szBuffer);
		
		// initialize start type
		CheckRadioButton(hDlg, IDC_SOFT_START, IDC_EXTERNAL_START,
			(gdwDOStartMode-1)?IDC_EXTERNAL_START:IDC_SOFT_START);
		// initialize stop type
		CheckRadioButton(hDlg, IDC_SOFT_STOP, IDC_EXTERNAL_STOP,
			(gdwDOStopMode-1)?IDC_EXTERNAL_STOP:IDC_SOFT_STOP);

      // Initialize DMA mode
      switch(gdwDmaMode)
      {
      case 0:
         CheckRadioButton(hDlg, IDC_DMA_SLAVE, IDC_DMA_MASTER, IDC_DMA_SLAVE);
         break;
      case 1:
         CheckRadioButton(hDlg, IDC_DMA_SLAVE, IDC_DMA_MASTER, IDC_DMA_MASTER);
         break;
      default:
         break;
      }
      if( BD_MIC3755 != gdwBoardId )
      {
         EnableWindow(GetDlgItem(hDlg, IDC_DMA_SLAVE), FALSE);
         EnableWindow(GetDlgItem(hDlg, IDC_DMA_MASTER), FALSE);
      }
		
		// initialize trigger source combo
		// Add trigger source control selection strings
		// The trigger source X'tal 30MHz - 10Mhz
		SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_ADDSTRING, 0,(LPARAM)((LPSTR)"30 MHz"));
		SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_ADDSTRING, 1,(LPARAM)((LPSTR)"15 MHz"));
		SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_ADDSTRING, 2,(LPARAM)((LPSTR)"10 MHz"));
		SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_ADDSTRING, 3,(LPARAM)((LPSTR)"Counter 1"));
		SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_ADDSTRING, 4,(LPARAM)((LPSTR)"External"));
      if( BD_MIC3755 == gdwBoardId )
      {
         SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_ADDSTRING, 5,(LPARAM)((LPSTR)"12 MHz"));
         SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_ADDSTRING, 6,(LPARAM)((LPSTR)"20 MHz"));
      }
		
		// 30MHz(1), 15Mhz(2), 10Mhz(3), Counter 0(4), External (5)
		SendDlgItemMessage(hDlg, IDC_LIST_TRIGGER_SOURCE, LB_SETCURSEL, gdwDOTriggerSource-1, 0);
		
        // Initialize Counter1 value
        itoa((int)gdwCounterValue[1], szBuffer, 10);
        SendDlgItemMessage(hDlg, IDC_EDIT_COUNTER0_VALUE, EM_REPLACESEL, 0,
            (LPARAM)((LPSTR)szBuffer));
		if ( gdwDOTriggerSource != 4 )
		{
			EnableWindow( GetDlgItem(hDlg, IDC_EDIT_COUNTER0_VALUE), FALSE);
		}
		
        // Initialize Repeated Mode
        CheckRadioButton(hDlg, IDC_CYCLIC, IDC_NONCYCLIC,
			(gdwCyclicMode)?IDC_CYCLIC:IDC_NONCYCLIC);
		
        // Initialize Data Width
        CheckRadioButton(hDlg, IDC_32DO, IDC_8DIO, (IDC_32DO + gdwDataWidth - 1) );
		
		// initialize User Buffer
		// Add trigger source control selection strings		  
		SendDlgItemMessage(hDlg, IDC_LIST_USER_BUFFER, LB_ADDSTRING, 0,(LPARAM)((LPSTR)"1  Mega"));
		SendDlgItemMessage(hDlg, IDC_LIST_USER_BUFFER, LB_ADDSTRING, 1,(LPARAM)((LPSTR)"2  Mega"));
		SendDlgItemMessage(hDlg, IDC_LIST_USER_BUFFER, LB_ADDSTRING, 2,(LPARAM)((LPSTR)"4  Mega"));
		SendDlgItemMessage(hDlg, IDC_LIST_USER_BUFFER, LB_ADDSTRING, 3,(LPARAM)((LPSTR)"8  Mega"));
		SendDlgItemMessage(hDlg, IDC_LIST_USER_BUFFER, LB_ADDSTRING, 4,(LPARAM)((LPSTR)"16 Mega "));
		SendDlgItemMessage(hDlg, IDC_LIST_USER_BUFFER, LB_SETCURSEL, gdwUserBufferSize, 0);

        
        return TRUE;
		
    case WM_COMMAND :

⌨️ 快捷键说明

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