boost C++ read from serial port with timeout example

from: http://www.ridgesolutions.ie/index.php/2012/12/13/boost-c-read-from-serial-port-with-timeout-example/

If you are doing any serial port communications these days in C++ and would like your code to be portable, then you are probably using boost’s asio::serial_port class.

One complication with using serial_port (and boost::asio more generally) is that it doesn’t provide a direct facility to allow synchronous blocking reads to time-out and return if no data arrives within a specified time period. Here is a little example that tries to read a character from COM3 (on windows..)

#include <boost/asio/serial_port.hpp> 
#include <boost/asio.hpp> 

using namespace boost;

char read_char() {
    asio::io_service io;
    asio::serial_port port(io);

    port.open("COM3");
    port.set_option(asio::serial_port_base::baud_rate(115200));

    char c;

    // Read 1 character into c, this will block
    // forever if no character arrives.
    asio::read(port, asio::buffer(&c,1));

    port.close();

    return c;
}

In this example read() will block forever if no data arrives to the serial port, this is not always what you want, especially when dealing with possibly noisy or unreliable rs232 communication.

In order to take advantage of read time-outs you have to issue asynchronous reads and incorporate a deadline_timer which will cancel the read after a specified time, i.e. if the read hasn’t received the data it was expecting before the deadline_timer expires, then it will be cancelled.

Using asynchronous IO in boost is a bit involved and it can be quite quite messy, so I have written small class called blocking_reader which will block while trying to read a single character, and will time out if a character hasn’t been received in a specified number of milliseconds. It can be used like this:

#include <string>
#include <boost/asio/serial_port.hpp> 
#include <boost/asio.hpp> 
#include "blocking_reader.h"

using namespace boost;

std::string read_response() {

    asio::io_service io;
    asio::serial_port port(io);

    port.open("COM3");
    port.set_option(asio::serial_port_base::baud_rate(115200));

    // A blocking reader for this port that 
    // will time out a read after 500 milliseconds.
    blocking_reader reader(port, 500);

    char c;

    std::string rsp;

    // read from the serial port until we get a
    // \n or until a read times-out (500ms)
    while (reader.read_char(c) && c != '\n') {
        rsp += c;
    }

    if (c != '\n') {
        // it must have timed out.
        throw std::exception("Read timed out!");
    }

    return rsp;
}

The above code isn’t the most sensible or efficient but it shows the use of blocking_reader, which in this case times out reads after 500ms.

You open the serial_port as normal and then pass it to blocking_reader’s constructor along with a timeout value. You then use blocking_reader.read_char() to read a single character. If the read times out then read_char() will return false (otherwise it will return true!)

The code for blocking_reader can be downloaded from here, I have also included it below:

//
// blocking_reader.h - a class that provides basic support for  
// blocking & time-outable single character reads from 
// boost::asio::serial_port.
//
// use like this:
// 
//  blocking_reader reader(port, 500);
//
//  char c;
//
//  if (!reader.read_char(c))
//      return false;
//
// Kevin Godden, www.ridgesolutions.ie
//

#include <boost/asio/serial_port.hpp> 
#include <boost/bind.hpp>

class blocking_reader
{
    boost::asio::serial_port& port;
    size_t timeout;
    char c;
    boost::asio::deadline_timer timer;
    bool read_error;

    // Called when an async read completes or has been cancelled
    void read_complete(const boost::system::error_code& error,
                        size_t bytes_transferred) {     

        read_error = (error || bytes_transferred == 0);

        // Read has finished, so cancel the
        // timer.
        timer.cancel();
    }

    // Called when the timer's deadline expires.
    void time_out(const boost::system::error_code& error) {

        // Was the timeout was cancelled?
        if (error) {
            // yes
            return;
        }

        // no, we have timed out, so kill
        // the read operation
        // The read callback will be called
        // with an error
        port.cancel();
    }

public:

    // Constructs a blocking reader, pass in an open serial_port and
    // a timeout in milliseconds.
    blocking_reader(boost::asio::serial_port& port, size_t timeout) :
                                                port(port), timeout(timeout),
                                                timer(port.get_io_service()),
                                                read_error(true) {

    }

    // Reads a character or times out
    // returns false if the read times out
    bool read_char(char& val) {

        val = c = '\0';

        // After a timeout & cancel it seems we need
        // to do a reset for subsequent reads to work.
        port.get_io_service().reset();

        // Asynchronously read 1 character.
        boost::asio::async_read(port, boost::asio::buffer(&c, 1), 
                boost::bind(&blocking_reader::read_complete, 
                        this, 
                        boost::asio::placeholders::error, 
                        boost::asio::placeholders::bytes_transferred)); 

        // Setup a deadline time to implement our timeout.
        timer.expires_from_now(boost::posix_time::milliseconds(timeout));
        timer.async_wait(boost::bind(&blocking_reader::time_out,
                                this, boost::asio::placeholders::error));

        // This will block until a character is read
        // or until the it is cancelled.
        port.get_io_service().run();

        if (!read_error)
            val = c;

        return !read_error;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值