title: threaded serial port IOException when writing
body: <p>I'm trying to write a small application that simply reads data from a socket, extracts some information (two integers) from the data and sends the extracted information off on a serial port.</p>
  <p>The idea is that it should start and just keep going. In short, it works, but not for long. After a consistently short period I start to receive IOExceptions and socket receive buffer is swamped.</p>
  <p>The thread framework has been taken from the MSDN serial port example.</p>
  <p>The delay in send(), readThread.Join(), is an effort to delay read() in order to allow serial port interrupt processing a chance to occur, but I think I've misinterpreted the join function. I either need to sync the processes more effectively or throw some data away as it comes in off the socket, which would be fine. The integer data is controlling a pan tilt unit and I'm sure four times a second would be acceptable, but not sure on how to best acheive either, any ideas would be greatly appreciated, cheers.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.IO.Ports;
  using System.Threading;
  using System.Net;
  using System.Net.Sockets;
  using System.IO;
  
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static bool _continue;
          static SerialPort _serialPort;
          static Thread readThread;
          static Thread sendThread;
          static String sendString;
          static Socket s;
          static int byteCount;
          static Byte[] bytesReceived;
  
          // synchronise send and receive threads
          static bool dataReceived;
  
          const int FIONREAD = 0x4004667F;
  
          static void Main(string[] args)
          {
              dataReceived = false;
              readThread = new Thread(Read);
              sendThread = new Thread(Send);
  
              bytesReceived = new Byte[16384];
  
              // Create a new SerialPort object with default settings.
              _serialPort = new SerialPort(&quot;COM4&quot;, 38400, Parity.None, 8, StopBits.One);
  
              // Set the read/write timeouts
              _serialPort.WriteTimeout = 500;
  
              _serialPort.Open();
              string moveMode = &quot;CV &quot;;
              _serialPort.WriteLine(moveMode);
  
              s = null;
              IPHostEntry hostEntry = Dns.GetHostEntry(&quot;localhost&quot;);
              foreach (IPAddress address in hostEntry.AddressList)
              {
                  IPEndPoint ipe = new IPEndPoint(address, 10001);
                  Socket tempSocket =
                      new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  
                  tempSocket.Connect(ipe);
  
                  if (tempSocket.Connected)
                  {
                      s = tempSocket;
                      s.ReceiveBufferSize = 16384;
                      break;
                  }
                  else
                  {
                      continue;
                  }
              }
  
              readThread.Start();
              sendThread.Start();
  
              while (_continue)
              {
                  Thread.Sleep(10);
                  ;// Console.WriteLine(&quot;main...&quot;);
              }
  
              readThread.Join();
              _serialPort.Close();
              s.Close();
          }
  
          public static void Read()
          {
              while (_continue)
              {
                  try
                  {
                      //Console.WriteLine(&quot;Read&quot;);
                      if (!dataReceived)
                      {
                          byte[] outValue = BitConverter.GetBytes(0);
                          // Check how many bytes have been received.
                          s.IOControl(FIONREAD, null, outValue);
                          uint bytesAvailable = BitConverter.ToUInt32(outValue, 0);
  
                          if (bytesAvailable &gt; 0)
                          {
                              Console.WriteLine(&quot;Read thread...&quot; + bytesAvailable);
                              byteCount = s.Receive(bytesReceived);
                              string str = Encoding.ASCII.GetString(bytesReceived);
                              //str = Encoding::UTF8-&gt;GetString( bytesReceived );
                              string[] split = str.Split(new Char[] { '\t', '\r', '\n' });
  
                              string filteredX = (split.GetValue(7)).ToString();
                              string filteredY = (split.GetValue(8)).ToString();
  
                              string[] AzSplit = filteredX.Split(new Char[] { '.' });
                              filteredX = (AzSplit.GetValue(0)).ToString();
                              string[] ElSplit = filteredY.Split(new Char[] { '.' });
                              filteredY = (ElSplit.GetValue(0)).ToString();
  
                              // scale values 
                              int x = (int)(Convert.ToInt32(filteredX) * 1.9);
                              string scaledAz = x.ToString();
                              int y = (int)(Convert.ToInt32(filteredY) * 1.9);
                              string scaledEl = y.ToString();
  
                              String moveAz = &quot;PS&quot; + scaledAz + &quot; &quot;;
                              String moveEl = &quot;TS&quot; + scaledEl + &quot; &quot;;
  
                              sendString = moveAz + moveEl;
                              dataReceived = true;
                          }
                      }
                  }
                  catch (TimeoutException) {Console.WriteLine(&quot;timeout exception&quot;);}
                  catch (NullReferenceException) {Console.WriteLine(&quot;Read NULL reference  exception&quot;);}
              }
          }
  
          public static void Send()
          {
              while (_continue)
              {
                  try
                  {
                      if (dataReceived)
                      {
                          // sleep Read() thread to allow serial port interrupt     processing 
                          readThread.Join(100);
                          // send command to PTU
              dataReceived = false;
                          Console.WriteLine(sendString);
                          _serialPort.WriteLine(sendString);
                      }
                  }
                  catch (TimeoutException) { Console.WriteLine(&quot;Timeout exception&quot;); }
                  catch (IOException) { Console.WriteLine(&quot;IOException exception&quot;); }
                  catch (NullReferenceException) { Console.WriteLine(&quot;Send NULL reference exception&quot;); }
              }
          }
      }
  }
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <p>Thanks for the response Jon.</p>
  <p>What I'm attempting to do is poll a <strong>socket</strong> for data, if its there process it and send it to the <strong>serial port</strong>, else keep polling the <strong>socket</strong> , repeating this whole process ad nauseum.</p>
  <p>My initial attempt used a single thread and I was getting the same problem, which led me to believe that I need to give the serial port some more time to allow it to send the data before giving it more data on the next loop, because once I've sent data to the serial port I'm back polling the socket very hard. Having said that IOExceptions occur after approximately 30 seconds of operation, possibly with what I'm saying is I should see IOExceptions immediately?</p>
  <p>My interpretation of the join function, I think, is incorrect, ideally calling readThread.Join from send() would allow read() to sleep while still pumping the COM port, but where I have it seems to put the send() to sleep, which I guess is the calling function?? and not producing the desired result.</p>