• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 

Exception while re-opening COM-Port

Started by Arne, August 03, 2010, 10:46:39 am

Previous topic - Next topic

Arne

Hello Roman,

we are working with nrComm 9.07, Delphi 2007 and Windows XP SP3 (32bit).
I get an exception sometimes when I try to reopen a COM-Port (which is actually an USB Virtual COM Port).
For easier use I wrote two wrappers for opening and closing the COM-Port:

***************************************************************************************************
  Name    : ComPortOpen
  Function: An API for accessing the nrcomm component in a more convenient way.

  Parameters:
  Name             I/O Type                                   Remark
  inoutComPortObj               I   TnrComm                                 The TnrComm object we want to change the properties
  inComPort                     I   Integer                                 [1..8] for COM1 .. COM8
  inBufferSize                  I   Integer                                 I/O buffersize of ring-buffer in byte
  inBaudrate                    I   Cardinal                                baudrate in bps
  inParity                      I   TParity                                 pNone, pEven, pOdd, pMark, pSpace
  inStopBits                    I   TStopBits                               sbOne, sbOneAndHalf, sbTwo
  inDataBits                    I   Byte                                    [4..8]
  ComPortOpen                     O Boolean                                 True  = successfully opened COM-Port
                                                                            False = an error occurred while openening the COM-Port
**************************************************************************************************}
function ComPortOpen(inoutComPortObj : TnrComm; inComPort : Integer; inBufferSize : Integer; inBaudrate : Cardinal; inParity : TParity; inStopBits : TStopBits; inDataBits : Byte) : Boolean;
begin
  Result := False;
  { nrComm Port initialization }
  if (inComPort > 0) and (inComPort < 100) and Assigned(inoutComPortObj) then begin
    if inoutComPortObj.Active = False then begin
      try
        inoutComPortObj.Update;
        inoutComPortObj.ComPortNo      := inComPort;
        inoutComPortObj.BufferInSize   := inBufferSize;
        inoutComPortObj.BufferOutSize  := inBufferSize;
        inoutComPortObj.ClearDeviceBuffers;
        inoutComPortObj.StreamProtocol := spNone;
        inoutComPortObj.BaudRate       := inBaudrate;
        inoutComPortObj.Parity         := inParity;
        inoutComPortObj.StopBits       := inStopBits;
        inoutComPortObj.ByteSize       := inDataBits;
        inoutComPortObj.TraceStates    := [tsRxChar, tsTxEmpty];
        try
          inoutComPortObj.Active       := True;
        except
          ShowMessage('Could not open COM' + IntToStr(inComPort));
        end;
      finally
        if inoutComPortObj.Active = True then begin
          Result := True;
        end;
      end;
    end
    else begin
      ShowMessage('COM' + IntToStr(inComPort) + ' already opened!');
    end;
  end;
end;

{***************************************************************************************************
  Name    : ComPortClose
  Function: Will close a COM-Port specified by the parameter object.

  Parameters:
  Name             I/O Type                                   Remark
  inComPortObj                  I   TnrComm                                 COM-Port object to close.
**************************************************************************************************}
procedure ComPortClose(inComPortObj : TnrComm);
begin
  if Assigned(inComPortObj) then begin
    inComPortObj.ClearDeviceBuffers;
    try
      inComPortObj.Active := False;
    except
      ShowMessage('Could not close COM' + IntToStr(inComPortObj.ComPortNo));
    end;
  end;
end;


My question is: is there a possibility that nrCommUSB.Active is False, but at that point when I reopen the same COM-Port the port wasn't closed 100%?
I am using this ugly code snippet for opening the COM-Port:

    ComPortClose(nrCommUSB);
    repeat
      Sleep(200);  { give nrComm Thread time to close the COM Port }
    until nrCommUSB.Active = False;

    if ComPortOpen(nrCommUSB, myComPortNo, 4 * 1024, 9600, pNone, sbOne, 8) = True then begin
    (...)

And sometimes it crashes while calling the ComPortOpen(..) function with an exception.

Thanx, Arne

Roman Novgorodov

Hello

nrComm component closes serial port synchronously. It calls CloseHandle system function.

It seems like problem in USB-To-Serial adapter driver.

You can try USB adapter with other chipset and drivers. Possible you will get other result.

I'm sorry if my reply is too late. :-(

Roman Novgorodov
DeepSoftware.Com
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Roman Novgorodov

DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.