Author Topic: Exception while re-opening COM-Port  (Read 1556 times)

Arne

  • Newbie
  • *
  • Posts: 15
    • View Profile
Exception while re-opening COM-Port
« on: August 03, 2010, 01:46:39 pm »
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:
Code: [Select]
***************************************************************************************************
  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:
Code: [Select]
    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

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Exception while re-opening COM-Port
« Reply #1 on: August 09, 2010, 06:12:54 pm »
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

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Exception while re-opening COM-Port
« Reply #2 on: August 27, 2010, 01:24:55 pm »
Hello

Possible this trouble was related with this topic:
http://forums.nrcommlib.com/index.php?topic=1191.0

You can try new trial beta version:
http://www.nrcommlib.com/nrcomm910beta.zip

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.