📄 faithread.c
字号:
// #########################################################################
// *************************************************************************
// #########################################################################
// *************************************************************************
#include <cvirte.h>
#include <userint.h>
#include "DriverController.h"
#include "ADCards.h"
#include "..\include\Global.h"
#include "MyFile.h"
//------------------------------------------------------------------------------------------------
extern int plMain;
extern PT_SystemData g_SystemData;
extern PT_DAQCard g_PCL818;
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------
// Function : AIScale()
// PURPOSE : Converts the binary result from an AIBinaryIn call
// to the actual input voltage.
// Parameters : reading(IN)
// MaxVolt(IN)
// MaxCount(IN)
// offset(IN)
// voltage(OUT)
// Return : TRUE (success), FALSE (failure)
//
// Call/Called procedure cross reference :
// Call Called Explanation
//------------------------------------------------------------------
float __stdcall DAQFAIScale(USHORT reading, FLOAT MaxVolt, USHORT MaxCount, USHORT offset)
{
float voltage;
if (MaxCount == offset)
return 0.0;
voltage = (float)(MaxVolt * ((float)reading - (float)offset)) / (MaxCount - offset);
return voltage;
}
//------------------------------------------------------------------------------------------------
USHORT __stdcall DAQFAIReading(ULONG ulRetrieved, USHORT ushChannel, ULONG ulTimes)
{
ULONG usReading = 0;
static USHORT* psDataBuffer;
ULONG ulOffset;
ULONG i;
long usData;
psDataBuffer = (USHORT*)g_SystemData.AcquisitionBuffer;
ulOffset = ulRetrieved;
for (i = 0; i < ulTimes; i ++)
{
// Get Offset
if (ulOffset > 0)
ulOffset -= FAI_ChannelNumber;
// Rewind to tail of data buffer
else
ulOffset = g_SystemData.ulBufferSize - FAI_ChannelNumber;
usData = (psDataBuffer [ulOffset + ushChannel] & 0xfff0);
usReading += usData;
}
return (usReading >> 4) / ulTimes;
}
//------------------------------------------------------------------------------------------------
int __stdcall DAQFAIReadAIData(void)
{
LRESULT ErrCde; // Return error code
float fValue;
static float fMaxValue = 5.0;
static USHORT usOffset = 2048;
USHORT usReading;
int i;
static USHORT usActiveBuf; // return by FAICheck
static USHORT usOverrun; // return by FAICheck, FAITransfer
static USHORT usStopped; // return by FAICheck
static ULONG ulRetrieved; // return by FAICheck
static USHORT usHalfReady; // return by FAICheck
static PT_FAICheck ptFAICheck;
static LPT_DAQData pData;
// Get DMA transfer status
ptFAICheck.ActiveBuf = &usActiveBuf;
ptFAICheck.stopped = &usStopped;
ptFAICheck.retrieved = &ulRetrieved;
ptFAICheck.overrun = &usOverrun;
ptFAICheck.HalfReady = &usHalfReady;
ErrCde = DRV_FAICheck (g_PCL818.hDevice, &ptFAICheck);
if (ErrCde != 0)
{
return FALSE;
}
ulRetrieved = ulRetrieved - ulRetrieved % FAI_ChannelNumber;
usReading = DAQFAIReading (ulRetrieved, AICH_TEMPERATURE, 10);
fValue = DAQFAIScale (usReading, fMaxValue, 4095, usOffset);
fValue = fValue * 1000.0 / 10.0 - 273.0;
g_SystemData.ptDAQData.fTemperature = fValue;
usReading = DAQFAIReading (ulRetrieved, AICH_RESISTER, 10);
fValue = DAQFAIScale (usReading, fMaxValue, 4095, usOffset);
fValue = (fValue - 1.0) * 1000.0 / 4.0;
if (fValue > 300.0)
fValue -= 300.0;
if (fValue < 0.0)
fValue = 0.0;
g_SystemData.ptDAQData.fResister = fValue;
usReading = DAQFAIReading (ulRetrieved, AICH_VOLTAGE, 10);
fValue = DAQFAIScale (usReading, fMaxValue, 4095, usOffset);
fValue = (fValue - 1.0) * 15.0 / 4.0;
g_SystemData.ptDAQData.fVoltage = fValue;
return TRUE;
}
//------------------------------------------------------------------------------------------------
int __stdcall DAQFAIReadResister(float* fData)
{
LRESULT ErrCde; // Return error code
float fValue;
static float fMaxValue = 5.0;
static USHORT usOffset = 2048;
USHORT usReading;
int i;
static USHORT usActiveBuf; // return by FAICheck
static USHORT usOverrun; // return by FAICheck, FAITransfer
static USHORT usStopped; // return by FAICheck
static ULONG ulRetrieved; // return by FAICheck
static USHORT usHalfReady; // return by FAICheck
static PT_FAICheck ptFAICheck;
static LPT_DAQData pData;
// Get DMA transfer status
ptFAICheck.ActiveBuf = &usActiveBuf;
ptFAICheck.stopped = &usStopped;
ptFAICheck.retrieved = &ulRetrieved;
ptFAICheck.overrun = &usOverrun;
ptFAICheck.HalfReady = &usHalfReady;
ErrCde = DRV_FAICheck (g_PCL818.hDevice, &ptFAICheck);
if (ErrCde != 0)
{
return FALSE;
}
ulRetrieved = ulRetrieved - ulRetrieved % FAI_ChannelNumber;
usReading = DAQFAIReading (ulRetrieved, AICH_RESISTER, 10);
fValue = DAQFAIScale (usReading, fMaxValue, 4095, usOffset);
fValue = (fValue - 1.0) * 1000.0 / 4.0;
if (fValue < 0.0)
fValue = 0.0;
*fData = fValue;
return TRUE;
}
//---------------------------------------------------------------------------
void __stdcall DAQFAIOnBufferChange(void)
{
char szData[32];
LRESULT ErrCde; // Return error code
char szErrMsg[80]; // Use for MESSAGEBOX function
USHORT usReading;
float fValue;
int i, j;
static float fMaxValue = 5.0;
static USHORT usOffset = 2048;
static USHORT usActiveBuf; // return by FAICheck
static USHORT usOverrun; // return by FAICheck, FAITransfer
static USHORT usStopped; // return by FAICheck
static ULONG ulRetrieved; // return by FAICheck
static USHORT usHalfReady; // return by FAICheck
static float* pfDataBuffer;
static ULONG ulOffset; //
static ULONG ulDataLen;
static PT_FAICheck ptFAICheck;
static ULONG ulSpace = 0;
// Get DMA transfer status
ptFAICheck.ActiveBuf = &usActiveBuf;
ptFAICheck.stopped = &usStopped;
ptFAICheck.retrieved = &ulRetrieved;
ptFAICheck.overrun = &usOverrun;
ptFAICheck.HalfReady = &usHalfReady;
ErrCde = DRV_FAICheck (g_PCL818.hDevice, &ptFAICheck);
if (ErrCde != 0)
{
DRV_FAIStop (g_PCL818.hDevice);
return;
}
ulDataLen = g_SystemData.ulBufferSize / 2;
if (2 == usHalfReady)
ulRetrieved = ulDataLen;
else
ulRetrieved = 0;
// Adjust Count
g_SystemData.ulBufferChanged ++;
if (g_SystemData.usTrendRecord == 0)
return;
if ((g_SystemData.ulSeconds * 2) >= FAI_DataBufferSize)
return;
// Get acquised force data
ulDataLen = g_SystemData.ulPacerRate / 2;
pfDataBuffer = (float*)g_SystemData.ufDataBuffer;
// Realtime trend
// memcpy (pfDataBuffer, pfDataBuffer + ulDataLen, sizeof (float) * ulDataLen);
pfDataBuffer += ulDataLen * g_SystemData.ulSeconds;
for (ulOffset = 0; ulOffset < ulDataLen; ulOffset ++)
{
usReading = DAQFAIReading (ulRetrieved + ulOffset * FAI_ChannelNumber, AICH_VOLTAGE, 10);
fValue = DAQFAIScale (usReading, fMaxValue, 4095, usOffset);
fValue = (fValue - 1.0) * 15.0 / 4.0;
pfDataBuffer [ulOffset] = fValue;
}
g_SystemData.ulSeconds ++;
return;
}
//---------------------------------------------------------------------------
void __stdcall DAQFAIOnOverrun(void)
{
g_SystemData.ulOverruned ++;
DRV_ClearOverrun (g_PCL818.hDevice);
}
//---------------------------------------------------------------------------
void __stdcall DAQFAIOnTerminate(void)
{
LRESULT ErrCde; // Return error code
// Close device
ErrCde = DRV_DeviceClose ((LONG far *)&g_PCL818.hDevice);
if (ErrCde != SUCCESS)
;
}
//------------------------------------------------------------------------------------------------
DWORD WINAPI DAQFAIRoutine(DWORD arg)
{
LRESULT ErrCde; // Return error code
USHORT usEventType;
char szData[32];
PT_CheckEvent ptCheckEvent;
while (g_SystemData.usThreadLoop)
{
ptCheckEvent.EventType = &usEventType;
// ptCheckEvent.Milliseconds = INFINITE;
ptCheckEvent.Milliseconds = 1000;
ErrCde = DRV_CheckEvent (g_PCL818.hDevice, &ptCheckEvent);
if (ErrCde != 0)
continue;
// Process buffer change event
if (usEventType & ADS_EVT_BUFCHANGE)
DAQFAIOnBufferChange ();
// Process overrun event
if (usEventType & ADS_EVT_OVERRUN)
DAQFAIOnOverrun ();
// Process terminate event
if (usEventType & ADS_EVT_TERMINATED)
{
DAQFAIOnTerminate ();
break;
}
}
return 0;
}
//------------------------------------------------------------------------------------------------
int __stdcall DAQFAIScanStart(LPT_DAQCard pDevice)
{
LRESULT ErrCde; // Return error code
char szErrMsg[256];
unsigned short ausGainCode[16];
int i;
PT_FAIIntScanStart ptFAIIntScanStart;
PT_EnableEvent ptEnableEvent;
long ulBufferSize;
if (pDevice->hDevice == 0L)
return -1;
// allocate memory
ulBufferSize = g_SystemData.ulBufferSize * sizeof (USHORT);
if ((g_SystemData.AcquisitionBuffer = (USHORT far *)GlobalAlloc (GPTR, ulBufferSize)) == 0)
{
lstrcpy ((char *)szErrMsg, "全局内存不足,不能够分配内存!");
MessageBox (NULL, (char *)szErrMsg, "错误", MB_OK);
return -1;
}
// Generate GainList
for (i = 0; i < 16; i ++)
ausGainCode [i] = pDevice->usGainCode;
// g_FAITransfer.DataBuffer = (FLOAT far *)g_DisplayBuffer;
// enable event
ptEnableEvent.EventType = ADS_EVT_BUFCHANGE | ADS_EVT_TERMINATED | ADS_EVT_OVERRUN;
ptEnableEvent.Enabled = TRUE;
ptEnableEvent.Count = 2048;
ErrCde = DRV_EnableEvent (pDevice->hDevice, &ptEnableEvent);
if (ErrCde != SUCCESS)
{
DRV_DeviceClose ((LONG far *)&pDevice->hDevice);
pDevice->hDevice = 0L;
return -1;
}
ptFAIIntScanStart.TrigSrc = 0; // Internal Trigger
ptFAIIntScanStart.cyclic = 1;
ptFAIIntScanStart.StartChan = g_SystemData.usStart;
ptFAIIntScanStart.NumChans = g_SystemData.usNumber;
ptFAIIntScanStart.SampleRate = g_SystemData.ulBufferSize;
ptFAIIntScanStart.GainList = &ausGainCode [0];
ptFAIIntScanStart.count = g_SystemData.ulBufferSize;
ptFAIIntScanStart.IntrCount = 1;
ptFAIIntScanStart.buffer = (USHORT *)g_SystemData.AcquisitionBuffer;
ErrCde = DRV_FAIIntScanStart (pDevice->hDevice, &ptFAIIntScanStart);
if (ErrCde != SUCCESS)
{
DRV_DeviceClose ((LONG far *)&pDevice->hDevice);
pDevice->hDevice = 0L;
return -1;
}
g_SystemData.ulBufferChanged = 0;
g_SystemData.ulOverruned = 0;
// Create Event check Thread;
g_SystemData.usThreadLoop = 1;
g_SystemData.uhFAIThread = CreateThread (NULL, 0,
(LPTHREAD_START_ROUTINE)&DAQFAIRoutine, 0, 0, NULL);
if (g_SystemData.uhFAIThread == NULL)
return -1;
return 0;
}
//------------------------------------------------------------------------------------------------
int __stdcall DAQFAIScanStop(LPT_DAQCard pDevice)
{
int ErrCde;
char szErrMsg[256];
g_SystemData.usThreadLoop = FALSE;
if (pDevice->hDevice != 0L)
{
ErrCde = DRV_FAITerminate (pDevice->hDevice);
if (ErrCde != SUCCESS)
{
DRV_GetErrorMessage (ErrCde, (LPSTR)szErrMsg);
MessageBox (NULL, (LPCSTR)szErrMsg, "错误信息", MB_OK);
}
// DRV_DeviceClose (&g_ADPCL812.hDevice);
}
// Terminate the thread
WaitForSingleObject (g_SystemData.uhFAIThread, 2000);
CloseHandle (g_SystemData.uhFAIThread);
g_SystemData.uhFAIThread = NULL;
if (g_SystemData.AcquisitionBuffer != NULL)
{
GlobalFree (g_SystemData.AcquisitionBuffer);
g_SystemData.AcquisitionBuffer = NULL;
}
return 0;
}
//------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -