search
Asio library for C++Aug 04, 2017 am 09:55 AM

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: Note

ASIO_STANDALONE In the non-Boost version,


INCLUDEPATH += $$PWD/../../serialport/asio-1.10.8/includeDEFINES += ASIO_STANDALONE

2) 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/writes

to 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:

Among them, 2 --> RxD, is the pin for receiving data; 3 --> TxD, is the pin for sending data.

In order for the above program to run successfully, you can

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 the

USB to serial port data cable and install the corresponding driver, then you can communicate with the device with a serial port;

Second, use

Virtual Serial Port Software to create a virtual serial port, for example, Configure Virtual Serial Port Driver

and with

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the alternatives to NULL in C languageWhat are the alternatives to NULL in C languageMar 03, 2025 pm 05:37 PM

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

How to add next-level C compilerHow to add next-level C compilerMar 03, 2025 pm 05:44 PM

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.

Which C language compiler is better?Which C language compiler is better?Mar 03, 2025 pm 05:39 PM

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

Is NULL still important in modern programming in C language?Is NULL still important in modern programming in C language?Mar 03, 2025 pm 05:35 PM

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

What are the web versions of C language compilers?What are the web versions of C language compilers?Mar 03, 2025 pm 05:42 PM

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

C language online programming website C language compiler official website summaryC language online programming website C language compiler official website summaryMar 03, 2025 pm 05:41 PM

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,

Method of copying code by C language compilerMethod of copying code by C language compilerMar 03, 2025 pm 05:43 PM

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

How to solve the problem of not popping up the output window by the C language compilerHow to solve the problem of not popping up the output window by the C language compilerMar 03, 2025 pm 05:40 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

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

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

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

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),