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

📄 didma.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 4 页
字号:
         lStartDisplay = gdwStartPt;
         ultoa(lStartDisplay, szBuffer, 10);
         SetWindowText(GetDlgItem(hDlg,IDC_FROM),(LPCTSTR)szBuffer);

         lEndDisplay = gdwStopPt;
         ultoa(lEndDisplay, szBuffer, 10);
         SetWindowText(GetDlgItem(hDlg,IDC_TO),(LPCTSTR)szBuffer);
         
         ultoa(dwCount, szBuffer, 10);
         SetWindowText(GetDlgItem(hDlg,IDC_TOTAL),(LPCTSTR)szBuffer);
         
         for(i = lStartDisplay; (i <= lEndDisplay) && (i < dwCount); ++i)
         {
            if ( gdwDataWidth == 3 )     // 8-bit
            {
               sprintf(szBuffer, "Buf[%ld] = %2x", i, ((BYTE far *)hUserBuf)[i]);
            }
            else if ( gdwDataWidth == 2 )  // 16-bit
            {
               sprintf(szBuffer, "Buf[%ld] = %4x",i, ((USHORT far *)hUserBuf)[i]);
            }
            else                           // 32-bit
            {
               sprintf(szBuffer, "Buf[%ld] = %8x",i, ((ULONG far *)hUserBuf)[i]);
            }


            if(SendDlgItemMessage(hDlg, IDC_DATALIST, LB_ADDSTRING, 0,
               (LPARAM)(LPSTR)szBuffer) == LB_ERRSPACE)
            {
               MessageBox(hWnd,(LPCSTR)"Not Enough Memory",
                  "Display Data",MB_OK);
               return TRUE;
            }
         }

        return TRUE;
		}

    case WM_COMMAND :
        switch (LOWORD(wParam))
        {
        case IDC_BACKBUTTON :
           break;			
        case IDC_BACKWARD :
			  break;
		  case IDC_FORWARD :
           break;			
        case IDC_FORWARDBUTTON :
           break;
        case IDC_UPDATE :
			 break;
        case IDOK :
        case IDCANCEL :
            EndDialog(hDlg, 0);
            return TRUE;
        }
        break;
    }
    return FALSE ;
}

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

    PURPOSE:  Processes dialog box messages for display range
******************************************************************************/

BOOL FTYPE DisplayDlgProc(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 :

        // Initialize Start Point
        ultoa(gdwStartPt, szBuffer, 10);

        SendDlgItemMessage(hDlg, IDC_ESTAPT, EM_REPLACESEL, 0,
            (LPARAM)((LPSTR)szBuffer));

        // Initialize Stop Point
        ultoa(gdwStopPt, szBuffer, 10);

        SendDlgItemMessage(hDlg, IDC_ESTOPT, EM_REPLACESEL, 0,
            (LPARAM)((LPSTR)szBuffer));

        return TRUE;

    case WM_COMMAND :
        switch (LOWORD(wParam))
        {
        case IDOK :
        case IDCANCEL :
            EndDialog(hDlg, 0);
            return TRUE;

        case IDC_ESTAPT :

            if (HIWORD(wParam) == EN_KILLFOCUS)
            {
                if(SendDlgItemMessage(hDlg, IDC_ESTAPT,
                    EM_GETMODIFY, 0, 0))
                {
                    SendDlgItemMessage(hDlg, IDC_ESTAPT,
                        WM_GETTEXT, 10, (LPARAM)(LPSTR)szBuffer) ;
                    gdwStartPt = (USHORT)atoi(szBuffer);
                    SendDlgItemMessage(hDlg, IDC_ESTAPT,
                        EM_SETMODIFY, FALSE, 0) ;
                }
            }
            return TRUE;

        case IDC_ESTOPT :
            if (HIWORD(wParam) == EN_KILLFOCUS)
            {
                if(SendDlgItemMessage(hDlg, IDC_ESTOPT,
                    EM_GETMODIFY, 0, 0))
                {
                    SendDlgItemMessage(hDlg, IDC_ESTOPT,
                        WM_GETTEXT, 10, (LPARAM)(LPSTR)szBuffer) ;
                    gdwStopPt = atol(szBuffer);
                    SendDlgItemMessage(hDlg, IDC_ESTOPT,
                        EM_SETMODIFY, FALSE, 0) ;
                }
            }
            return TRUE;
        }
        break;
    }
    return FALSE ;
}

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

    PURPOSE:  Processes messages

    MESSAGES:

        WM_CREATE     - create window
        WM_COMMAND    - application menu (About dialog box)
        WM_DESTROY    - destroy window
******************************************************************************/

