Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c++ - Boost Asio serial_port - need help with io

So I've been trying to learn the boost::asio stuff to communicate to a serial device using RS232. The documementation is sparse and the examples are non-existent. Can't figure out exactly how to communicate with the device. The device can't send data so all I need to do is write, but other projects require actual back and forth communication so help with that would be appreciated. What code I have so far follows.

#include <boost/asio/serial_port.hpp>
using namespace::boost::asio;

int main()
{
    io_service io;
    serial_port port( io, "COM3" );
    port.set_option( serial_port_base::baud_rate( 19200 ) );

    unsigned char commands[4] = { 1, 128, 240, 0 };

    // write the commands to the device

    return 0;
}

In short: need help with the io part of the serial_port.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In addition to the baud rate, you may also need to set other options like: character_size, flow_control, parity and stop_bits. To write your data to the serial port you can do the following:

boost::asio::write(port, boost::asio::buffer(commands, 4));

The libraries acceptance of buffer types is very flexible and you may want to read further on that topic here: Buffers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...