从串口取数,在receiveData事件中写的代码能正确执行,并且用了try...except,可是在程序运行中还是总弹出一个对话筐说:'' is not a valid float point value。又不影响程序运行,又不能不管。程序中设断也拦不住,不知该怎么办?不知有没有人遇到过?是否程序的bug?
procedure TDispFrm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); var S,tmp,s1:String; begin try SetLength(S, BufferLength); //接收RS232的数据并显示Memo1上。 Move(Buffer^, PChar(S)^, BufferLength); if comm2.CommName<>'COM0' then comm2.WriteCommData(PChar(Buffer),BufferLength); if pos('=',S)=0 then tmp:='0000000' else begin tmp:=copy(s,pos('=',S)+1,7); if pos('=',tmp)<>0 then exit; end; s1:=ReverseString(tmp); //showmessage(s1); except s1:='0000000'; end; iAd.Value:=StrToFloatDef(s1,0)/1000; end; //这就是相关代码
COM通讯,发送和接收到的数据都是有协议的..数据段都是规定好的.. 你用 function ComStrToDouble(const st:string;var num:doubel):boolean; var i: integer; begin val(st,num,i); if (i=0) then result:=true else result:=false; end; 这样的方法可以知道接收到的数据是否正常.如果返回false..就证明通讯出了问题. 另外,具体的数据要根据协议来看了..我见过很多BT的协议...
可能里面又调用了其它函数,
如没有,用 StrToFloatDef 这个函数
BufferLength: Word);
var
S,tmp,s1:String;
begin
try
SetLength(S, BufferLength); //接收RS232的数据并显示Memo1上。
Move(Buffer^, PChar(S)^, BufferLength);
if comm2.CommName<>'COM0' then
comm2.WriteCommData(PChar(Buffer),BufferLength);
if pos('=',S)=0 then
tmp:='0000000'
else
begin
tmp:=copy(s,pos('=',S)+1,7);
if pos('=',tmp)<>0 then
exit;
end;
s1:=ReverseString(tmp);
//showmessage(s1);
except
s1:='0000000';
end;
iAd.Value:=StrToFloatDef(s1,0)/1000;
end;
//这就是相关代码
你用
function ComStrToDouble(const st:string;var num:doubel):boolean;
var i: integer;
begin
val(st,num,i);
if (i=0) then result:=true
else result:=false;
end;
这样的方法可以知道接收到的数据是否正常.如果返回false..就证明通讯出了问题.
另外,具体的数据要根据协议来看了..我见过很多BT的协议...