EDN Admin
Well-known member
Hello,
I am facing one problem with System.IO.SerialPort connection. I am using .net framework 2.0
I am developing an SDK which communicate with my device which is connected with my pc via RS232 com port. This sdk is being used by a web application.
At first scenario it works fine if no error occured inbetween the com port Open and Close method called...
But in second senario when I open com port and communicating with my device and if any error occurs or application crashes or some one refresh the page from the browser, after that second time I am not able to reconnected with my device with the same com port.
SerialPort.Open() method throws System.UnauthorizedAccessException.
I tried to found the solution of this problem via google but that all work around is not enough for me. What I want to do is that, If this error occurs I want to close that port and want to use that port again to communicate with my device.
Any help is appreciated.....internal class SerialConnection: IDisposable
{
#region Global Fields
private SerialPort serialPort = null;
private ICOMPort serialPortSettings = null;
#endregion
#region Constructor
public SerialConnection(ICOMPort serialCommPort)
{
this.serialPortSettings = serialCommPort;
}
#endregion
#region Properties
/// <summary>
/// Gets SerialPortSettings
/// </summary>
internal ICOMPort SerialPortSettings
{
get
{
return this.serialPortSettings;
}
}
#endregion
#region Operations
public bool Connect()
{
try
{
//// Close the serial port if it is open
if (this.serialPort != null && this.serialPort.IsOpen)
{
this.serialPort.Close();
}
//// configure the various paramters of the serial port
this.serialPort = new SerialPort
{
PortName = this.serialPortSettings.PortName,
BaudRate = this.serialPortSettings.BaudRate,
Parity = this.serialPortSettings.Parity,
DataBits = this.serialPortSettings.DataBitsPerByte,
StopBits = this.serialPortSettings.StopBit,
DtrEnable = true,
RtsEnable = true,
Handshake = Handshake.None
////ReadTimeout = this.serialPortSettings.ReceiveDataTimeout,
////WriteTimeout = this.serialPortSettings.SendDataTimeout
};
////calling the Close method before attempting to call the Open method,
////as the port may not be closed instantly.
Thread.Sleep(1000);
////opens serial port with specified settings
this.serialPort.Open();
}
catch (ArgumentOutOfRangeException ex)
{
//handle exception
}
catch (IOException ex)
{
//handle exception
}
catch (UnauthorizedAccessException ex)
{
//handle exception
}
return this.serialPort.IsOpen;
}
public bool Close()
{
try
{
if (this.serialPort != null && this.serialPort.IsOpen)
{
Thread.Sleep(500);
this.serialPort.Close();
}
}
catch (InvalidOperationException ex)
{
//Handler exception
}
var port = this.serialPort;
return (port != null && port.IsOpen);
}
public void Dispose()
{
this.Dispose(true);
}
#endregion
#region IDisposable Implemenation
protected virtual void Dispose(bool disposing)
{
if (this.serialPort == null)
{ return; }
if (this.serialPort.IsOpen)
{
this.serialPort.Close();
}
this.serialPort.Dispose();
}
#endregion
}
Thank you
Regards, Hiren Bharadwa
View the full article
I am facing one problem with System.IO.SerialPort connection. I am using .net framework 2.0
I am developing an SDK which communicate with my device which is connected with my pc via RS232 com port. This sdk is being used by a web application.
At first scenario it works fine if no error occured inbetween the com port Open and Close method called...
But in second senario when I open com port and communicating with my device and if any error occurs or application crashes or some one refresh the page from the browser, after that second time I am not able to reconnected with my device with the same com port.
SerialPort.Open() method throws System.UnauthorizedAccessException.
I tried to found the solution of this problem via google but that all work around is not enough for me. What I want to do is that, If this error occurs I want to close that port and want to use that port again to communicate with my device.
Any help is appreciated.....internal class SerialConnection: IDisposable
{
#region Global Fields
private SerialPort serialPort = null;
private ICOMPort serialPortSettings = null;
#endregion
#region Constructor
public SerialConnection(ICOMPort serialCommPort)
{
this.serialPortSettings = serialCommPort;
}
#endregion
#region Properties
/// <summary>
/// Gets SerialPortSettings
/// </summary>
internal ICOMPort SerialPortSettings
{
get
{
return this.serialPortSettings;
}
}
#endregion
#region Operations
public bool Connect()
{
try
{
//// Close the serial port if it is open
if (this.serialPort != null && this.serialPort.IsOpen)
{
this.serialPort.Close();
}
//// configure the various paramters of the serial port
this.serialPort = new SerialPort
{
PortName = this.serialPortSettings.PortName,
BaudRate = this.serialPortSettings.BaudRate,
Parity = this.serialPortSettings.Parity,
DataBits = this.serialPortSettings.DataBitsPerByte,
StopBits = this.serialPortSettings.StopBit,
DtrEnable = true,
RtsEnable = true,
Handshake = Handshake.None
////ReadTimeout = this.serialPortSettings.ReceiveDataTimeout,
////WriteTimeout = this.serialPortSettings.SendDataTimeout
};
////calling the Close method before attempting to call the Open method,
////as the port may not be closed instantly.
Thread.Sleep(1000);
////opens serial port with specified settings
this.serialPort.Open();
}
catch (ArgumentOutOfRangeException ex)
{
//handle exception
}
catch (IOException ex)
{
//handle exception
}
catch (UnauthorizedAccessException ex)
{
//handle exception
}
return this.serialPort.IsOpen;
}
public bool Close()
{
try
{
if (this.serialPort != null && this.serialPort.IsOpen)
{
Thread.Sleep(500);
this.serialPort.Close();
}
}
catch (InvalidOperationException ex)
{
//Handler exception
}
var port = this.serialPort;
return (port != null && port.IsOpen);
}
public void Dispose()
{
this.Dispose(true);
}
#endregion
#region IDisposable Implemenation
protected virtual void Dispose(bool disposing)
{
if (this.serialPort == null)
{ return; }
if (this.serialPort.IsOpen)
{
this.serialPort.Close();
}
this.serialPort.Dispose();
}
#endregion
}
Thank you
Regards, Hiren Bharadwa
View the full article