之前我做一個量測A/D信號藉8951經由RS232傳送至PC,
附上BCB程式,以供您參考.
//-------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//-------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE handle;
DCB dcb;
int a;
//----------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Form1->Chart1->Series[0]->AddXY(0,1,"",clYellow); COMMTIMEOUTS TimeOut;
TimeOut.ReadIntervalTimeout = 0;
TimeOut.ReadTotalTimeoutMultiplier = 0;
TimeOut.ReadTotalTimeoutConstant = 500;
//(總讀取 time out 時間 = 0.5 秒)
TimeOut.WriteTotalTimeoutMultiplier = 0;
TimeOut.WriteTotalTimeoutConstant = 500;
//(總寫入 time out 時間 = 0.5 秒) if(BuildCommDCB("19200,n,8,1",&dcb))
{ lbBuildCommDCB->Caption="BuildCommDCB 成功"; }
else
{ lbBuildCommDCB->Caption="BuildCommDCB 失敗"; }
handle=CreateFile("Com1",GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); if(SetCommState(handle,&dcb))
{ lbSetCommState->Caption="SetCommState 成功"; }
else
{ lbSetCommState->Caption="SetCommState 失敗"; } if(SetupComm(handle,1024,1024))
{ lbSetupComm->Caption="SetupComm 成功"; }
else
{ lbSetupComm->Caption="SetupComm 失敗"; } if(SetCommTimeouts(handle,&TimeOut))
{ lbSetCommTimeouts->Caption="SetCommTimeouts 成功"; }
else
{ lbSetCommTimeouts->Caption="SetCommTimeouts 失敗"; }
}
//-------------------------------------------------------------- void __fastcall TForm1::btnReadClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
int ReceiveValue,TransferValue=0;
double Voltage;
DWORD dwWrite,dwRead; WriteFile(handle,&TransferValue,1,&dwWrite,0);
ReadFile(handle, &ReceiveValue, 1, &dwRead, NULL);
Voltage=(double)ReceiveValue*5.0/255.0;
Form1->Chart1->Series[0]->AddXY(a,Voltage,"",clYellow);
a ; }
//-------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender)
{
a=0;
Timer1->Enabled=true;
}
//---------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender)
{
a=0;
Timer1->Enabled=false;
Form1->Chart1->Series[0]->Clear();
}
//-----------------------------------------------