在boost :: asio应用程序的示例中,我如何重置send_break

How can I reset with send_break in this example of a boost::asio application?

本文关键字:何重置 break send asio boost 应用程序      更新时间:2023-10-16

在使用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.