C + + 's Asio library

Source: Internet
Author: User

1 Introduction

Asio is a cross-platform C + + library that is commonly used for network programming, low-level I/O programming, and so on (low-level I/O), with the following structural framework:

2 Using Asio

2.1 Downloads

Asio can be divided into Boost version and Non-boost version, the latter is: http://think-async.com/

After the download is complete, unzip directly to the appropriate location.

2.2 Configuration

1) using Qt 5.9.1, in its. Pro project file, add the following configuration: note Asio_standalone must be in the Non-boost version with

+ = $ $PWD/.. /.. /serialport/asio-1.10. 8/includedefines+ = Asio_standalone

2) Using VS 2015, the asio_standalone configuration is as follows:

2.3 Code Examples

Here is a simple serial port communication example, the main steps are: create a serial port -- configuration Parameters -- Read and write Data --turn on the event loop

#include <functional>#include"asio.hpp"#include<QDebug>using namespaceASIO;//Store the received dataCharkbuf[ -];//declaring a callback functionvoidprintbuf ();intMain () {//Serial COM1Io_service Iosev; Serial_port Port (Iosev,"COM1"); //parameter settings: Baud rate, flow control, parity check, stop bit, data bitPort.set_option (Serial_port::baud_rate (115200));    Port.set_option (Serial_port::flow_control (Serial_port::flow_control::none));    Port.set_option (serial_port::p arity (serial_port::p arity::none));    Port.set_option (Serial_port::stop_bits (Serial_port::stop_bits::one)); Port.set_option (Serial_port::character_size (8)); //write data to the serial portWrite (port, buffer ("Hello Asio", -)); //read data from the serial port (asynchronous)port.async_read_some (Buffer (KBUF), Std::bind (PRINTBUF)); //Turn on event loopsIosev.run ();}//print the Received datavoidPrintbuf () {qdebug ()<<kbuf;}

3 DB9 Serial Port

When running the above program, you will find a problem: The program first to the serial port COM1, Send/write "Hello Asio" data, and then to collect/read out the data, which for a serial port, is not to receive data.

The DB9 serial pin is numbered as follows:

Among them, 2---RxD, is the pin that receives the data, 3--and TxD, is the pin that sends the data.

For the above program to run successfully, you can get 2 feet and 3 feet short before running the program , so that you can either send data or collect data

4 Virtual serial Port

If you use a notebook, there is generally no serial port, then there are two options:

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

Second, use the virtual serial port software to create a virtual serial port, for example, Configure virtual Serial port Driver

Then with the serial debugging tool , you can flexibly debug the serial port program

C + + 's Asio library

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.