c++ - 在这个 boost::asio 应用程序示例中,如何使用 send_break 进行重置?

标签 c++ boost serial-port boost-asio

在使用 boost::asio 创建应用程序时,我根据自己的目的改编了一个示例。

然而,在让它工作之后,我现在正在努力让它工作得更好。为此,我需要以某种方式重置我的串行设备。在类似的应用程序中,这是通过发送 break 信号来完成的。

出于某种原因,我似乎无法在没有异常的情况下执行此操作。

我正在使用 void send_break() 函数,也许这就是问题所在,因为它似乎总是会抛出错误。

这是 boost 代码:

  /// 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 port\n";
                        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.

关于c++ - 在这个 boost::asio 应用程序示例中,如何使用 send_break 进行重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971601/

相关文章:

c++ - 将 boost::asio::ip::address_v4 隐藏为字符串

c++ - Boost ICL : Are some combinations of interval types and functions not implemented?中函数 "contains"的基本使用

c# - 串行数据的二进制通信协议(protocol)解析器设计

c++ - 将关键点转换为 mat 或将它们保存到文本文件 opencv

c++ - cvSplit 中的 OpenCV 错误 : Assertion failed (i < src. channel ())

C++ vector 语法错误

java - 如何使用Java读取原始串行数据?

c++ - 完美转发和 std::forward<T> 的使用

c++ - boost 上下文库

c - 使用 C 从路由器转储闪存