gpt4 book ai didi

c++ - GTKMM 和 boost.asio

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:50 26 4
gpt4 key购买 nike

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

#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;
}
}

最佳答案

我通过将 timeout_value 增加到 2 秒 (timeout_value(2000)) 并在信号处理程序中使用 Gtk::sleep() 函数来为串行端口提供更多时间来获取读数来解决此问题。我之前尝试过的最多的是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;
}
}

关于c++ - GTKMM 和 boost.asio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com