Wednesday, November 20, 2013

Writing a File to Computer with Arduino

As far as I found, there is no native support for this in the Arduino libraries. Nevertheless, it can be done and with any language of choice! For my demonstration I will use C#. Now so I don't confuse my audience already, writing requires two separate applications, one for the Arduino board and one for the computer - the app for Arduino is in C/C++ while the app for the computer is in C# (or any supported language of your choosing really).

To get started, lets look at the code for the Arduino:

int led = 13;
bool flag = true;

void setup()
{
    pinMode(9, OUTPUT);
}

void loop()
{
    Serial.begin(9600);
    Serial.println("Hello World!");
    Serial.end();
    Toggle();
    delay(500);
}

void Toggle(){
     if(flag) digitalWrite(led, HIGH);
     else digitalWrite(led, LOW);
     flag = !flag;
}


For every 500 milliseconds, the board will send the message "Hello World!" via USB to the computer. Any program listening on that port, will hear the message. (I added a little extra code that toggles an on-board LED on and off after every consecutive message sent - just to signify it itself is functioning properly.)

Serial.begin() primes the USB for data transfer and the (9600) portion is the baud rate (pulses per second - the ancestor of bits per second).
Serial.end() ends the transmission and releases control of the port.
Serial.println() writes ASCII data to the port.
A list of Serial functions can be found on Arduino.cc under References: http://arduino.cc/en/Reference/Serial




Now on to the computer side of things:

using System;
using System.IO.Ports;
using System.Threading;

namespace Arduino_SerialReader
{
    class Program
    {
        static SerialPort port;
        static void Main(string[] args)
        {
            port = new SerialPort("COM", 9600, Parity.None);
            Thread reader = new Thread(Read);

            port.ReadTimeout = 500;

            port.Open();
            reader.Start();
        }

        static void Read()
        {
            bool flag = true;
            while (flag)
            {
                try
                {
                    string message = port.ReadLine();
                    Console.WriteLine(message);
                }
                catch { }
            }
        }
    }
}


First, create a SerialPort object and give it a name: 'COM', the same baud rate set for the Arduino board (9600), and finally, no parity checking. (A parity bit is a simple way of error checking data received; more on parity checking can be found here) Purposefully, I set the timeout to be the same as my first app so to be almost perfectly synchronous.

Open the port and start the Read thread. The read thread is a separate thread that will indefinitely listen for any and all incoming messages on the port and then print them to the console.

So where is the code to write a file? Well, by now you would know enough to take it from here, but if you really need to know how to write a file in C#, here is a simple one-liner hack:

File.WriteAllText("filename.txt", "your string here");

More file handling example can be found here: microsoft OR stackoverflow
Feel free to ask questions!


No comments:

Post a Comment