||
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.
Add these files to your C++ project.
If you’re willing to Serial data from arduino then your main file should contain this type of code.
Reading data from arduino
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”.
Input and output altogether
The library can be found here. Hope my post was concise and to the point. Thanks for reading.
Sources:
https://github.com/manashmndl/SerialPort
https://blog.manash.me/serial-communication-with-an-arduino-using-c-on-windows-d08710186498
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-9 04:45
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社