C ++ Asio library, asio

Source: Internet
Author: User

C ++ Asio library, asio

1 Overview

Asio is a cross-platform C ++ library. It is often used in network programming and underlying I/O programming (low-level I/O). Its structure is as follows:

 

2. Use Asio

Download 2.1

Asio library is divided into Boost version and non-Boost version, the latter is: http://think-async.com/, after the download is complete, directly extract to the appropriate location.

2.2 Configuration

1) Use Qt 5.9.1 and add the following configuration to the. pro project file:ASIO_STANDALONEThe non-Boost version must contain

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

2) When VS 2015 is usedASIO_STANDALONEThe configuration is as follows:

2.3 sample code

The following is a simple serial communication example. The main steps are: Create a serial port --> Configure parameters --> Read and Write Data --> enable the event Loop

# Include <functional> # include "asio. hpp "# include <QDebug> using namespace asio; // store the received data char kBuf [16]; // declare the callback function void PrintBuf (); int main () {// serial port COM1 io_service iosev; serial_port port (iosev, "COM1"); // parameter settings: baud rate, traffic control, parity check, stop bit, data bit port. set_option (serial_port: baud_rate (115200); port. set_option (serial_port: flow_control: none); port. set_option (serial_port: parity: none); port. set_option (serial_port: stop_bits: one); port. set_option (serial_port: character_size (8); // write data to the serial port (port, buffer ("Hello Asio", 16 )); // read data from the serial port (asynchronous) port. async_read_some (buffer (kBuf), std: bind (PrintBuf); // enable the event loop iosev. run () ;}// print the received data void PrintBuf () {qDebug () <kBuf ;}

 

3 DB9 serial port

When running the above program, you will find a problem: the program first sends/writes "Hello Asio" data to the serial port COM1, and then collects/reads the data, for a serial port, data is not received.

After the desktop, the serial port number of DB9 is as follows:

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

To run the above program successfully, you can connect the two and three feet before running the program. In this way, you can send data or collect data.

 

4. virtual serial port

If you use a notebook, there is generally no serial port. There are two solutions:

First, you can use the USB to serial port data cable and install the corresponding driver to communicate with the device with serial ports;

Second, use Virtual Serial Port software to create Virtual Serial ports, for example, Configure Virtual Serial Port Driver

CooperationSerial Port debugging toolTo debug the serial program flexibly.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.