1

I use an Arduino-Nano (knock-off) in a circuit that allows me to programatically switch a Relay off and on. I have this connected for days and weeks at a time. On occasion, the serial communication fails between the Arduino-Nano and my computer.

In my program, I open the port, send a simple command, receive confirmation from the device, and close the port. I don't leave it open.

Here is the function I use to send the command to the arduino:

public static string SendCommandToArduino(string port, string command)
{
    try
    {
        using (var serialPort = new SerialPort(port, 115200, Parity.None, 8, StopBits.One))
        {
            serialPort.ReadTimeout = 100;
            serialPort.WriteTimeout = 100;
            serialPort.Open();
            serialPort.WriteLine(command);
            return serialPort.ReadLine().Replace("\r", "");
        }
    }
    catch (Exception e)
    {
        Log.Write(e);
    }

    return string.Empty;
}

When it is failing and I attempt to connect to it, I get the following exception:

A device attached to the system is not functioning.

   at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
   at System.IO.Ports.InternalResources.WinIOError()
   at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
   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()

The system will work fine for weeks, and then sometimes I get the above exception. No particular pattern, so it's pretty much impossible to reproduce the problem. I can easily fix it by unplugging the Arduino's USB wire and then plugging it back in a few seconds later.

However, I use this Arduino as part of an automated nightly test, so when it fails, I'm not usually sitting there ready to unplug and replug it in.

I have replaced the USB hub I'm using with 2 other powered hubs and have replaced the USB wire with a good quality one. This seems to have reduced the frequency of the failure, but it still acts up on occasion.

I'm using a knock-off arduino, not an original. Is this the problem? Would I have less issues if I use an Arduino Uno, or Mega instead of a Nano?

I'm thinking that Windows 10 might just get confused sometimes with COM ports and lose the connection.

Is there a way to programatically tell windows to power-cycle or reset a com port programatically? My app is in C#.

Curtis
  • 5,794
  • 8
  • 50
  • 77

1 Answers1

1

Cannot say what your exact problem is but it is really common problem with simple serial communication devices that they occassionally lose connection.

You have to make connection in a way that it will reconnect if this happens in any case, unless you specifically want to leave it disconnected.