●●●●●●●请用过ComPort的串口高手帮忙!!!●●●●●●●●(100分)

  • 主题发起人 主题发起人 inprises
  • 开始时间 开始时间
I

inprises

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用TComport控件从串品读取数据,我设置TimeOuts的ReadInterval属性,但是不起任何作用,只要收到数据就立即返回。我用类似的控件Spcomm就(它类似的属性为ReadIntervalTimeout)就好使,我想Comport这么有名的控件不会出这样的毛病吧,是不是使用上有什么说道,请用过Comport的串口高手指教,小弟在这里谢过了!
 
你要把接收到数据定义成数组,
 
看例子啊。这个控件带例子的呀。嘿嘿
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 (Events = []) or (Step = 20);
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.
 
谢谢,我知道怎样读数据,我是想知道它的TimeOuts的ReadInterval属性为什么会无效!
 
怎样才能让TimeOuts的ReadInterval属性起作用???
 
do you know what is ReadInterval?
 
不是ComPort不毛病,看源码:
procedure TCustomComPort.ApplyTimeouts;
var
Timeouts: TCommTimeouts;

function GetTOValue(const Value: Integer): DWORD;
begin
if Value = -1 then
Result := MAXDWORD
else
Result := Value;
end;

begin
// if not connected or inside BeginUpdate/EndUpdate block, do nothing
if (FConnected) and (FUpdateCount = 0) and
(not ((csDesigning in ComponentState) or (csLoading in ComponentState))) then
begin
Timeouts.ReadIntervalTimeout := GetTOValue(FTimeouts.ReadInterval);
Timeouts.ReadTotalTimeoutMultiplier := GetTOValue(FTimeouts.ReadTotalMultiplier);
Timeouts.ReadTotalTimeoutConstant := GetTOValue(FTimeouts.ReadTotalConstant);
Timeouts.WriteTotalTimeoutMultiplier := GetTOValue(FTimeouts.WriteTotalMultiplier);
Timeouts.WriteTotalTimeoutConstant := GetTOValue(FTimeouts.WriteTotalConstant);

// apply settings
if not SetCommTimeouts(FHandle, Timeouts) then
raise EComPort.Create(CError_TimeoutsFailed, GetLastError);
end;
end;

Timeouts的ReadInterval实际上去设置硬件的ReadIntervalTimeout.

Spcomm有的是定时查询的方式读COM口;
而ComPort用Overlapped机制进行COM读写,只要COM有数据接收到,ComPort就能够从Event响应,并通知应用程序接收,所以,某种ComPort的实时性更好。
但是,Overlapped的Event被触发,并不是COM接入一段完整的信息才触发,是Windows自己决定的,我在多个计算机和不同的Windows版本上试过,其触发后接收到的字符数并不一样。因此,需要自己建一个缓冲区来处理数据。

看下面代码中的中文标注:
procedure TCustomComPort.CreateHandle;
begin
FHandle := CreateFile(
PChar('//./' + FPort),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, // 以Overlapped方式打开COM口
0);

if FHandle = INVALID_HANDLE_VALUE then
raise EComPort.Create(CError_OpenFailed, GetLastError);
end;

procedure TComThread.Execute;
var
EventHandles: array[0..1] of THandle;
Overlapped: TOverlapped;
Signaled, BytesTrans, Mask: DWORD;
begin
FillChar(Overlapped, SizeOf(Overlapped), 0);
Overlapped.hEvent := CreateEvent(nil, True, True, nil);
EventHandles[0] := FStopEvent;
EventHandles[1] := Overlapped.hEvent; // COM口上的Overlapped事件
repeat
// 等待COM上的事件
// wait for event to occur on serial port
WaitCommEvent(FComPort.Handle, Mask, @Overlapped);
Signaled := WaitForMultipleObjects(2, @EventHandles, False, INFINITE);
// if event occurs, dispatch it
if (Signaled = WAIT_OBJECT_0 + 1)
and GetOverlappedResult(FComPort.Handle, Overlapped, BytesTrans, False)
then
begin
// 通知应用程序接收数据
FEvents := IntToEvents(Mask);
DispatchComMsg;
end;
until Signaled <> (WAIT_OBJECT_0 + 1);
// clear buffers
SetCommMask(FComPort.Handle, 0);
PurgeComm(FComPort.Handle, PURGE_TXCLEAR or PURGE_RXCLEAR);
CloseHandle(Overlapped.hEvent);
CloseHandle(FStopEvent);
end;
 
接受答案了.
 
后退
顶部