在boost :: asio应用程序的示例中,我如何重置send_break
How can I reset with send_break in this example of a boost::asio application?
在使用boost :: asio创建应用程序时,我为我的目的调整了一个示例。
但是,在使其正常工作之后,我现在正试图使其正常工作。为此,我需要以某种方式重置串行设备。在类似的应用程序中,这是通过发送break
信号完成的。
出于某种原因,我似乎不能没有例外。
我正在使用void send_break()
函数,也许这是问题,因为它似乎总是会丢弃错误。
这是启动代码:
/// Send a break sequence to the serial port.
/**
* This function causes a break sequence of platform-specific duration to be
* sent out the serial port.
*
* @throws boost::system::system_error Thrown on failure.
*/
void send_break()
{
boost::system::error_code ec;
this->get_service().send_break(this->get_implementation(), ec);
boost::asio::detail::throw_error(ec, "send_break");
}
/// Send a break sequence to the serial port.
/**
* This function causes a break sequence of platform-specific duration to be
* sent out the serial port.
*
* @param ec Set to indicate what error occurred, if any.
*/
boost::system::error_code send_break(boost::system::error_code& ec)
{
return this->get_service().send_break(this->get_implementation(), ec);
}
这是我试图从:
调用函数的代码class minicom_client
{
public:
minicom_client(boost::asio::io_service& io_service, unsigned int baud, const string& device)
: active_(true),
io_service_(io_service),
serialPort(io_service, device)
{
if (!serialPort.is_open())
{
cerr << "Failed to open serial portn";
return;
}
boost::asio::serial_port_base::baud_rate baud_option(baud);
serialPort.set_option(baud_option); // set the baud rate after the port has been opened
serialPort.send_break();
read_start();
}
编辑:
玩了一段时间后,我发现我得到的错误代码是boost::asio::error::operation_not_supported;
- 但是当这是函数中的构建时,这怎么可能?!
来自 *win_iocp_serial_port_service.hpp *:
// Send a break sequence to the serial port.
boost::system::error_code send_break(implementation_type&,
boost::system::error_code& ec)
{
ec = boost::asio::error::operation_not_supported;
return ec;
}
现在我真的迷路了。
基本上,您需要打开COM端口并通过Windows API发送休息。只要您正确包含Windows.h,以下代码应工作。
#include <windows.h>
TCHAR pcCommPort[512];
_tcscpy(pcCommPort, "COM1"); // Or COM2, COM3, ...
// Open a handle to the specified com port.
handleToComPort = CreateFile(pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
FILE_ATTRIBUTE_NORMAL, // not overlapped I/O
NULL); // hTemplate must be NULL for comm devices
::SetCommBreak(handleToComPort);
::Sleep(125); // Sleep 125ms to ensure a good break
::ClearCommBreak(handleToComPort);
::CloseHandle(handleToComPort); // Close the COM port only if you need to.
相关文章:
- QTableView:endMoveRows在模型中重置水平页眉大小
- 更改"child's parent's style sheet"时如何将孩子的样式表重置为 Qt 默认样式表?
- 切换大小写后如何阻止变量重置?
- 当客户端在 write() 期间终止连接时,由对等套接字错误重置连接
- 移动后是否需要重置标准::列表?
- 在读取文件后重置句柄
- C++变量在调用 x64 程序集函数后重置为 0
- Adafruit 羽毛RFM69HCW在使用过程中会冻结,需要硬重置
- 变量始终在函数中重置为默认值
- 为什么我的 LogError 函数会重置 GetLastError?
- 无法重置 std::shared_ptr 标准::设置<>中的对象...为什么?
- C++上的手动重置事件(来自 C#)实现:如何避免争用条件
- 如何在IQualProp::get_AvgFrameRate中重置帧速率?
- 奇怪的UC3 用户页面 NVRAM 使用后重置行为
- 如何在接触时重置我的角色位置?
- C++ - 重置字符串流不会重置获取位置或清除标志吗?
- 重置 std::shared_ptr 会导致重置其删除器吗?
- 为什么重置引用时没有收到错误?
- DirectX 9 在重置调用之间保留状态
- 如果输入类型与目标类型不同,"cin"变量是否重置为某个默认值?