C# interfacing

My problem is that in C# i wrote a code to communicate with the COM 3 port, but when I type in a command using my C# code, my arduino is not doing what I want it to do.

My arduino is programed so that when the user types in 'H' the LED will turn on, and when the user types in 'B' the LED will blink. However instead of typing these commands through the arduino serial monitor i would like the commands to go through the c# program i wrote.

My code for the c# program is: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

namespace Simple_Serial { public partial class Form1 : Form { // Add this variable

string RxString;

public Form1() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { serialPort1.PortName = "COM3"; serialPort1.BaudRate = 9600;

serialPort1.Open(); if (serialPort1.IsOpen) { buttonStart.Enabled = false; buttonStop.Enabled = true; textBox1.ReadOnly = false; }

}

private void buttonStop_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); buttonStart.Enabled = true; buttonStop.Enabled = false; textBox1.ReadOnly = true; } }

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { // If the port is closed, don't try to send a character.

if (!serialPort1.IsOpen) return;

// If the port is Open, declare a char[] array with one element. char[] buff = new char[1];

// Load element 0 with the key character.

buff[0] = e.KeyChar;

// Send the one character buffer. serialPort1.Write(buff, 0, 1);

// Set the KeyPress event as handled so the character won't // display locally. If you want it to display, omit the next line. e.Handled = true;

} private void DisplayText(object sender, EventArgs e) { textBox1.AppendText(RxString); }

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText));

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); } } }

the following link has a picture of the gui i made for this code: http://picasaweb.google.com/116676369266478394688/SerialProgram#

can you tell me what the problem could possibly be?

Thanks!!

Looks fine, If you are not getting any errors with C# then I would recommend making sure you are casting your Serial.Read() to a char in the Arduino sketch because it will be read in as a byte.

Speaking of, can you post your sketch?

because it will be read in as a byte

sp. "int"

int HardwareSerial::read(void)

@Bella - when posting code, please use the "#" button on the posting editor's toolbar.

Is COM3 the port that you upload the code to the Arduino on?

In the button1_Click method, perhaps you should pop up a message box if the connection to the serial port fails, instead of just ignoring it.

In the textBox1_KeyPress method, perhaps you should pop up a message box if the serial port is not open, instead of just ignoring it.

My problem is that in C# i wrote a code to communicate with the COM 3 port, but when I type in a command using my C# code, my arduino is not doing what I want it to do. can you tell me what the problem could possibly be?

You didn't say what the Arduino is doing that you don't want it to, or not doing that you want it to.