1 Introduction
Asio is a cross-platform C++ library, commonly used for network programming, low-level I/O programming, etc. (low-level I/O). Its structural framework As follows:
2 Using Asio
##2.1 Download
Asio library is divided into Boost version and non-Boost version. Please go to the official website to download.2.2 Configuration
1) Using Qt 5.9.1, add the following configuration to its .pro project file: NoteASIO_STANDALONE In the non-Boost version,
INCLUDEPATH += $$PWD/../../serialport/asio-1.10.8/includeDEFINES += ASIO_STANDALONE2) If you use VS 2015, then
ASIO_STANDALONE is configured as follows Shown:
2.3 Code example
The following is a simple serial communication example. The main steps are:Create Serial port --> Configuration parameters --> Read and write data --> Enable event loop
#include <functional>#include "asio.hpp"#include <QDebug>using namespace asio;// 存储接收到的数据char kBuf[16];// 声明回调函数void PrintBuf();int main() { // 串口 COM1 io_service iosev; serial_port port(iosev, "COM1"); // 参数设置:波特率、流控、奇偶校验、停止位、数据位 port.set_option(serial_port::baud_rate(115200)); port.set_option(serial_port::flow_control(serial_port::flow_control::none)); port.set_option(serial_port::parity(serial_port::parity::none)); port.set_option(serial_port::stop_bits(serial_port::stop_bits::one)); port.set_option(serial_port::character_size(8)); // 向串口写数据 write(port, buffer("Hello Asio", 16)); // 从串口读数据(异步) port.async_read_some(buffer(kBuf),std::bind(PrintBuf)); // 开启事件循环 iosev.run(); }// 打印接收的数据void PrintBuf() { qDebug() << kBuf; }
3 DB9 serial port
When running the above program, you will find a problem: the program first sends/writesto the serial port COM1, "Hello Asio" data, and then go to to receive/read data. For a serial port, the data will not be received.
On the back of the desktop computer, the DB9 serial port pin numbers are as follows:short pin 2 and pin 3 before running the program. In this way, you can both send and receive data
4 Virtual serial port
If you use a laptop, there is generally no serial port. In this case, there are two options: First, use theUSB to serial port data cable and install the corresponding driver, then you can communicate with the device with a serial port;
Second, useVirtual Serial Port Software to create a virtual serial port, for example, Configure Virtual Serial Port Driver
Serial Port Debugging Tool, you can flexibly Debugging the serial port program
The above is the detailed content of Asio library for C++. For more information, please follow other related articles on the PHP Chinese website!

This article explores the challenges of NULL pointer dereferences in C. It argues that the problem isn't NULL itself, but its misuse. The article details best practices for preventing dereferences, including pre-dereference checks, pointer initiali

This article explains how to create newline characters in C using the \n escape sequence within printf and puts functions. It details the functionality and provides code examples demonstrating its use for line breaks in output.

This article guides beginners on choosing a C compiler. It argues that GCC, due to its ease of use, wide availability, and extensive resources, is best for beginners. However, it also compares GCC, Clang, MSVC, and TCC, highlighting their differenc

This article emphasizes the continued importance of NULL in modern C programming. Despite advancements, NULL remains crucial for explicit pointer management, preventing segmentation faults by marking the absence of a valid memory address. Best prac

This article reviews online C compilers for beginners, focusing on ease of use and debugging capabilities. OnlineGDB and Repl.it are highlighted for their user-friendly interfaces and helpful debugging tools. Other options like Programiz and Compil

This article compares online C programming platforms, highlighting differences in features like debugging tools, IDE functionality, standard compliance, and memory/execution limits. It argues that the "best" platform depends on user needs,

This article discusses efficient code copying in C IDEs. It emphasizes that copying is an IDE function, not a compiler feature, and details strategies for improved efficiency, including using IDE selection tools, code folding, search/replace, templa

This article troubleshoots missing output windows in C program compilation. It examines causes like failing to run the executable, program errors, incorrect compiler settings, background processes, and rapid program termination. Solutions involve ch


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
