Waiting for events
When Events property is not empty, a special thread is created for monitoring port events when application calls Open method. This is possible only when you use TComPort component in an application (thread) which creates message loop. Most applications have message loop (GUI, NT services). However, if you want to use TComPort component in a console application, you have to set Events property to empty before calling Open method or your application will crash. To monitor events in console application, you have to use WaitForEvent method.
Example
var
ComPort: TComPort;
Events: TComEvents;
begin
ComPort := TComPort.Create(nil);
try
ComPort.Events := []; // do not create monitoring thread
ComPort.Open; // open port
Events := [evCTS, evDSR, evRLSD]; // define events to wait for
ComPort.WaitForEvents(Events, nil, WaitInfinite); // wait until at least one event happens
if evCTS in Events then
WriteLn('CTS Changed'); // CTS signal changed state
ComPort.Close; // close port
finally
ComPort.Free;
end;
end;
Partager