I have a .NET 4 Windows service I've written that periodically (usually once a day) communicates with an external device over a serial port. All in all the service works great, but for one customer, every now and then, a call to SerialPort.Open()
throws the following exception:
System.IO.IOException: Insufficient system resources exist to complete the requested service. at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str) at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace) at System.IO.Ports.SerialPort.Open()
Based on the exception, would would think that the server is running low on resources, but that doesn't seem to be the case. The CPU is more or less idle and there's plenty of memory and disk.
There are lots of mentions online of SerialPort.Open()
throwing other IOExceptions and I have implemented Zach Saw's SerialPortFixer, but it appears it fixes a different issue.
Here's an example of what I'm doing (greatly simplified). A couple of instances of this class (using different serial port names) are in memory at all times and then the Run() method is called approximately once a day for each instance.
public class Collector
{
private SerialPort _port;
private string _portName;
public void Run()
{
try
{
// Run Zach Saw's IOException workaround
SerialPortFixer.Execute(_portName);
using (_port = new SerialPort(_portName, 9600, Parity.None, 8, StopBits.One))
{
_port.DataReceived += PortDataReceived;
_port.ErrorReceived += PortErrorReceived;
_port.Handshake = Handshake.None;
_port.DtrEnable = true;
_port.RtsEnable = true;
_port.Open();
// Do the stuff
_port.Close();
}
}
catch (Exception e)
{
// Handle exception
}
}
private void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Do other stuff
}
private void PortErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
// Log error
}
}
Any help would be appreciated.
The answer to this question is that the serial port server used by the client was a wi-fi version (Moxa NPort W2150 Plus) and it turns out the exception occurs when the serial port server has wi-fi connectivity problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With