Serial Communication with an Arduino using C++ on Windows
If you’re acquainted with arduino, at some point you needed to establish serial communication between a pc and the arduino. There are a lot of tutorials available online for serving the purpose. Still I want to show you how to communicate with your arduino using C++ programming language.
There are some source codes available online which requires Visual C++. I’ve modified an existing library and made it worked on plain C++. So you can communicate with the arduino right out of your code::blocks or similar C++ IDE. And yeah, no library dependency is needed.
If you’re looking for a quick way to establish serial communication without understanding the whole library source. Then you’ve come to the right place.
‘SerialPort.h’ Class Header File
‘SerialPort.cpp’ Class Source File
Add these files to your C++ project.
Reading data from arduino:
If you’re willing to Serial data from arduino then your main file should contain this type of code.
main.cpp [reading data from arduino]
write_data.ino [writing data to serialport]
Writing Data to Arduino:
main.cpp [Writing data to arduino]
read_data.ino [reading data from C++ program]
Here it is not possible to share one serial port for two concurrent programs. For finding out if my C++ program works or not, I have used the on board arduino led.
I am going to send a specific string to turn on and off the led of the arduino. If the led of the arduino is turned on, it will reply by sending a string “led is on” otherwise it will turn off the led and will reply “led is off”.
The library can be found here. Hope my post was concise and to the point. Thanks for reading.