#29 Restore original DBC state by closing

open
nobody
None
5
2010-09-01
2010-09-01
Anonymous
No

After using CPort, other ComPort librarys can have problems. Could be fixed with save and restore DBC state befor and after using CPort see code below.
Gruss Heinz

const
{Heinz}CError_GetStateFailed = 24;

var
{Heinz}actualDCB: _DCB;

// create handle to serial port
procedure TCustomComPort.CreateHandle;
var
dev : String;
flags : Cardinal;
begin
dev := '\\.\' + FPort;

if FOverlapped then { This is a big change from classic TComPort }
flags := FILE_FLAG_OVERLAPPED
else
flags := 0;

FHandle := CreateFile(
PChar(dev),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
flags,
0);

if FHandle = INVALID_HANDLE_VALUE then
begin
{$ifdef CPORT_CALLTRACE}
OutputDebugString(PChar('CreateHandle: CreateFile ' + dev));
{$endif}
raise EComPort.Create(CError_OpenFailed, GetLastError, FPort);
{Heinz}//end;
//Originalzustand einlesen
end else
If not GetCommState(FHandle, actualDCB) then
raise EComPort.Create(CError_GetStateFailed, GetLastError, FPort);
end;

// destroy serial port handle
procedure TCustomComPort.DestroyHandle;
begin
if FHandle <> INVALID_HANDLE_VALUE then
{Heinz}//CloseHandle(FHandle);
begin
//Originalzustand zurückschreiben
If not SetCommState(FHandle, actualDCB) then
raise EComPort.Create(CError_SetStateFailed, GetLastError, FPort);
CloseHandle(FHandle);
end;
end;

Discussion

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks