title: C# The semaphore timeout period has expired exception when writing to virtual Serial port
body: <p>I'm trying to communicate with USB device that uses CDC. The device connection is seen in Device Manager as Serial Port. I see the device and able to open the serial port. However, when I try to send a message to device, the <strong>System.IO.IOException</strong> is thrown saying: <strong>"The semaphore timeout period has expired."</strong> I get the same exception with several third party C# Serial port terminal.</p>
  
  <p>Using the same program with USB-RS232 Converter I'm able to communicate with another device with serial port (<strong><em>not CDC</em></strong>)</p>
  
  <p>The CDC device manufacturer says that their module uses standard Windows CDC driver so the problem is on my end. What am I doing wrong?</p>
  
  <p>My code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO.Ports;
  
  namespace ConsoleApplication1
  {
      class Program
      {
          private static readonly SerialPort _serial = new SerialPort();
          static void Main(string[] args)
          {
              var portList = SerialPort.GetPortNames().ToList();
              _serial.PortName = portList[0]; 
              _serial.BaudRate = 9600;
              _serial.Handshake = Handshake.None;
              _serial.Parity = Parity.None;
              _serial.DataBits = 8;
              _serial.StopBits = StopBits.Two;
              _serial.ReadTimeout = 3000;
              _serial.WriteTimeout = 3000;
              _serial.DataReceived += ReceiveData;
              _serial.Open();
  
              while (true)
              {
                  string str = Console.ReadLine();
  
                  if (str.Equals("exit"))
                      return;
                  SerialCmdSend(str);
              }
  
          }
  
          public static void SerialCmdSend(string data)
          {
              if (_serial.IsOpen)
              {
                  try
                  {
                      byte[] hexstring = Encoding.ASCII.GetBytes(data);
                      foreach (byte hexval in hexstring)
                      {
                          byte[] _hexval = { hexval }; 
      //The exception is thrown 3 sec. after the first call of the Write function
                          _serial.Write(_hexval, 0, 1); 
                      }
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                      throw;
                  }
              }
          }
  
  
          private static void ReceiveData(object sender, SerialDataReceivedEventArgs e)
          {
              if (!_serial.IsOpen)
              {
                  return;
              }
              try
              {
                  while (_serial.BytesToRead &gt; 0)
                  {
                      var b = _serial.ReadChar();
                      //Handle read data
                  }
  
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
                  throw;
              }
          }
      }
  }
  </code></pre>