分类:
2008-12-17 18:00:15
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
try
{
YbCommDevice1->PortNo
= 1; //COM1
YbCommDevice1->Active
= true;
}
catch(Exception &e)
{
ShowMessage("YbCommDevice1:
"+e.Message);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSet1Click(TObject *Sender)
{
YbCommDevice1->SettingsDialog(this,true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSend1Click(TObject *Sender)
{
YbCommDevice1->Write(Memo1->Text.c_str(),
Memo1->Text.Length());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
//收到的字节数不会超过串口缓存的容量, 所以分配一个缓存容量相同的Buf
char Buf[8192+1]; //多一个字节用来保存文本(字符串)的结束符
int n = YbCommDevice1->Read(Buf,8192);
if(n>0) //收到字节数
{
Buf[n] = 0;
//添加一个文本结束符 '\0'
Memo2->Text
= Memo2->Text + Buf;
}
}
//---------------------------------------------------------------------------