2017-06-22 30 views
1

我想用gtkmm编写一个简单的应用程序,它将显示从串行端口接收到的值。我正在使用这里找到的AsyncSerial类 - https://github.com/fedetft/serial-port。这些类在命令行上都适合我,但我无法让它们在gtkmm中更新标签。当我调用函数start_serial_read()时,lblMass接收到一个空字符串,并且没有输出。如果我在函数内停止了程序并逐步完成,scaleReading被分配了正确的值,并且输出了读数。任何想法我失踪?我尝试使用glib :: sleep(50)给时间来读取数据,但没有运气。谢谢。GTKMM和boost.asio

#include "FrmMain.h" 
#include "AsyncSerial.h" 
#include "BufferedAsyncSerial.h" 
#include <iostream> 
#include <boost/thread.hpp> 
#include <string> 
#include <gtkmm.h> 

using namespace std; 
using namespace Gtk; 


FrmMain::FrmMain() 
     : btnOk("OK"), 
     btnCancel("Cancel"), 
     labelSentStatus(""), 
     lblMass("0.0"), 
     timeout_value(1500), // 1500 ms = 1.5 seconds 
     vBox1(Gtk::ORIENTATION_VERTICAL), 
     hBox1(Gtk::ORIENTATION_HORIZONTAL), 
     hBox2(Gtk::ORIENTATION_HORIZONTAL), 
     hBox3(Gtk::ORIENTATION_HORIZONTAL), 
     hBox4(Gtk::ORIENTATION_HORIZONTAL) 
{  
     set_title("Additives"); 
     set_border_width(10); 

     hBox1.pack_start(lblMass, Gtk::PACK_SHRINK); //pack mass reading into top box 
     vBox1.pack_start(hBox1, Gtk::PACK_SHRINK);  //pack top box  
     vBox1.pack_start(hBox2, Gtk::PACK_SHRINK);  //pack empty space 
     hBox3.pack_start(btnOk, Gtk::PACK_SHRINK);  //pack OK button into hBox3 
     hBox3.pack_start(btnCancel, Gtk::PACK_SHRINK);  //pack cancel button into hBox3 
     vBox1.pack_start(hBox3, Gtk::PACK_SHRINK);  //pack hbox3 
     vBox1.pack_start(hBox4, Gtk::PACK_SHRINK);  //pack hbox3 
     add(vBox1); 

     btnOk.signal_clicked().connect(sigc::mem_fun(*this, &FrmMain::on_ok_button_clicked)); 
    btnCancel.signal_clicked().connect(sigc::mem_fun(*this, &FrmMain::on_cancel_button_clicked)); 
     sigc::slot<bool> timeoutSlot = sigc::mem_fun(*this, &FrmMain::start_serial_read); 
     sigc::connection conn = Glib::signal_timeout().connect(timeoutSlot,timeout_value); 
     show_all_children(); 
} 

void FrmMain::on_ok_button_clicked(){ 
    labelSentStatus.set_text("Sent"); 
} 

void FrmMain::on_cancel_button_clicked(){ 
    labelSentStatus.set_text(""); 
} 

bool FrmMain::start_serial_read() 
{ 
    try { 
     BufferedAsyncSerial serial("/dev/ttyUSB0",4800); 
     { 
      //Return immediately. String is written *after* the function returns, 
      //in a separate thread. 
      //serial.writeString("Hello world\r\n"); 
      string scaleReading = serial.readStringUntil("\r");   
      lblMass.set_text(scaleReading); 
      cout<<serial.readStringUntil("\r")<<endl;   
      serial.close(); 
      return true; 
     } 
    } catch(boost::system::system_error& e) 
    { 
     cout<<"Error: "<<e.what()<<endl; 
     return true; 
    } 
} 

回答

0

我通过增加timeout_value至2秒(timeout_value(2000)),并使用基于GTK ::睡眠()函数信号处理程序中给予更多的时间用于串行端口来获得一个固定的问题读。我以前试过的最多的是timeout_value(1500)和sleep(0.5),结果太短了一段时间。这里是更新的代码:

/* 
* FrmMain.cpp 
* 
* Created on: Jun 17, 2017 
*  Author: tim 
*/ 

#include "FrmMain.h" 
#include "AsyncSerial.h" 
#include "BufferedAsyncSerial.h" 
#include <iostream> 
#include <boost/thread.hpp> 
#include <string> 
#include <gtkmm.h> 
#include <typeinfo> 
using namespace std; 
using namespace Gtk; 


FrmMain::FrmMain() 
     : btnOk("OK"), 
     btnCancel("Cancel"), 
     labelSentStatus(""), 
     lblMass("0.0"), 
     timeout_value(2000), // 1500 ms = 1.5 seconds 
     vBox1(Gtk::ORIENTATION_VERTICAL), 
     hBox1(Gtk::ORIENTATION_HORIZONTAL), 
     hBox2(Gtk::ORIENTATION_HORIZONTAL), 
     hBox3(Gtk::ORIENTATION_HORIZONTAL), 
     hBox4(Gtk::ORIENTATION_HORIZONTAL) 

{  
     set_title("Additives"); 
     set_border_width(10); 
     hBox1.pack_start(lblMass, Gtk::PACK_SHRINK); //pack mass reading into top box 
     vBox1.pack_start(hBox1, Gtk::PACK_SHRINK);  //pack top box  
     vBox1.pack_start(hBox2, Gtk::PACK_SHRINK);  //pack empty space 
     hBox3.pack_start(btnOk, Gtk::PACK_SHRINK);  //pack OK button into hBox3 
     hBox3.pack_start(btnCancel, Gtk::PACK_SHRINK);  //pack cancel button into hBox3 
     vBox1.pack_start(hBox3, Gtk::PACK_SHRINK);  //pack hbox3 
     vBox1.pack_start(hBox4, Gtk::PACK_SHRINK);  //pack hbox3 
     add(vBox1); 
     btnOk.signal_clicked().connect(sigc::mem_fun(*this, &FrmMain::on_ok_button_clicked)); 
    btnCancel.signal_clicked().connect(sigc::mem_fun(*this, &FrmMain::on_cancel_button_clicked)); 
     sigc::slot<bool> timeoutSlot = sigc::mem_fun(*this, &FrmMain::start_serial_read); 
     Glib::signal_timeout().connect(timeoutSlot,timeout_value); 
     show_all_children(); 
} 

FrmMain::~FrmMain(){ 

} 

void FrmMain::on_ok_button_clicked(){ 
    labelSentStatus.set_text("Sent"); 
} 

void FrmMain::on_cancel_button_clicked(){ 
    labelSentStatus.set_text(""); 
} 

bool FrmMain::start_serial_read() 
{ 
    try { 
     BufferedAsyncSerial serial("/dev/ttyUSB0",4800); 
     sleep(1); 
     //boost::this_thread::sleep(boost::posix_time::seconds(2)); 
     //Return immediately. String is written *after* the function returns, 
     //in a separate thread. 
     //serial.writeString("Hello world\r\n"); 
     string scaleReading = serial.readStringUntil("\r"); 
     lblMass.set_text(scaleReading); 
     cout<<scaleReading("\r")<<endl;   
     serial.close(); 
     return true; 

    } catch(boost::system::system_error& e) 
    { 
     cout<<"Error: "<<e.what()<<endl; 
     return true; 
    } 
}