求救!!关于TComPort的事件触发问题.(200分)(200分)

  • 主题发起人 主题发起人 cancheung
  • 开始时间 开始时间
C

cancheung

Unregistered / Unconfirmed
GUEST, unregistred user!
问题:
如果使用TComPort的OnRxchar事件,没有丢包的现象.(发送多少帧,接收多少帧),而使用TComPort的WaitForEvent函数,等待串口事件,会出现不稳定现象,发生丢包.

(为TComPort中附带的例子,源码如下)

program ModTest;
{$APPTYPE CONSOLE}

uses
SysUtils,
CPort,
Windows,
SyncObjs;

var
ComPort: TComPort;
Events: TComEvents;
Answer, Data: string;
Step: Integer;
Event: TEvent;


function CtrlHandler(CtrlType: LongWord): Boolean;
begin
Event.SetEvent;
Result := True;
end;

begin
Event := TEvent.Create(nil, True, False, '');
SetConsoleCtrlHandler(@CtrlHandler, True);
try
ComPort := TComPort.Create(nil);
try
if ParamCount > 0 then
ComPort.Port := ParamStr(1)
else
ComPort.Port := 'COM1';
ComPort.Events := [];
ComPort.FlowControl.ControlDTR := dtrEnable;
ComPort.FlowControl.ControlRTS := rtsEnable;
ComPort.Open; // open port
ComPort.WriteStr('AT'#13#10); // send test command
Answer := '';
Step := 0;
repeat
Events := [evRxChar];
ComPort.WaitForEvent(Events, Event.Handle, 5000); // wait for charachters
if evRxChar in Events then
begin
ComPort.ReadStr(Data, ComPort.InputCount);
Answer := Answer + Data;
if Pos('OK', Answer) > 0 then
Break;
end;
Inc(Step)
until [red](Events = [])[/red] or (Step = 20);//[red]<----此处有时为为何收不到evRxChar 事件??????[/red]
if Pos('OK', Answer) > 0 then
WriteLn('Modem found on ' + ComPort.Port)
else
WriteLn('Modem NOT found on ' + ComPort.Port);
finally
ComPort.Free;
end;
except
on E: Exception do
WriteLn('Error: ' + E.Message);
end;
Event.Free;
end.
 
是异步方式的事件不可靠还是什么原因,导致串口事件触发不稳定呢?
 
过段时间俺也要学串口,现在还看不懂,学习下
 
用多线程加消息
 
低层的问题还是得用API解决....解决了..

谢谢二位的参与.
 
后退
顶部