jingweimo的个人博客分享 http://blog.sciencenet.cn/u/jingweimo

博文

[转载]Serial Communication with an Arduino using C++ on Windows

已有 1800 次阅读 2019-2-22 10:58 |系统分类:科研笔记|文章来源:转载

 

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.

TL;DR

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]

Outcome

Reading data from arduino

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”.

Outcome

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

https://social.msdn.microsoft.com/Forums/en-US/35ed4698-c989-4eb5-808c-a39f14d255a4/serial-comm-with-arduino?forum=vcgeneral

 



https://wap.sciencenet.cn/blog-578676-1163550.html

上一篇:Create flashing LED indicators with QtSvgRenderer
下一篇:[转载]Serial Port Programming using Win32 API
收藏 IP: 68.83.204.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-14 04:31

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部