2012-10-22 35 views
0

我對這個看似簡單的任務感到非常頭痛:發送一箇中斷信號給我的設備,如wxTerm(或任何類似的終端應用程序)。根據我的測試和設備規格,該信號必須長125ms。這種嘗試發送中斷信號有什麼問題?

它應該導致一個特定的響應,但我得到的是比預期更長的響應,並且傳輸日期是錯誤的。

如:

它應該響應08 00 81 00 00 01 07 00

它做什麼迴應08 01 0A 0C 10 40 40 07 00 7F

真正博格爾斯我的是,我已經用過之後wxTerm看我可用的COM端口(無連接或發送任何東西),我的代碼開始工作!我可以隨時發送儘可能多的休息時間,從那時起我就可以得到我的答覆。我必須重置我的電腦才能再次嘗試。

這到底是怎麼回事?

這裏是我的一個突破信號復位代碼:

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::flow_control FLOW(boost::asio::serial_port_base::flow_control::hardware); 
     boost::asio::serial_port_base::baud_rate baud_option(baud); 
     serialPort.set_option(FLOW); 
     serialPort.set_option(baud_option); 
     read_start(); 
     std::cout << SetCommBreak(serialPort.native_handle()) << std::endl;  
     std::cout << GetLastError() << std::endl; 
     boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time(); 
     boost::this_thread::sleep(boost::posix_time::millisec(125)); 
     boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();  
     std::cout << ClearCommBreak(serialPort.native_handle()) << std::endl;  
     std::cout << GetLastError() << std::endl;   
     boost::posix_time::time_duration msdiff = mst2 - mst1; 
     std::cout << msdiff.total_milliseconds() << std::endl; 
    } 

編輯:

它是隻需要看看組合框中選擇wxTerm的COM端口 - 爲了使我的代碼正常工作,不需要建立活動連接。

我猜測,有一些初始化丟失,這是在wxTerm正在爲串口組合框創建列表時完成的。

+0

這是一個瘋狂的猜測:你的「設備」需要進入適當的狀態,然後纔會根據你正在查看的規範進行響應。 'wxTerm'在中斷之前將它置於該狀態,否則你的代碼不會。 – alexis

+0

@alexis - 是和不是 - 看起來,它已經足以啓動wxTerm並轉到選擇Combo的組合框 - 無需連接任何東西!在此之後,我的代碼適用於每個設備,並且我必須重新啓動,以便重建此行爲。 – Jook

+0

那麼也許這是你的計算機的端口需要進入適當的狀態? – alexis

回答

0

在深入研究這個問題之後,我發現我必須正確設置character_size才能正常工作。

但是我沒有真正地忘記這一點,直到現在才這樣。當我的設備已處於重置後狀態時,他們會根據我的請求發送他們的數據 - 所以一切正常,我只需要指定baud_rate

因爲沒有活動流量控制是一個經常使用的默認值,我想,這裏可能會出現一些錯誤。但是,只需要設置character_size即可。然後,用期望的答案來響應中斷信號。

這是基本設置:

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::character_size CSIZE(8); 
    boost::asio::serial_port_base::baud_rate baud_option(baud); 
    serialPort.set_option(CSIZE); 
    serialPort.set_option(baud_option); 
    read_start(); 
    std::cout << SetCommBreak(serialPort.native_handle()) << std::endl;  
    std::cout << GetLastError() << std::endl; 
    boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time(); 
    boost::this_thread::sleep(boost::posix_time::millisec(125)); 
    boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();  
    std::cout << ClearCommBreak(serialPort.native_handle()) << std::endl;  
    std::cout << GetLastError() << std::endl;   
    boost::posix_time::time_duration msdiff = mst2 - mst1; 
    std::cout << msdiff.total_milliseconds() << std::endl; 
} 

然而,僅僅爲了保險起見,我現在設置了所有的串口參數。

在這裏找到的示例文件Boost Asio serial_port - need help with io幫助。