There are times where you may want to build a nice user interface for your Arduino program to monitor input/output. In this tutorial, I will show you how to use C# to send data to and receive data from the Arduino. This tutorial assumes basic knowledge of Arduino programming and C#. For testing purposes, we will create a vary basic “echo” program that takes an input and outputs the input.
void setup() { Serial.begin(9600); } void loop() { while(Serial.available()) { Serial.write(Serial.read()); } }
While there is data available, we read it, then write it to the output. Now open Visual Studio and create a new Windows Forms Application. I will name it ArduinoSerial.
From your toolbox, add two Label’s, two ComboBox’s, and a SerialPort. I have named them lblCOM, lblBaud, cboPorts, cboBaud, and serialPort1 respectively. ComboBox’s cboPorts and cboBaud which will allow the user to select which COM port the Arduino is using and the correct Baud rate to use.
To get the available serial ports, use the GetPortNames() method from System.IO.Ports.SerialPort. You can then add this array to cboPorts.
string[] serialPorts = System.IO.Ports.SerialPort.GetPortNames(); cboPorts.Items.AddRange(serialPorts);
For the Baud rate ComboBox I am simply going to enumerate the values.
cboBaud.Items.Add(2400); cboBaud.Items.Add(4800); cboBaud.Items.Add(9600); cboBaud.Items.Add(14400); cboBaud.Items.Add(19200); cboBaud.Items.Add(28800); cboBaud.Items.Add(38400); cboBaud.Items.Add(57600); cboBaud.Items.Add(115200);
The final constructor takes the following form:
public ArduinoSerial() { InitializeComponent(); string[] serialPorts = System.IO.Ports.SerialPort.GetPortNames(); cboPorts.Items.AddRange(serialPorts); cboBaud.Items.Add(2400); cboBaud.Items.Add(4800); cboBaud.Items.Add(9600); cboBaud.Items.Add(14400); cboBaud.Items.Add(19200); cboBaud.Items.Add(28800); cboBaud.Items.Add(38400); cboBaud.Items.Add(57600); cboBaud.Items.Add(115200); cboPorts.SelectedIndex = 0; cboBaud.SelectedIndex = 2; }
Next we are going to create a button called btnStart. This button will initiate the connection to the Arduino. In the code for the buttons click event, we first need to get the port from cboPorts. Then are are going to assign this value to the PortName attribute of the SerialPort object.
serialPort1.PortName = cboPorts.SelectedItem.ToString();
We are going to do the same for the BaudRate attribute.
serialPort1.BaudRate = Convert.ToInt32(cboBaud.SelectedItem.ToString());
Then we are going to open the connection (and toggle the Start/Stop buttons)
if (!serialPort1.IsOpen) { btnStart.Enabled = false; btnStop.Enabled = true; serialPort1.Open(); }
In the btnStop click event is very similar to the above.
private void btnStop_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { btnStart.Enabled = true; btnStop.Enabled = false; serialPort1.Close(); } }
Now we can create a TextBox and a Button called txtInput and btnSend. These two controls will be responsible for sending data to the Arduino. In the btnSend click event add the following code.
if (!serialPort1.IsOpen) return; serialPort1.Write(txtInput.Text + "\n");
The rest of the magic takes place in the SerialPorts DataReceived event. To get the data from the serial port we simply call the ReadExisting() function on the SerialPort object. Then we just need to append that text to a text box. However, we must make this thread safe to prevent any dead locks.
/** * http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx */ delegate void SetTextCallback(string text); private void SetText(string text) { if (this.txtOutput.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.BeginInvoke(d, new object[] { text }); } else { txtOutput.AppendText(text); } } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { SetText(serialPort1.ReadExisting()); } catch (Exception ex) { SetText(ex.ToString()); } }

3. June 2010
No Comments »