I use Cportlib to do several comms applications, but recently we have been developing USB comms equipment that generates virtual comm ports, that can be seen by components like CPort.
I have a problem in that if someone pulls out the usb connection this will raise an error in Cport thus
or at shut down when dissconecting the port
if I attempt to handle this error thus
where that code was called ...
The problem then propagates out to shutdown
this doesnt handle the error correctly as windows will then display an application exception message.
I cant think of a way to handle the error so that the Cport can recover or at least exit gracefully.
Someone has suggested using the APRO comms library, but I don't know if this will solve the problem, and it will involve reworking the code?
Steve: Delphi a feersum engin indeed.
I have a problem in that if someone pulls out the usb connection this will raise an error in Cport thus
Code:
// perform asynchronous write operation
function TCustomComPort.WriteAsync(const Buffer; Count: Integer; var AsyncPtr: PAsync): Integer;
var
Success: Boolean;
BytesTrans: DWORD;
begin
AsyncPtr^.Kind := okWrite;
Success := WriteFile(FHandle, Buffer, Count, BytesTrans, @AsyncPtr^.Overlapped)
or (GetLastError = ERROR_IO_PENDING);
if not Success then
raise EComPort.Create(CError_WriteFailed, GetLastError);
SendSignals(lsTx, True);
Result := BytesTrans;
end;
or at shut down when dissconecting the port
Code:
// abort all asynchronous operations
procedure TCustomComPort.AbortAllAsync;
begin
if not PurgeComm(FHandle, PURGE_TXABORT or PURGE_RXABORT) then
raise EComPort.Create(CError_PurgeFailed, GetLastError);
end;
if I attempt to handle this error thus
Code:
try
Commportdriver.Write(Txbuff, preamble.value + 3);
except
result := false;
end;
where that code was called ...
Code:
if not TxMessage(Acktoken) then
begin
AbortComms := True;
showmessage('A Comm port error has occurred: PClink is unable to'+chr(13)+ .. etc
end;
The problem then propagates out to shutdown
Code:
procedure TEmergiForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if commportdriver.Connected then
try
commportdriver.Close;
except
showmessage('Error Closing Comm port')
end;
this doesnt handle the error correctly as windows will then display an application exception message.
I cant think of a way to handle the error so that the Cport can recover or at least exit gracefully.
Someone has suggested using the APRO comms library, but I don't know if this will solve the problem, and it will involve reworking the code?
Steve: Delphi a feersum engin indeed.