Subject: 有关TCnModem的两个疑惑 [Print This Page]
Author:
szyourname Time: 2010-8-12 17:29 Subject: 有关TCnModem的两个疑惑
有关TCnModem组件,我有两个疑惑,请大家帮忙指点一下:
当调用SendATOk()函数发送AT指令时,
1、从串口中接收到成功应答数据,如果"OK"不是在一次ReceiveData中到达,而是先来"O", 第二次ReceiveData才收到"K",那SendATOK岂不是要返回失败?
2、是否会存在由于接收的数据中本身包含包括"OK"(如短信内容中包括了OK字符),而响应SendATOK对应答数据是否成功的准确性判断呢?
谢谢!
附CnModem单元的相关代码:
// 等待一条AT命令执行结果
function TCnModem.WaitATResult(Delay: Cardinal): string;
var
Tick: Cardinal;
begin
FWaitATResult := True;
try
FATResult := '';
Tick := GetTickCount;
while (GetTickCount - Tick < Delay) and (FATResult = '') do
Application.HandleMessage;
Result := FATResult;
FATResult := '';
finally
FWaitATResult := False;
end;
end;
// 发送一条AT命令,返回是否成功
function TCnModem.SendATOk(AT: string; Delay: Cardinal): Boolean;
var
i, j: Integer;
s: string;
begin
Result := False;
for i := 0 to 2 do
begin
WriteATCommand(AT);
for j := 0 to 2 do
begin
s := Trim(UpperCase(WaitATResult(Delay)));
if Pos('OK', s) > 0 then
begin
Result := True;
Exit;
end
else if Pos('ERROR', s) > 0 then
begin
InvalidCommand(AT);
Exit;
end;
end;
end;
end;
// 接收到数据
procedure TCnModem.ReceiveData(Buffer: PAnsiChar; BufferLength: WORD);
var
s: AnsiString;
begin
if FWaitATResult then // 正在等待AT命令执行结果
begin
FATResult := {$IFDEF DELPHI12_UP}String{$ENDIF}(Buffer);
Exit;
end;
s := Buffer;
s := {$IFDEF DELPHI12_UP}AnsiString{$ENDIF}(Trim(UpperCase({$IFDEF DELPHI12_UP}String{$ENDIF}(s))));
if (ModemState in [msOffline, msOnlineCommand, msConnecting]) and (s = 'RING') then
Ring // 振铃信号
else if (ModemState = msOnline) and (s = 'NO CARRIER') then
DisConnect // 载波丢失
else
inherited;
end;
[ 本帖最后由 szyourname 于 2010-8-12 17:32 编辑 ]
Author:
solokey Time: 2010-8-15 21:25
CnModem有个超时设置,把读间隔超时的时间设置的稍微长一点..比如30-50ms,估计就没这个问题了.试试吧.
Welcome to CnPack Forum (http://bbs.cnpack.org/) |
Powered by Discuz! 5.0.0 |