Arduino and C# serial communication

In this short article will be presented how you can set up serial communication between Arduino Nano or Uno and a C# application. Many articles are written about this subject all around the internet, but I have not found any of them that could help to create a “real world” and usable application, so I am going to show you my way of dealing with this task.

Lets see what is a serial communication, serial communication port or interface.

Serial communication is a meaning of data transfer between two physical devices, like PC to PC, PC to uC. One of the most popular serial communication interface is USB (Universal Serial Bus), that is the one we are going to use.

 

Working principles

The whole communication is based on Visual Studios Serial Port library and a timer. The Serial port library takes care of all low level addressing and hardware management. In this project the default parameters are used, only the COM port name is changed to our desired port, because that is different for every USB port.

For setting up a serial (COM) communication between two devices the following steps should be used:

 

Scan the available ports

Select one of the available ports and pass the desired parameters to the corresponding variables of SerialPort component

Create the connection to the external device

Send and receive data

Until this point the whole code was straight, nothing special was made. But here comes the little tricky part, because if you mess up your application is going to use a lot of CPU time, actually for doing nothing useful – and your application is going to lag. In order to avoid manipulating and managing multiple threads, and let the whole thread management be solved by the included libraries, I am going to use a timer. A timer actually has only callable method (function) which is Tick. Every time a timer makes a tick, a function is called and a message is sent to the microcontroller. The time interval between two ticks can be set correspondingly to our preference.

And what about getting a response from the microcontroller? When the response is going to arrive? Actually, it depends on how fast the microcontroller can complete the given task. It can take from a few microseconds to minutes or even hours. So the program should always be reading up the serial port for checking if there is a new message? No, that is a bad idea because that eats up a lot of resources. The SerialPort component has a nice so called Event, which is called DataReceived(object sender, SerialDataReceivedEventArgs e). This small piece of code actually starts on a new thread, and read up and save the data received from the microcontroller.

#IMPORTANT: Safe thread handling

In many cases the code from above works fine, but depending on your systems configuration this piece of code may cause an error, because the dataFromArduino variable could be handled by a method which is running on an other thread. It also caused me headaches, because its a bit tricky, once works fine and once not. A helper method can fix the problem and than its going to be safe.

You need to paste this line in your code where the helper method is going to be placed.

The task of this delegation is to enable the asynchronous calls for setting the text into a textbox or label.

Here is the method (function) that should be used and changed accordingly to your needs:

Where:

textbox2 – should be changed to your label or textbox name

 

Closing the connection

This is not a necessary step, but before closing the application and pulling out the microcontroller from the PC it is nice to close the connection. If not maybe the port is stuck and you need to restart your pc.

Arduino code structure

The microcontroller side of this communication is just a simple loop which is always checking if there is some data (a character actually) on the serial port. If there is, the whole line is read out until the newline character (which is by default \n) is found, then an if statement takes care about what the microcontroller should do, with the simple strcmp() function and return or not return data. In the example if a commandFromPC string is received, then it returns it to the PC – if not, nothing happens.

Just imagine the following scenario: There is one temperature sensor and a servo motor connected to the Arduino and you want to control them with a computer. You request the temperature from the device and after that you make some decisions, a few minutes after when you wish to move the servo you just send out the command but you dont need any kind of response.

Now lets see the source code for Arduino

Conclusion

So understanding how this connection works, or using this C# example application can bootstrap your projects.

 

 

Resources:

Visual Studio project for the example software