void __fastcall TForm1::ButtonSendClick(TObject *Sender)
{
const BufSize = 4096;
char Buffer[BufSize];
int nBytes = 0;
char *EndPtr=0; //指示数据转换错误 if(EndPtr){if(*EndPtr){ 转换失败 }}
AnsiString t,s = Edit1->Text.Trim();
while((s.Length()>0) && (nBytes {
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->WritePackage(Buffer,nBytes); //发送数据包
Sleep(200);
//接受数据包,执行的时候收不到数据,一定要在Timer事件里吗?
while((nBytes=YbCommDevice1->ReadPackage(Buffer,BufSize))>0)
{
AnsiString s;
for(int i=0; i s += IntToHex(Buffer[i],2) + " ";
s = s.Trim();
if(!s.IsEmpty())
Memo1->Lines->Add(s);
}
--------------------next---------------------
阅读(1076) | 评论(0) | 转发(0) |