So, today I decided for the first time to try to communicate with my Arduino Uno via a program in C# using the USB port.

I guess the drivers that come with Arduino make COM3 a serial port. over USB So I created a new form in Visual Studio Express and popped in a SerialPort from the toolbox. Also a timer.

Then I whipped up this program that does a heartbeat and sends out an ‘A’ once per second:

 1 void setup() {
 2  // put your setup code here, to run once:
 3  Serial.begin(9600);
 4  pinMode(13, OUTPUT);
 5  digitalWrite(13, LOW);
 6 }
 7 
 8 
 9 void loop() {
10  // put your main code here, to run repeatedly:
11  Serial.write('A');
12  delay(600);
13  digitalWrite(13, HIGH);
14  delay(200);
15  digitalWrite(13, LOW);
16  delay(200);
17  digitalWrite(13, HIGH);
18  delay(200);
19  digitalWrite(13, LOW);
20 }

Then looked to see if my C# program would show a stream of A.

But first, this was the first time I ever uploaded something to my Arduino. It told me there was a problem when I tried, but I just had select COM3 as the port.

Here is the important part of the C# code:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace ArduinoSerialTest
11 {
12   public partial class Form1 : Form
13   {
14     private Queue<Char> _queue;
15     private Object _monitor;
16     private bool _running;
17 
18     public Form1()
19     {
20       InitializeComponent();
21       _queue = new Queue<char>();
22       _monitor = new object();
23       timer1.Start();
24 
25       _running = true;
26     }
27 
28     private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
29     {
30       if (e.EventType == System.IO.Ports.SerialData.Chars)
31       {
32         while (serialPort.BytesToRead > 0)
33         {
34           char c = (char)serialPort.ReadByte();
35           lock (_monitor)
36           {
37             _queue.Enqueue(c);
38             System.Threading.Monitor.Pulse(_monitor);
39           }
40         }
41       }
42     }
43 
44     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
45     {
46       _running = false;
47       serialPort.Close();
48     }
49 
50     private void Form1_Load(object sender, EventArgs e)
51     {
52       serialPort.Open();
53     }
54 
55     private void timer1_Tick(object sender, EventArgs e)
56     {
57       if (InvokeRequired)
58       {
59       }
60       else
61       {
62         lock (_monitor)
63         {
64           while (_queue.Count > 0)
65           {
66             textBox1.Text = textBox1.Text + _queue.Dequeue();
67           }
68         }
69       }
70     }
71   }
72 }

And the result?   A stream of A. So I can use the SerialPort from .NET to talk to COM3 at whatever baudrate is supportable.

I am now in the process of creating an Arduino driver for the CPLD that will connect to the flash to program it.