1

我有一个 SMS 网关,我使用 AT 命令控制该组件工作正常,但我无法正确解析从设备接收到的字符串。

首先我使用硬件流控制这对我有什么影响?

我使用 TComDataPacket 组件,女巫有一个事件 onPacket(),如果我使用调试器,我会收到一个字符串,但如果我使用 pos() 搜索或 ShowMessage() 显示字符串,则字符串非常短我没有看到任何#00

我尝试使用 RxChar 事件,但没有被触发,并且 RxBuf 给出了访问冲突。

我使用 Delphi XE 2 组件有什么问题还是我做错了什么?

LE:如果我不在方法末尾插入睡眠,事件 onPacket() 将冻结这是我的代码:

procedure TfrmMain.comPacketPacket(Sender: TObject; const Str: string);
var
  S,numar,coord : string;
  i,poi,rr,j,tmp2 : Integer;
const
  Readul = 'READ|';
  delim  = 'gps=';
  CMG = 'CMGL';
begin
  if pos('CMTI',Str) > 0 then
                          SendATCommand('AT+CMGL="ALL"');
  for i := 1 to Length(Str) do
    if Str[i] in ['A'..'Z','a'..'z','0'..'9','=','.'] then S := S + Str[i]
    else if Str[i] = '"' then S := S + '|';
    repeat
       poi := Pos('gps=',S);
       if poi > 0 then
       begin
          j := poi;
          repeat
            Dec(j);
            rr := PosEx(Readul,S,j);
            if rr >= poi then  rr := 0;
          until (rr>0) or (j = 1);
          numar := Copy(S,rr+Length(Readul)+1,PosEx('|',S,(rr+Length(Readul)+1)) - (rr + Length(Readul) + 1));
          ShowMessage('Trimit la numarul ' + numar);
          coord := Copy(S,poi + Length(delim),PosEx(CMG,S,poi) - poi - Length(CMG));
          S := Copy(S,poi + Length(delim),Length(S));
       end;
    until poi = 0;
    Sleep(1500);
end;
4

1 回答 1

1

这里的问题是字符串。在 XE2 中,字符串是 unicode 字符串。在 Delphi 2007 及更低版本中,字符串是 ansi 字符串。它们的大小不同。您必须通过检查 TComport 源来确定。

于 2012-05-12T00:35:50.763 回答