分类:
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)
{
int nBytes = 0;
char Buffer[1000], *EndPtr;
AnsiString t,s = Edit1->Text.Trim();
while(s.Length()>0)
{
int p = s.Pos('
'); //空格
if(p>0)
{
t
= s.SubString(1,p-1);
s
= s.SubString(p+1,s.Length()).Trim();
Buffer[nBytes++]
= strtol(t.c_str(), &EndPtr, 16); //十六进制字符串转成字节
}
else //还剩下最后一个字节
{
t
= s;
s
= "";
Buffer[nBytes++]
= strtol(t.c_str(), &EndPtr, 16); //十六进制字符串转成字节
}
}
YbCommDevice1->Write(Buffer,nBytes);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
unsigned char Buf[8192]; //收到的字节数不会超过串口缓存的容量,
所以分配一个缓存容量相同的Buf
int n = YbCommDevice1->Read(Buf,8192);
//收到 n 个字节
AnsiString s;
for(int i=0; i
s = s.Trim();
if(!s.IsEmpty())
Memo1->Lines->Add(s);
}
//---------------------------------------------------------------------------