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

📄 wdadmabm.c

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 C
📖 第 1 页 / 共 4 页
字号:
				ptFAOWaveFormStart.Buffer     = (USHORT far *)lpCommonBuf;  // analog output data
				ptFAOWaveFormStart.EnabledChannel     = usEnabledChannel;   // which channel

                if ((ErrCde = DRV_FAOWaveFormStart(DriverHandle,
                    (LPT_FAOWaveFormStart)&ptFAOWaveFormStart)) != 0)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
					MyFreeBuffer();
					GlobalUnlock(hCommonBuf);
					GlobalFree(hCommonBuf);
					DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 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);
                EnableWindow(hwndStopButton, TRUE);	// Enable stop buttons


                // if event feature is enabled 
                // Set the new timer type and start it up.
                //
                if (gwEvtFlag)
                {			
					if (SetTimer(hWnd, 1, 50, NULL) == (UINT)NULL)
					{
						MessageBox(hWnd, "Couldn't create timer.", "Notice!", MB_OK);
						DRV_DeviceClose((LONG far *)&DriverHandle);
						return 0;
					}
					bRunTimer = TRUE;
				}
                return 0;

                //end IDM_RUN


           case IDC_STOPBUTTON:  			   

                EnableWindow(hwndStopButton, FALSE);    // Disable stop buttons
				CloseHandle(hThreadHandle);				// close thread handle
                EnableMenuItem(hMenu, IDM_RUN, MF_ENABLED | MF_BYCOMMAND);


                //  Stop D/A conversion 
                if ((ErrCde = DRV_FAOTerminate(DriverHandle)) != 0)
                {
                    DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                    MessageBox(hWnd,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
					MyFreeBuffer();
					GlobalUnlock(hCommonBuf);
					GlobalFree(hCommonBuf);
					DRV_DeviceClose((LONG far *)&DriverHandle);
                    return 0;
                }               

                return 0;
          }


    case WM_TIMER:

	    // Get transfer status
		ptFAOCheck.ActiveBuf    = &gwActiveBuf;
		ptFAOCheck.stopped      = &gwStopped;
		ptFAOCheck.CurrentCount = &ulCurrentCount;
		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);
			MyFreeBuffer();
			GlobalUnlock(hCommonBuf);
			GlobalFree(hCommonBuf);
			DRV_DeviceClose((LONG far *)&DriverHandle);
			return 0;
		}

        // Display Data
        hdc = GetDC(hWnd);
        GetClientRect(hWnd, &rect);
        InvalidateRect(hWnd, NULL, TRUE);
        UpdateWindow(hWnd);
        wsprintf((LPSTR)szBuffer, "Wave Count  = %ld", ulCurrentCount);
        DrawText(hdc, szBuffer, -1, &rect,
               DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        ReleaseDC(hWnd, hdc);


		// if thread finish run stop instruction 
		if(!bThreadloop)
		{			
			// if A/O process finish --> call DRV_FAOStop
			if(gwStopped) 
			{  
                // Step 1: Free buffer
				MyFreeBuffer();
				GlobalUnlock(hCommonBuf);
				GlobalFree(hCommonBuf);
				
				// Step 3: Close driver
				DRV_DeviceClose((LONG far *)&DriverHandle);				
				
				// Stop Timer
				if(bRunTimer)  
				{
					KillTimer(hWnd, 1);
					bRunTimer = FALSE;  
				}
				EnableMenuItem(hMenu, IDM_RUN, MF_ENABLED | MF_BYCOMMAND);
				if ( hThreadHandle )
				{
					CloseHandle(hThreadHandle);
				}				
			}
			
		}

        return 0;


    case WM_DESTROY:

        PostQuitMessage(0);
        break;

    default:
         return (DefWindowProc(hWnd, message, wParam, lParam));
      }

   return ((LONG)NULL);
}

