Monday, July 13, 2015

Arduino Uno and USB Communication

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:

       
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}


void loop() {
  // put your main code here, to run repeatedly:
  Serial.write('A');
  delay(600);
  digitalWrite(13, HIGH);
  delay(200);
  digitalWrite(13, LOW);
  delay(200);
  digitalWrite(13, HIGH);
  delay(200);
  digitalWrite(13, LOW);
}

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:

       
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 ArduinoSerialTest
{
    public partial class Form1 : Form
    {
        private Queue<Char> _queue;
        private Object _monitor;
        private bool _running;

        public Form1()
        {
            InitializeComponent();
            _queue = new Queue<char>();
            _monitor = new object();
            timer1.Start();

            _running = true;
        }

        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (e.EventType == System.IO.Ports.SerialData.Chars)
            {
                while (serialPort.BytesToRead > 0)
                {
                    char c = (char)serialPort.ReadByte();
                    lock (_monitor)
                    {
                        _queue.Enqueue(c);
                        System.Threading.Monitor.Pulse(_monitor);
                    }
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _running = false;
            serialPort.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort.Open();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
            }
            else
            {
                lock (_monitor)
                {
                    while (_queue.Count > 0)
                    {
                        textBox1.Text = textBox1.Text + _queue.Dequeue();
                    }
                }
            }
        }
    }
}


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.

No comments:

Post a Comment