Arduino Mega & C# .NET

Hi Guys,

I am wanting to integrate by tecom challenger alarm with my PC using an Arduino Mega board. However i want to develop the application in C# .NET

Has anyone already done this? If not, can it be done?

Thanks,
Adam

I don't know what the specific requirements for interfacing with the alarm is, but you can easily talk to Arduino over serial from a c# program.

Search the forum for a couple of recent threads about it.

The way the alarm and the arduino mega will talk to each other is through the analogue/digital inputs/outputs using a dry contact. All my program will do is check to see if there is an input and will tell the arduino to output. How easy is this?

Also, when you refer to serial, do you mean serial over USB?

Thanks,
Adam

Reading inputs on Arduino and sending the result to a c# program , and the other way around should be quite easy to implement.

Yes it's serial over USB.

The USB port on Arduino is a serial port in disguise. When you install the Arduino software on a PC (or Mac or Linux) you instal a driver for a virtual serial port. So your c# program will see it as any other serial port, except it's only present when your Aduino board is plugged into a USB port on the PC.

.net has a built in class for doing serial port stuff. I only have VB.net experience but since it's basically .net it should be almost the same.

Hi,

I've been working on a C# GUI myself to interface with the arduino board.

It's fairly straight forward.

Sounds like on the GUI side you can have it sit in a loop waiting for information.

while (_serial.BytesToRead == 0) {};

When it exits the loop you can read in the data and use it accordingly.

On the Arduino Side, just used Serial.print("data") or Serial.println("data")
to send your info when appropriate.

Post if you have any issues

If you only need one way communication it is very easy (two-way is not bad either i have a class for that if you need it).

C# Snippet

using System.IO.Ports;
private void connectToArduino()
{
        SerialPort activeSerial = new SerialPort("com4", 9600, Parity.None, 8, StopBits.One); //Change COM4 to your arduino port
          activeSerial.ReadBufferSize = 128;
          activeSerial.Open();
          activeSerial.DataReceived += new SerialDataReceivedEventHandler(activeSerial_DataReceived);
}

void activeSerial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
      string dataFromArduino = activeSerial.ReadLine();
      //Do what you want with the data here
}

Arduino Snippet

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
}

void sendData()
{
  Serial.print("DaTA");
  Serial.println(); //This is needed for the c# app to know where to stop listening
  delay(5);
}

void loop()
{
  sendData();
}

The beauty of this is it takes advantage of the threading abilities your PC has. So it will run in the background of your app. Not in your main thread. So you can interact with the GUI (if your using one). Just be sure to run activeSerial.Close(); when you close your form/app.

For multi values i tend to user a terminal char like ':'. So i will send data from my arduino that looks like this.

Serial.print(someInt);
Serial.print(":");
Serial.print("3.21");
Serial.print(":");
Serial.print("54324");
Serial.print(":");
Serial.print("123");
Serial.println();

Thant outputs this to the c# app
12:3.21:54324:123

Then on the c# side i split them into a string array and since i know the order i just log it however i want

string [] splitData = dataFromArduino.split(':'); 
int value = int.parse(splitData[0]); //This is the 12 that was sent
double doubleValue = double.parse(splitData[1]); //This is now a double with a value of 3.21

//So on and so forth

//lance

I think I just stampled upon the right page to get some answers...I am trying to design a GUI in C# to interact with my Arduino. I need to read and write to the serial port of the Arduino....send digits 1,2 to control an LED on or off and send "received" message back to the C# GUI to be displayed in text box let's say...i see some discussion in that pattern but I was wondering if anybody knows how hard it is and how much time for a beginner in C# (and programming in general) to get it done. I have no idea about GUI's and I kept reading several hours and I'm finally able to design the buttons and textbox but not able to do anything with them...as far event handlers go....any help i can get it would be very much appreciated