title: C# - Arduino Serial Communication freezes(?) while reading Data
body: <p>My program checks on startup if an Arduino is connected, and if that's the case, it sends a test message via Serial Port to see if it responds correctly. It then waits for the result, and if the answer is "success", it continues starting up.</p>
  
  <p>Here's the important part of the code:</p>
  
  <pre><code>...
  using System.IO.Ports;
  using System.Threading;
  
  namespace ProFlagControlApp
  {
      public partial class MainWindow : Window
      {
          static AutoResetEvent autoEvent = new AutoResetEvent(false);
  
          ...
          private SerialPort arduinoBoard = new SerialPort();
          private string ardAnswer;
  
          /// &lt;summary&gt;
          /// Automatically detect the COM port on which an Arduino is connected.
          /// &lt;/summary&gt;
          /// &lt;returns&gt;If an Aduino is connected, the port is returned as a string. If not, it returns null.&lt;/returns&gt;
          private string AutodetectArduinoPort() { ... }
  
          /// &lt;summary&gt;
          /// Initializing communications with the Arduino.
          /// &lt;/summary&gt;
          /// &lt;param name="port"&gt;The identifier of the port the Arduino is connected to. Example: 'COM4'&lt;/param&gt;
          private void OpenArduinoConnection(string port)
          {
              if (!arduinoBoard.IsOpen)
              {
              arduinoBoard.DataReceived += new SerialDataReceivedEventHandler(ArdSerPort_DataReceived);
              arduinoBoard.BaudRate = 115200;
              arduinoBoard.PortName = port;
              arduinoBoard.Parity = Parity.None;
              arduinoBoard.DataBits = 8;
              arduinoBoard.StopBits = StopBits.One;
              arduinoBoard.Handshake = Handshake.None;
  
              arduinoBoard.Open();
              }
              else
              {
                  throw new InvalidOperationException("port is already in use");
              }
          }
  
          /// &lt;summary&gt;
          /// The event handler for receiving data from the Arduino.
          /// &lt;/summary&gt;
          private void ArdSerPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              string data = arduinoBoard.ReadTo("\x03"); // Read Arduino data until exit code
              ardAnswer = data.Split('\x02', '\x03')[1]; // Only save information between the start and exit code
              autoEvent.Set();
          }
  
          public MainWindow()
          {
              InitializeComponent();
  
              ...
  
              // Detect if Arduino is connected, shutdown the application otherwise.
              if (AutodetectArduinoPort() == null) { ... }
  
              OpenArduinoConnection(AutodetectArduinoPort());
  
              // Test Arduino communication
              arduinoBoard.Write("connection#");
  
              autoEvent.WaitOne(500);
              if (ardAnswer != "success")
              {
                  MessageBox.Show("Error communicating with Arduino", "Control Unit Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                  Application.Current.Shutdown();
                  return;
              }
  
              ...
          }
  
          ...
      }
  }
  </code></pre>
  
  <p>I checked via the Arduino Serial Monitor if the command is read correctly and the appropriate response message is written into the Serial Port, which is the case.</p>
  
  <p>However, the <code>ArdSerPort_DataReceived</code> event is never triggered. When I try to manually put a <code>ardAnswer = arduinoBoard.ReadTo("\x03");</code> before it tests what is in the <code>ardAnswer</code> variable, the program seems to freeze and does not continue with anything.</p>
  
  <p>I am really wondering why. I have to admit that I haven't touched this program for quite a while now, but when I last worked on it, it all behaved as it should, with the exact same code.</p>