2012-10-19 22 views
0

在使用boost :: asio創建應用程序時,我爲我的目的調整了一個示例。如何在boost :: asio應用程序的這個示例中使用send_break重置?

但是,使其工作後,我現在試圖讓它工作得更好。爲了這樣做,我需要以某種方式重置我的串行設備。在類似的應用中,這是通過發送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 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; 
    } 

現在我真的失去了這裏。

+0

是的,這將清除它幾乎 - 然而,爲什麼它是這樣實現的,我不明白這一點O_O你有什麼想法,我怎麼可以創建一個破葛,它被指定爲*的RX輸入非空閒狀態(0v)超過125ms *。 – Jook

+0

@llonesmiz - 你爲什麼刪除你的答案?你基本上解決了我的問題,通過指向我的一些正確的來源。我必須使用SetCommBreak | Sleep | ClearCommBreak和我的串行端口的正確設置,現在我的設備重置工作 - 所以謝謝!如果你應該取消刪除你的答案,你會得到一個接受。 – Jook

回答

1

基本上,您需要打開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. 
相關問題