void SetRealBuffer(float far *lpBuf, long num, LPSWAVE lpWave)
{
    int i, r, half;
    float slope;

    switch (lpWave->wWaveform)
    {
        case WAVE_SINE:
             for (i = 0; i < num; i ++)
			 {
               *(lpBuf+i) = (float)(lpWave->fMagnitude
                            * sin(6.2831853*(double)i/(double)lpWave->wPeriod)
                            + lpWave->fOffset);
			 }
             break;
        case WAVE_TRIANGLE:
             slope = lpWave->fMagnitude * 4.0f / (float)lpWave->wPeriod;

             for (i = 0; i < num; i ++)
                {
                    r = i % lpWave->wPeriod;
                    half = (int)((float)lpWave->wPeriod / 2.0f);
                    if (r <= half)
                       *(lpBuf + i) = slope * r - lpWave->fMagnitude
                                      + lpWave->fOffset;
                    else
                       *(lpBuf + i) = slope * (lpWave->wPeriod - r)
                                      - lpWave->fMagnitude + lpWave->fOffset;
                }

             break;
        case WAVE_SQUARE:
             for (i = 0; i < num; i ++)
                {
                    r = i % lpWave->wPeriod;
                    half = (int)((float)lpWave->wPeriod / 2.0f);
                    if (r <= half)
                       *(lpBuf + i) = lpWave->fOffset - lpWave->fMagnitude;
                    else
                       *(lpBuf + i) = lpWave->fOffset + lpWave->fMagnitude;
                }
             break;
        case SINE_TRIANGLE:
			{
			 ULONG x;
			 double y;

             slope = lpWave->fMagnitude * 4.0f / (float)lpWave->wPeriod;
             for (i = 0; i < num; i ++)
			 {
				if(i%2)
				{
					modf(((double)i/2.0),(double *)&y);
					y = y + 1;
                    r = ((USHORT)y) % lpWave->wPeriod;
                    half = (int)((float)lpWave->wPeriod / 2.0f);
                    if (r <= half)
                       *(lpBuf + i) = slope * r - lpWave->fMagnitude
                                      + lpWave->fOffset;
                    else
                       *(lpBuf + i) = slope * (lpWave->wPeriod - r)
                                      - lpWave->fMagnitude + lpWave->fOffset;
				}
				else
				{
					x = i/2;
					*(lpBuf+i) = (float)(lpWave->fMagnitude
                            * sin(6.28318*(double)x/(double)lpWave->wPeriod)
                            + lpWave->fOffset);
				}
			 }
             break;
			}
    }

}

void SetMultiToOneBuffer(USHORT usEnabledChannel, int count)
{
	int i,TotalCount;

	TotalCount=0;

    for (i = 0; i < count; i ++)
	{
		if(usEnabledChannel & ADV_CHANNEL0)
		{
			((USHORT far *)lpCommonBuf)[TotalCount] = 
				((USHORT)((USHORT far *) lpBuf[0])[i]) & 0x0fff;
			TotalCount++;
		}
		if(usEnabledChannel & ADV_CHANNEL1)
		{
			((USHORT far *)lpCommonBuf)[TotalCount] = 
				((USHORT)((USHORT far *) lpBuf[1])[i] | ( 0x01 << 12)) & 0x3fff;
			TotalCount++;
		}
		if(usEnabledChannel & ADV_CHANNEL2)
		{
			((USHORT far *)lpCommonBuf)[TotalCount] = 
				((USHORT)((USHORT far *) lpBuf[2])[i] | ( 0x02 << 12)) & 0x3fff;
			TotalCount++;
		}
		if(usEnabledChannel & ADV_CHANNEL3)
		{
			((USHORT far *)lpCommonBuf)[TotalCount] = 
				((USHORT)((USHORT far *) lpBuf[3])[i] | ( 0x03 << 12)) & 0x3fff;
			TotalCount++;
		}
	}

}


//------------------------------------------------------------------
void UserThread()
{
    USHORT usEventType;
    

    while(bThreadloop)
    {
        // Check message
        ptCheckEvent.EventType = &usEventType;
        ptCheckEvent.Milliseconds = 100;

		bThreadflag = TRUE;
        DRV_CheckEvent(DriverHandle, (LPT_CheckEvent)&ptCheckEvent);

        // Process interrupt event
        if (usEventType & ADS_EVT_INTERRUPT)
            adInterruptEvent();

        // Process buffer change event
        if (usEventType & ADS_EVT_BUFCHANGE)
            adBufChangeEvent();

        // Process overrun event
        if (usEventType & ADS_EVT_OVERRUN)
            adOverrunEvent();

        // Process terminate event
        if (usEventType == ADS_EVT_TERMINATED)
		{
            adTerminateEvent();
	    	bThreadflag = FALSE;
			hThreadHandle = NULL;
		}
    }

}

//------------------------------------------------------------------
void adInterruptEvent()
{
    return;
}

//------------------------------------------------------------------
void adBufChangeEvent()
{
    return;
}
//------------------------------------------------------------------
void adOverrunEvent()
{

    return;
}
//------------------------------------------------------------------
void adTerminateEvent()
{

    bThreadloop = FALSE;
    // Disable stop and status buttons
    EnableWindow(hwndStopButton, FALSE);
    EnableWindow(hwndStatusButton, FALSE);

    return;
}


void MyFreeBuffer()
{
	int i;

	for(i=0; i<4; i++)
	{

		if(hBuf[i]) 
		{
			GlobalFree(hBuf[i]);
			hBuf[i] = NULL;
		}
		if(hVoltageBuf[i])
		{
			GlobalFree(hVoltageBuf[i]);
			hVoltageBuf[i] = NULL;
		}
	}

}

⌨️ 快捷键说明

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