Asio library for C++

炎欲天舞
Release: 2017-08-04 09:55:55
Original
2845 people have browsed it

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_STANDALONEIn the non-Boost version,


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

2) If you use VS 2015, then

ASIO_STANDALONEis 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 #include "asio.hpp"#include 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; }
Copy after login

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 toto receive/readdata. 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 portdata cable and install the corresponding driver, then you can communicate with the device with a serial port;

Second, use

Virtual Serial PortSoftware 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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!