title: Sending ASCII from C# to Arduino using Bluetooth
body: <p>I am trying to interface my C# <a href="http://en.wikipedia.org/wiki/Windows_Forms" rel="nofollow">Windows Forms</a> application from my laptop to the Arduino <a href="http://arduino.cc/en/Main/ArduinoBoardDuemilanove" rel="nofollow">Duemilanove</a>. A Bluetooth module is connected to the Tx and Rx pins on the Arduino. My goal is to light up the on-board LED when I type in the letter 'a' and so far it has been unsuccessful. I am sure the Bluetooth is connected with my laptop, but it is not responding to the letter I am pressing.</p>
  
  <h3>C# code</h3>
  
  <pre><code>public partial class Form1 : Form
  {
      private Guid service = BluetoothService.SerialPort;
      private BluetoothClient bluetoothClient;
  
      public Form1()
      {
          InitializeComponent();
  
          this.KeyPreview = true;
          this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
      }
  
      void Form1_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (e.KeyChar == 'a')
          {
              Stream peerStream = bluetoothClient.GetStream();
              Byte[] buffer = Encoding.ASCII.GetBytes("a");
              peerStream.Write(buffer, 0, buffer.Length);
          }
      }
  
      private void search_Click(object sender, EventArgs e)
      {
          BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
          BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
          bluetoothClient = new BluetoothClient();
          Cursor.Current = Cursors.WaitCursor;
          BluetoothDeviceInfo[] bluetoothDeviceInfo = { };
          bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10);
          comboBox1.DataSource = bluetoothDeviceInfo;
          comboBox1.DisplayMember = "DeviceName";
          comboBox1.ValueMember = "DeviceAddress";
          comboBox1.Focus();
          Cursor.Current = Cursors.Default;
      }
  
      private void Connect_Click(object sender, EventArgs e)
      {
          if (comboBox1.SelectedValue != null)
          {
              try
              {
                  bluetoothClient.Connect(new BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue, service));
                  MessageBox.Show("Connected");
  
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
      }
  }
  </code></pre>
  
  <h3>Arduino Code</h3>
  
  <pre><code>int incomingByte = 0;   // For incoming serial data
  void setup()
  {
      pinMode(13, OUTPUT); // On-board LED as output
      Serial.begin(9600);     // Opens serial port, sets data rate to 9600 bit/s.
  }
  
  void loop()
  {
      if (Serial.available() &gt; 0)
      {
          // Read the incoming byte:
          incomingByte = Serial.read();
  
          if (incomingByte == 'a')
              digitalWrite(13, HIGH);
      }
  }
  </code></pre>
  
  <p>Am I sending the ASCII code wrongly or what am I missing?</p>