本文介绍了我怎样才能建立在这种环境中deadline_timer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点在图书馆,我要缠在一起的结构丢失。我需要帮助一些定时器indtroduce到这个结构。

I am a bit lost in a construct of libraries, which I have to tangle together. I need help to indtroduce some timers into this construct.

我有以下内容:


  • com.cpp 的有和包括的 com.hpp

  • com.hpp 的其中包括的 host.h 的,需要提升包括并定义一个类的 comClient

  • host.c 的与包括的 host.h

  • wrapper.cpp 的与包括的 com.hpp 的和一些必要的升压包括

  • com.cpp which has main and includes com.hpp
  • com.hpp which includes a host.h and needed boost includes and defines a class comClient
  • host.c with included host.h
  • wrapper.cpp with included com.hpp and some needed boost includes

现在,我的 com.cpp 的是创建的 comClient 的,并使用它的COM端口上的非同步通信。使用提高:: ASIO :: serial_port 的boost ::支持ASIO :: io_service对象

Now, my com.cpp is creating a comClient and uses it for asynch communication on the com-port. Using boost::asio::serial_port and boost::asio::io_service.

我需要一些定时器的工作,为了赶时,需要一揽子贷款太长时间来传输。

I need to work with some timers, in order to catch when a paket needed too long to transmit.

在创建实例的 comClient 的,该一揽子贷款定时器应当被初始化。

When creating an instance of comClient, the paket-timer should be initialised.

comClient 的,我称之为的 comClient 的的私人处理私有函数使用 asynch_read_some ,那么这个处理程序调用的函数的 host.c 的,它调用到的 wrapper.cpp 的重启定时器功能。

Using asynch_read_some in a private function of comClient, I call a private handler of comClient, then this handler calls a function of host.c, which calls to the wrapper.cpp a function to restart the timer.

这是给init定时器功能:

This is the function to init the timer:

//wrapper.cpp
void    IniPacketTimer(void *pCHandle){

    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::milliseconds(25));
    t.async_wait(&hostOnTimeout(pCHandle));
    io.run();
}

这将是短命令链:

//comClient.cpp
main{
  comClient cc();
}
//comClient.hpp
class comClient(boost::asio::io_service& io_service){
  comClient(){
   hostInit();
   aread();
  }
  private:
    aread( call aread_done)
    areaddone(call hostNewData())
}

//host.c
hostInit(){
   IniPacketTimer()
}
hostNewData(){
  resetTimer
}

//wrapper.cpp
resetTimer(){
  t.expires_from_now
}

问题:

我怎么能提供一个异步定时器,不影响读取/写入我的串口操作非同步,但在最后期限被击中触发功能的执行?

How can I provide an asynchronous timer, which does not affect the asynch read/write operations on my serial port, but triggers execution of a function when the deadline is hit?

我应该使用现有的 io_service对象还是确定,如果我只是创建另一个?

Should I use the already existing io_service or is it ok, if I just create another?

为什么我得到一个错误C2102'和;'预计L-价值我行 t.async_wait

Why do I get an error C2102 '&' expects L-Value for my line t.async_wait?

推荐答案

您的问题是不明确,因为你没有真正张贴code这是相当难以猜测你的问题是什么。结果
尤其是你的线程是不明确但对ASIO非常重要的。

You problem is not clear and since you don't post real code it is quite hard to guess what your problem is.
Especially your threading is not clear but for asio very important.

下面是将编译,但无法运行的例子。我希望它让你对如何进行的提示。结果
它就会打开一个串行端口和一个定时器。每当计时器到期,将开始一个新的。这是code的一个简化版本,我用了前一段时间,所以也许它会帮助你。

Below is an example that will compile but not run. I hope it gives you an hint on how to proceed.
It will open a serial port and a timer. Whenever the timer expires it will start a new one. It is a stripped version of code I used some time ago so maybe it will help you.

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


class SerialCommunication
{
    public:
    SerialCommunication(boost::asio::io_service& io_service, const std::string& serialPort)
        : m_io_service(io_service)
        , m_serialPort(m_io_service)
        , m_timeoutTimer(m_io_service, boost::posix_time::milliseconds(5))
    {
        configureSerialPort(serialPort);
    }

    void configureSerialPort(const std::string& serialPort)
    {
        if(m_serialPort.is_open())
        {
            m_serialPort.close();
            m_timeoutTimer.cancel();
        }
        boost::system::error_code ec;
        m_serialPort.open(serialPort, ec);
        if(m_serialPort.is_open())
        {
            // start Timer
            m_timeoutTimer.async_wait(boost::bind(&SerialCommunication::TimerExpired, this, _1));
            header_sync();
        }
    }

    void header_sync()
    {
        m_serialPort.async_read_some(boost::asio::buffer(&m_header.back(), 1),
            boost::bind(&SerialCommunication::header_sync_complete, this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
    }

    void header_sync_complete(
        const boost::system::error_code& error, size_t bytes_transferred)
    {
        // stripped
        read_payload(&m_payload[0], 0);
    }


    void read_payload(uint8_t* buffer, uint8_t length)
    {
        m_serialPort.async_read_some(boost::asio::buffer(buffer, length),
            boost::bind(&SerialCommunication::payload_read_complete, this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
    }

    void payload_read_complete(
        const boost::system::error_code& error, size_t bytes_transferred)
    {
        // stripped
        // timer cancel and reset
        m_timeoutTimer.cancel();
        m_timeoutTimer.expires_at(boost::posix_time::microsec_clock::local_time() +
                            boost::posix_time::milliseconds(5));
        m_timeoutTimer.async_wait(boost::bind(&SerialCommunication::TimerExpired, this, _1));
        memset(&m_header[0], 0, 3);
        header_sync();
    }

    void TimerExpired(const boost::system::error_code& e)
    {
        m_timeoutTimer.expires_at(m_timeoutTimer.expires_at() + boost::posix_time::milliseconds(5));
        m_timeoutTimer.async_wait(boost::bind(&SerialCommunication::TimerExpired, this, _1));
    }

    boost::asio::io_service& m_io_service;
    boost::asio::deadline_timer m_timeoutTimer;
    boost::asio::serial_port m_serialPort;
    std::vector<uint8_t> m_header;
    std::vector<uint8_t> m_payload;
};



int main()
{
    boost::asio::io_service io_service;
    SerialCommunication cc(io_service, "/dev/ttyS0");

    io_service.run();
    return 0;
}

这篇关于我怎样才能建立在这种环境中deadline_timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:27