发表于:2004-09-24 15:57:00
楼主
用的是研华的PCL1800,我要实现的功能是AD通道接收DA通道发送的数据,AD通道和DA通道用一根导线连接起来,发送的是正弦,接收的应该和发送的一样吧
可是我发现接收的数据的幅值是发送的一半,当我发送的是个常值时,接收的数据几乎为零,这是为什么呀,请大家帮我看看程序到底哪出问题了,多谢了
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "driver.h"
#include "stdio.h"
#include "math.h"
#include "mmsystem.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
LONG DriverHandle; // driver handle
static PT_AOVoltageOut ptAOVoltageOut; // AOVoltageOut table
static PT_AIVoltageIn ptAIVoltageIn;
static PT_AIConfig ptAIConfig;
LRESULT ErrCde; // return error code
int DeviceNum;
int DAChannel,ADChannel;
static int step = 0;
float backdata;
FILE *DAfd,*ADfd;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TimerID = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditDeviceChange(TObject *Sender)
{
DeviceNum = StrToInt(EditDevice->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditDAChannelChange(TObject *Sender)
{
DAChannel = StrToInt(EditDAChannel->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditADChannelChange(TObject *Sender)
{
ADChannel = StrToInt(EditADChannel->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
DAfd = fopen("DA.txt", "wt");
if(DAfd == NULL)
MessageBox(NULL, "创建文件出错!", "出错信息", MB_OK);
ADfd = fopen("AD.txt", "wt");
if(ADfd == NULL)
MessageBox(NULL, "创建文件出错!", "出错信息", MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButStartClick(TObject *Sender)
{
ErrCde = DRV_DeviceOpen(DeviceNum,(LONG far *)&DriverHandle);
if (ErrCde != SUCCESS)
ErrorHandler(ErrCde);
TimerID = timeSetEvent(10, 0, (LPTIMECALLBACK)TimeProc, 0, TIME_PERIODIC);
if(TimerID ==0)
ShowMessage("创建失败");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButStopClick(TObject *Sender)
{
DRV_DeviceClose((LONG far *)&DriverHandle);
if(TimerID !=0)
timeKillEvent(TimerID);
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ErrorHandler(LRESULT dwErrCde)
{
//TODO: Add your source code here
char szErrMsg[80];
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
MessageBox(NULL,(LPCSTR)szErrMsg,"Driver Message",MB_OK);
}
void CALLBACK TForm1::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
float DAdata;
DAdata = (float)(2.0 * sin(6.28318*(double)step/(double)100) + 2.0);
ptAOVoltageOut.chan = DAChannel;
ptAOVoltageOut.OutputValue = DAdata;
fprintf(DAfd, "%f\n", ptAOVoltageOut.OutputValue);
ErrCde = DRV_AOVoltageOut(DriverHandle,(LPT_AOVoltageOut)&ptAOVoltageOut);
if(ErrCde != 0)
Form1->ErrorHandler(ErrCde);
ptAIConfig.DasGain = 0;
ptAIConfig.DasChan = ADChannel;
ErrCde = DRV_AIConfig(DriverHandle,(LPT_AIConfig)&ptAIConfig);
if (ErrCde != SUCCESS)
Form1->ErrorHandler(ErrCde);
ptAIVoltageIn.chan = ADChannel;
ptAIVoltageIn.gain = 0;
ptAIVoltageIn.TrigMode = 0;
ptAIVoltageIn.voltage = (FLOAT far *)&backdata;
ErrCde = DRV_AIVoltageIn (DriverHandle,(LPT_AIVoltageIn)&ptAIVoltageIn);
if(ErrCde != 0)
Form1->ErrorHandler(ErrCde);
fprintf(ADfd, "%f\n", backdata);
step = step +10;
}