【发布时间】:2018-07-01 07:16:22
【问题描述】:
我正在通过串行端口实现一些基本通信。
根据协议,我必须在收到请求后 225 毫秒内回复。最大封装大小为 256B。
因此,当我收到请求时,我创建响应 [header, lenght, payload, crc16](总共 256 B),我需要平均 30 - 40 毫秒。然后,当我将该响应(字节数组)放入asio::async_write 时,就会出现实际问题。该函数平均需要大约 240 毫秒来处理该字节数组。
除了我发送最大长度的包裹外,一切正常。它需要 240ms (asio::async_write) + 40ms(封装组装) 大约 280 ~ 300 ms。
端口:9600 波特,长度 8,一位停止位
知道如何加快速度吗?
void Example::do_write()
{
if (pimpl_->WriteBuffer == nullptr)
{
boost::lock_guard<boost::mutex> l(pimpl_->WriteQueueMutex);
pimpl_->WriteBufferSize = pimpl_->WriteQueue.size();
pimpl_->WriteBuffer.reset(new byte[pimpl_->WriteQueue.size()]);
std::move(pimpl_->WriteQueue.begin(), pimpl_->WriteQueue.end(), pimpl_->WriteBuffer.get());
pimpl_->WriteQueue.clear();
begin = boost::chrono::steady_clock::now();
async_write(pimpl_->Port, asio::buffer(pimpl_->WriteBuffer.get(), pimpl_->WriteBufferSize), boost::bind(&Example::write_end, this, asio::placeholders::error));
}
}
void Example::write_end(const system::error_code& error)
{
if (!error)
{
boost::lock_guard<boost::mutex> l(pimpl_->WriteQueueMutex);
if (pimpl_->WriteQueue.empty())
{
pimpl_->WriteBuffer.reset();
pimpl_->WriteBufferSize = 0;
end = boost::chrono::steady_clock::now();
OutputDebugString(string("\nWRITE TIME: " + to_string(boost::chrono::duration_cast<boost::chrono::milliseconds>(end - begin).count()) + "\n").c_str());
return;
}
pimpl_->WriteBufferSize = pimpl_->WriteQueue.size();
pimpl_->WriteBuffer.reset(new byte[pimpl_->WriteQueue.size()]);
std::move(pimpl_->WriteQueue.begin(), pimpl_->WriteQueue.end(), pimpl_->WriteBuffer.get());
pimpl_->WriteQueue.clear();
async_write(pimpl_->Port, asio::buffer(pimpl_->WriteBuffer.get(), pimpl_->WriteBufferSize), boost::bind(&Example::write_end, this, asio::placeholders::error));
}
else
{
set_error_status(true);
do_close();
}
}
【问题讨论】:
-
你的代码在哪里?你如何衡量给定的时间?
-
您的互斥锁不适合保护写入缓冲区,因为异步操作不在互斥锁下运行
标签: c++ boost serial-port boost-asio