long FTYPE MainWndProc(hWnd, message, wParam, lParam)
HWND     hWnd;                            /* window handle                   */
unsigned message;                         /* type of message                 */
WPARAM   wParam;                          /* additional information          */
LPARAM   lParam;                          /* additional information          */
{

    TEXTMETRIC tm;
    HDC     hdc;
    static  int cxChar, cyChar;
    DWORD  dwUserBufferSize;
    DWORD  dwCount;
    ULONG  dwFdiStatus, dwRetrieved;
    char    *szStr, szDest[100], szTemp[50];
    double  ratio;
    int     dec, sign;
//    USHORT  temp;


    switch (message)
    {
    case WM_CREATE:

        hMenu = GetMenu(hWnd);

        hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;

        lpfnConfigDlgProc = MakeProcInstance (ConfigDlgProc, hInstance) ;

        lpfnShowDataDlgProc = MakeProcInstance (ShowDataDlgProc, hInstance) ;

        lpfnDisplayDlgProc = MakeProcInstance (DisplayDlgProc, hInstance) ;
        
        hdc = GetDC(hWnd);
        SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
        GetTextMetrics(hdc, &tm);
        cxChar = tm.tmAveCharWidth;
        cyChar = tm.tmHeight + tm.tmExternalLeading;
        ReleaseDC(hWnd, hdc);

        //Create stop button and status button
        hwndStopButton = CreateWindow("button", "Stop",
            (DWORD)(WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON),
            0, 0, 8*cxChar, 3*cyChar/2, hWnd,
            (HMENU)IDC_STOPBUTTON,
            (HANDLE)hInstance, (LPVOID)NULL);

        hwndStatusButton = CreateWindow("button", "Status",
            (DWORD)(WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON),
            8*cxChar, 0, 8*cxChar, 3*cyChar/2, hWnd,
            (HMENU)IDC_STATUSBUTTON,
            (HANDLE)hInstance, (LPVOID)NULL);

        EnableWindow(hwndStopButton, FALSE);
        EnableWindow(hwndStatusButton, FALSE);

        return 0 ;

    case WM_COMMAND:     /* message: from application menu */

        hMenu = GetMenu(hWnd);

        switch(LOWORD(wParam))
        {
            // Show the dialog box of the input parameter
        case IDM_SETTING :

            DialogBox (hInstance, MAKEINTRESOURCE(IDD_SETTING),
                hWnd, lpfnConfigDlgProc) ;
            return 0;

            // Show the dialog box for the range of data to display
        case IDM_DISPLAY:

            DialogBox (hInstance, MAKEINTRESOURCE(IDD_DISPLAY),
                hWnd, lpfnDisplayDlgProc) ;

            return 0;

        case IDM_RUN :

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

            // Step 2: 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;
            }

            // Step 3: Allocate memory for Fast DI data
            //********Version 2.0B*******************
            //  As with DMA transfers, it's better to start the buffer at
            //  page boundary(4096 Bytes). If the buffers used for the 
            //  DMA operation is aligned on addresses of page boundary, user 
            //  can get exact page change event(just set the Count of 
            //  PT_EnableEvent to 2048, it means 1 page). Once Interrupt
            //  event occurs, it means 2048 samples(4096 byes) are available.
            //  
            //  It is why we changed the memory allocation policy from
            //  Solution A to solution B at Version 2.0B.
            //
            //  At the same time, the buffer that receives the samples must
            //  be a multiple of 4096 bytes(2048 samples). In cycle mode, if
            //  you are interested in buffer change event, the minimal buffer
            //  is 8192 bytes(4096 samples).
            //****************************************
            
            //*************** 2.0B note *********************
            // Device driver's ReadFile will Error(System resource insufficient
            // when use GlobalAlloc function to allocate more than 16M byte memory.
            // So we use VirtualAlloc, but not test in Win9x platform.

            dwUserBufferSize = (DWORD)(Mega * pow((double)2, (double)gdwUserBufferSize));

            /*
            // Solution A:
            if((hUserBuf = GlobalAlloc(GPTR,
                 dwUserBufferSize )) == 0)
            {
                MessageBox(hWnd,"Not enough memory for buffer ",
                    "High Speed",MB_OK);
                DRV_DeviceClose((LONG far *)&DriverHandle);
                return 0;
            }
            */
               
            // Solution B:
            // Win9x seems don't support the routine well.
            // so it's recommended only to use it at WinNT/2K.
            hUserBuf = VirtualAlloc( NULL,                    // region to reserve or commit
                                     dwUserBufferSize,          // size of region
                                     MEM_COMMIT,  // type of allocation
                                     PAGE_READWRITE );        // type of access protection
            
            if ( NULL==hUserBuf )                       
            {
                MessageBox( hWnd, "Not enough memory for buffer", "High Speed",MB_OK );
                DRV_DeviceClose((LONG *)&DriverHandle);
                return 0;
            }
            // End of solution B.
            

            // Step 4: Enable event feature
            ptEnableEvent.Enabled = 1;
            ptEnableEvent.Count = 1;

            ptEnableEvent.EventType = ADS_EVT_DI_OVERRUN;    // Overrun event 
            ErrCde = DRV_EnableEvent(DriverHandle, &ptEnableEvent);
            ptEnableEvent.EventType = ADS_EVT_DI_LOBUFREADY; // Low buffer ready event
            ErrCde = DRV_EnableEvent(DriverHandle, &ptEnableEvent);
            ptEnableEvent.EventType = ADS_EVT_DI_HIBUFREADY; // High buffer ready event.
            ErrCde = DRV_EnableEvent(DriverHandle, &ptEnableEvent);
            ptEnableEvent.EventType = ADS_EVT_DI_TERMINATED; // Fast DI function terminated
            ErrCde = DRV_EnableEvent(DriverHandle, &ptEnableEvent);

            if (ErrCde != 0)
            {
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                // GlobalFree( hUserBuf );                
                // Decommit first, then release the memory
                VirtualFree( hUserBuf, dwUserBufferSize, MEM_DECOMMIT ); 
                VirtualFree( hUserBuf, 0, MEM_RELEASE);
                            

⌨️ 快捷键说明

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