title: C# program to communicate with a xbee module using COM Ports / Using USB Serial COM Port
body: <p>I am working a c# program which use the usb serial COM Port to transmit and receive data. The component is XBEE module which I want ot access but the code I wrote dosen't detect any serial port on my Computer. Any one who can help me rectifying my mistake.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using System.IO.Ports;
  
  namespace WindowsFormsApplication3
  {
      public partial class Test : Form
      {
          private StringBuilder receivedData = new StringBuilder();
  
          public Test()
          {
              InitializeComponent();
          }
  
          private void Test_Load(object sender, EventArgs e)
          {
              Array ports = System.IO.Ports.SerialPort.GetPortNames();
              for (int x = 0; x &lt;= ports.Length; comboBox1.Items.Add(ports.GetValue(x))) ;
              timer1.Start();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              serialPort1.PortName = comboBox1.Text;
              if (!serialPort1.IsOpen)
              {
                  serialPort1.Open();
              }
          }
  
          private void button2_Click(object sender, EventArgs e)
          {
              if (serialPort1.IsOpen)
              {
                  serialPort1.Close();
              }
          }
  
          private void Test_FormClosing(object sender, FormClosingEventArgs e)
          {
              if (serialPort1.IsOpen)
              {
                  serialPort1.Close();
              }
          }
  
          private void button3_Click(object sender, EventArgs e)
          {
              if (serialPort1.IsOpen)
              {
                  serialPort1.Write(textBox2.Text + "\n\r");
              }
          }
  
          private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
              receivedData.Append(serialPort1.ReadExisting());
          }
  
          private void timer1_Tick(object sender, EventArgs e)
          {
              textBox1.Text = receivedData.ToString();
          }
      }
  }
  </code></pre>