Using an Arduino with C#

I am currently working on a project, in which, I need to be able to set different configurations from a computer and have them sent to an Arduino. I would also like the program on the computer, to be able to find the Arduino automatically without any input from the user. Both these requests seem very reasonable and I know I have seen other projects with such features in the past. With this in mind I went to Google to see if I could find how I would be able to accomplish this task myself.

One of the first links to come up was from the Arduino playground with the title, “Detect Arduino com port and use Serial Comms.” This writeup had both features I wanted, a way to send information to the Arduino and a way for the program to find what port the Arduino was on. I read through the article and then tried it out for myself. I ran into a few problems, so I thought I would do my own writeup base on the work I found. The original writeup and code came from Richard210363 of the Arduino forums. Thank you for your hard work.

Communications Protocol:

Being that I simply added to the work I already had, the communications protocol is the same from the source article.

To send a message you have to send five sets of bytes. The first byte is used to start the message, while the second is used to send the first command. Using this method you can have the Arduino preform different tasks based on what you send it.

For this example, when you send 128 in the first command byte, the Arduino will announce itself with an “HELLO” response.

Sketch:


/*

* Serial Port Monitor
*
*
* Original code from http://playground.arduino.cc//Csharp/SerialCommsCSharp
* Code by Richard210363
* Code Taken from Website 11/11/13
*
* This code has been modified by
* Mario Avenoso  11/11/13
* Added to this code was more comments to better explain how the different
* parts work. This code was also changed as I found the code, as is, from
* the Arduion website did not compile
*/

//Setup Output
// int ledPin_3 = 3;

//Setup message bytes

byte inputByte_0;//used to store starting bit

byte inputByte_1;

byte inputByte_2;

byte inputByte_3;

byte inputByte_4;

//Setup
int ledPin_3 =13;

void setup() {

    pinMode(ledPin_3, OUTPUT);
    Serial.begin(9600);
    digitalWrite(ledPin_3, HIGH);//
    delay(250);//
    digitalWrite(ledPin_3, LOW);//
    delay(250);//

}

//Main Loop

void loop() {

//Read Buffer
if (Serial.available() == 5) //wait until all 5 bytes have been sent from
//the computer
{
      //Read buffer
    inputByte_0 = Serial.read();//start of the communication
    delay(100);
    inputByte_1 = Serial.read();//type of command
    delay(100);
    inputByte_2 = Serial.read();//first value
    delay(100);
    inputByte_3 = Serial.read();//second value
    delay(100);
    inputByte_4 = Serial.read(); //third value
}
//Check for start of Message
if(inputByte_0 == 16)
{
    //Detect Command type
    switch (inputByte_1)
{
    case 127:
    //check for pin and detect if high or low
switch (inputByte_2)
{
    case 4://if byte is four then the led is selected
    if(inputByte_3 == 255)//sending 255 will enable the led
    {

        digitalWrite(ledPin_3, HIGH);
        break;
    }
    else //sending any other value will disable the led
    {
        digitalWrite(ledPin_3, LOW);
        break;
    }
    break;
}
    break;
    case 128://this case is used for Arduino detection by the computer
    //by first sending the value of 128, the C# program is able to
    //find the port the Arduino is on automatically
    //Say hello
    Serial.print("HELLO");//Short message to let the computer know
    //that there is an Arduino connected to that port
    break;
}
//Clear Message bytes
inputByte_0 = 0;
inputByte_1 = 0;
inputByte_2 = 0;
inputByte_3 = 0;
inputByte_4 = 0;
//not needed for this example
//Let the PC know we are ready for more data
//delay(1000);
//Serial.print("-READY TO RECEIVE");
}

}

C Sharp:

In the original write up, all that was given was a C# class to be used with a project. This is helpful if you just want to add functionality to an existing project, but might be a bit confusing to someone not to familiar with C#.

I also found that there was some issues with the posted class that would not allow it to compile. What I did was created a Form application and added the C# classes from the other article. I then went line by line explaining (the best I could) what each part did. This is in hopes that someone who has just started with C#, or anyone who wanted to created a computer program for use with the Arduino, could do so with as little knowledge as possible about programing in the language.

Download:

In the zip file, you will find the .ino file, a precompiled port finder, and the source code. I should note that I was using Visual Studio 2010 on Windows 7, the code should still work with other versions but I’m not sure. Your mileage may vary.

Download “C_Sharp_Auto_Detect_Arduino.zip”

C_Sharp_Auto_Detect_Arduino.zip – Downloaded 2749 times – 39.09 KB
Arduino, Arduino C, C#, Hardware, Programing , , , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.