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

📄 dadmabm.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 4 页
字号:

long FTYPE MainWndProc(hWnd, message, wParam, lParam)
HWND hWnd;                              /* window handle                   */
unsigned message;                       /* type of message                 */
WPARAM wParam;                            /* additional information          */
LPARAM lParam;                            /* additional information          */
{
    int     dec, sign;
    TEXTMETRIC tm;
    HDC     hdc;
    static  int cxChar, cyChar;
    char    *szStr, szDest[100], szTemp[50];
    double  ratio;
    DWORD   dwExitCode;

    switch (message)
      {
       case WM_CREATE:

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

            lpfnConfigDlgProc = MakeProcInstance (ConfigDlgProc, hInstance) ;

            lpfnWaveDlgProc = MakeProcInstance (WaveDlgProc, 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;

            case IDM_WAVEFORM :

                DialogBox (hInstance, MAKEINTRESOURCE(IDD_WAVEFORM),
                         hWnd, lpfnWaveDlgProc) ;
                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: Allocate memory used by driver
                if((hBuf=(USHORT far *)GlobalAlloc(GPTR,
                    sizeof(USHORT) * gulConvNum)) == 0)
                {
                    MessageBox(hWnd,"Not enough memory for buffer ",
                        "High Speed",MB_OK);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }

                // Step 3: Allocate memory for real voltage
                if((hVoltageBuf=(FLOAT far *)GlobalAlloc(GPTR,
                    sizeof(FLOAT) * gulConvNum)) == 0)
                {
                    MessageBox(hWnd,"Not enough memory for buffer ",
                        "High Speed",MB_OK);
                    GlobalFree(hBuf);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }

                // Step 4: Allocate memory for binary data
                if((hBinaryBuf=(USHORT far *)GlobalAlloc(GPTR,
                    sizeof(USHORT) * gulConvNum)) == 0)
                {
                    MessageBox(hWnd,"Not enough memory for buffer ",
                        "High Speed",MB_OK);
                    GlobalFree(hBuf);
                    GlobalFree(hVoltageBuf);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }

                // Step 5: Allocate DMA buffer for DMA transfer
                ptAllocateDMABuffer.CyclicMode = gwCyclicMode;
                ptAllocateDMABuffer.RequestBufSize = gulConvNum * 2;
                ptAllocateDMABuffer.ActualBufSize = &gwActualBufSize;
                ptAllocateDMABuffer.buffer = &pBuffer;
                if ((ErrCde = DRV_AllocateDMABuffer(DriverHandle,
                    (LPT_AllocateDMABuffer)&ptAllocateDMABuffer)) != SUCCESS)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                    GlobalUnlock(hBuf); GlobalFree(hBuf);
                    GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                    GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }

                // Lock down buffer
                lpBuf        = (USHORT far *)GlobalLock(hBuf);
                lpBinaryBuf  = (USHORT far *)GlobalLock(hBinaryBuf);
                lpVoltageBuf = (FLOAT far  *)GlobalLock(hVoltageBuf);

                // set real voltage to hVoltageBuf
                SetRealBuffer(lpVoltageBuf, gulConvNum, &sWaveA);

                // Step 6: call FAOScale for transfer voltage to binary data
                ptFAOScale.VoltArray = lpVoltageBuf;
                ptFAOScale.BinArray  = lpBuf;
                ptFAOScale.chan      = gwStartChl;
                ptFAOScale.count     = gulConvNum;
                if ((ErrCde = DRV_FAOScale(DriverHandle,
                    (LPT_FAOScale)&ptFAOScale)) != 0)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                    GlobalUnlock(hBuf); GlobalFree(hBuf);
                    GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                    GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                    DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }


                // Step 7: Enable event feature
                ptEnableEvent.EventType = ADS_EVT_AO_INTERRUPT; 
                ptEnableEvent.Enabled = gwEvtFlag;
                ptEnableEvent.Count   = 1;
                if ((ErrCde = DRV_EnableEvent(DriverHandle,
                    (LPT_EnableEvent)&ptEnableEvent)) != 0)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                    GlobalUnlock(hBuf); GlobalFree(hBuf);
                    GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                    GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                    DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }
                ptEnableEvent.EventType = ADS_EVT_AO_BUFCHANGE;
                if ((ErrCde = DRV_EnableEvent(DriverHandle,
                   (LPT_EnableEvent)&ptEnableEvent)) != 0)
                {
                   DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                   MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                   GlobalUnlock(hBuf); GlobalFree(hBuf);
                   GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                   GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                   DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                   DRV_DeviceClose((LONG far *)&DriverHandle);
                   return 0;
                }

                ptEnableEvent.EventType = ADS_EVT_AO_TERMINATED;
                if ((ErrCde = DRV_EnableEvent(DriverHandle,
                   (LPT_EnableEvent)&ptEnableEvent)) != 0)
                {
                   DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                   MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                   GlobalUnlock(hBuf); GlobalFree(hBuf);
                   GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                   GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                   DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                   DRV_DeviceClose((LONG far *)&DriverHandle);
                   return 0;
                }
  
                ptEnableEvent.EventType = ADS_EVT_AO_UNDERRUN;
                if ((ErrCde = DRV_EnableEvent(DriverHandle,
                   (LPT_EnableEvent)&ptEnableEvent)) != 0)
                {
                   DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                   MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                   GlobalUnlock(hBuf); GlobalFree(hBuf);
                   GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                   GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                   DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                   DRV_DeviceClose((LONG far *)&DriverHandle);
                   return 0;
                }
                // Step 8: call FAODmaStart for start action
                // Default steting - conversion number = 1000
                // start channel = 1, pacer rate = 1000 Hz
                // cyclic , enable event
                ptFAODmaStart.buffer     = (LONG far *)lpBuf;        // analog output data
                ptFAODmaStart.TrigSrc    = gwExtTrig;    // triggering
                ptFAODmaStart.SampleRate = gdwPacerRate; // pacer rate = 1K
                ptFAODmaStart.chan       = gwStartChl;   // start channel
                ptFAODmaStart.count      = gulConvNum;   // DA conversion number

                if ((ErrCde = DRV_FAODmaStart(DriverHandle,
                    (LPT_FAODmaStart)&ptFAODmaStart)) != 0)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                    GlobalUnlock(hBuf); GlobalFree(hBuf);
                    GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                    GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                    DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }

                gwRepeatCount = 0;				
            
               // if event feature is enabled , then create one thread.
                if (gwEvtFlag)
                {
                    hThreadHandle = CreateThread(0, 0,
                        (LPTHREAD_START_ROUTINE) UserThread,
                        0, 0, (LPDWORD)&dwThreadID);
					     bThreadloop = TRUE;
                }

                EnableMenuItem(hMenu, IDM_RUN, MF_DISABLED | MF_BYCOMMAND);

                // Enable stop and status buttons
                EnableWindow(hwndStopButton, TRUE);

                return 0;

                //end IDM_RUN

           case IDC_STOPBUTTON:
			   {
				
                
				//Warning: If you enable the events, DONOT terminate the CheckEvent
				//thread here, UNLESS the Terminate Event is not enabled. You should
				//allow the CheckEvent thread to obtaine the Terminate Event generated
				//by DRV_FAOTerminate, then exit the thread cleanly. Otherwise you will
				//leave a signaled event that can never be reset.
				
                // Stop D/A conversion for high speed
                if ((ErrCde = DRV_FAOTerminate(DriverHandle)) != 0)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                    GlobalUnlock(hBuf); GlobalFree(hBuf);
                    GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                    GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                    DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }
				
				if (!gwEvtFlag){
			         adTerminateEvent();
				}
                
			   }
                return 0;

           case IDC_STATUSBUTTON:

                // Get Interrupt transfer status
                ptFAOCheck.ActiveBuf    = &gwActiveBuf;
                ptFAOCheck.stopped      = &gwStopped;
                ptFAOCheck.CurrentCount = &gulRetrieved;
                ptFAOCheck.overrun      = &gwOverrun;
                ptFAOCheck.HalfReady    = &gwHalfReady;

                if ((ErrCde = DRV_FAOCheck(DriverHandle,
                    (LPT_FAOCheck)&ptFAOCheck)) != 0)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
                    GlobalUnlock(hBuf); GlobalFree(hBuf);
                    GlobalUnlock(hVoltageBuf); GlobalFree(hVoltageBuf);
                    GlobalUnlock(hBinaryBuf); GlobalFree(hBinaryBuf);
                    DRV_FreeDMABuffer(DriverHandle, (LPARAM)&pBuffer);
                    DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }
                else
                {   // Display the status of the A/D conversion
                    sprintf(szDest, "Cyclic Counts: %d\n", gwRepeatCount);
                    if (gwHalfReady == 0)

⌨️ 快捷键说明

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