I want make device execute command , when I press the radio-button in QT
-
I am in serial communication with the device. if i enter the command, device execute and send data to me. I can see that in data_show . but i want if i press the radio-button, device execute command. It's original command is "mon 1" and "mon 0" . How can i do that? (command is command window ui )
below is my code.
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); mSerialport = new QSerialPort (this); connect(ui->pushButton_send,&QPushButton::clicked,[=](){ sendMsg(ui->command->toPlainText()); }); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_connect_clicked() { mSerialport->setPortName(ui->comboBox_port->currentText()); if (ui->comboBox_baudrate->currentText() == "9600 bps") mSerialport->setBaudRate(QSerialPort::Baud9600); else if (ui->comboBox_baudrate->currentText() == "19200 bps") mSerialport->setBaudRate(QSerialPort::Baud19200); else if (ui->comboBox_baudrate->currentText() == "38400 bps") mSerialport->setBaudRate(QSerialPort::Baud38400); else if (ui->comboBox_baudrate->currentText() == "57600 bps") mSerialport->setBaudRate(QSerialPort::Baud57600); else if (ui->comboBox_baudrate->currentText() == "115200 bps") mSerialport->setBaudRate(QSerialPort::Baud115200); mSerialport->setDataBits(QSerialPort::Data8); mSerialport->setStopBits(QSerialPort::OneStop); if (mSerialport->open(QIODevice::ReadWrite)) { QMessageBox::information(this,tr("connect"),"Serial communication start"); } else { QMessageBox::critical(this,tr("error"),mSerialport->errorString()); } } void MainWindow::on_pushButton_disconnect_clicked() { QMessageBox::information(this,tr("disconnect"),"Serial communication end"); mSerialport->close(); } void MainWindow::sendMsg(const QString &msg) { QString str = msg; str.append("\n"); this->mSerialport->write(str.toLatin1()); ui->datalog->insertPlainText(QDateTime::currentDateTime(). toString("yyyy-MM-dd hh:mm:ss") + " [send] " + msg + "\n"); } void MainWindow::recvMsg() { QByteArray msg = this->mSerialport->readAll(); ui->datalog->insertPlainText(QDateTime::currentDateTime(). toString("yyyy-MM-dd hh:mm:ss") + " [recieve] " + msg.toHex(). data() + "\n"); } void MainWindow::read_device_data() { QByteArray device_data(mSerialport->readAll()); ui->data_show->setText(device_data); }
-
Y r u opening duplicate question for the same topic ?. U have already posted one question exactly same topic. I have asked you the question.
Now in this piece of code where is the radio button ? I don't see any radio button at all in the code. Is it part of UI code ? If yes, you can add the code piece like this.
connect(ui->radioButton,&QRadioButton::clicked,={
CallMe();
}); -
@dheerendra
sorry... Callme is what mean? -
callme() is function name.
-
ui->setupUi(this); mSerialport = new QSerialPort (this); connect(ui->pushButton_send,&QPushButton::clicked,[=](){ sendMsg(ui->command->toPlainText()) }); connect(ui->radioButton_on,&QRadioButton::clicked,[=](){ monitor_mode_on(); });
void MainWindow::monitor_mode_on() { " " } //I have write this, but between " " I do not know what code to write. is this 'mSerialport->"mon 1"' ? (mon 1 is command)
-
Hi
I assume you want to have
mSerialport->write("XXX");
To write the command to the device. -
@mrjj
thank you for reply.MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); mSerialport = new QSerialPort (this); connect(ui->pushButton_send,&QPushButton::clicked,[=](){ sendMsg(ui->command->toPlainText()); }); connect(ui->radioButton_on,&QRadioButton::clicked,[=](){ monitor_mode_on(); }); connect(ui->radioButton_off,&QRadioButton::clicked,[=](){ monitor_mode_off(); }); }
Is this the right structure for the rules? Add two connectors below and it will not run . (Two sentences inclusing radio-button)
-
Well on button you call sendMsg and for the
radioButton_on you call monitor_mode_on() which will send a command and
same does radioButton_off
Which seem pretty ok if monitor_mode_on/off sends
command to device to enable/disable something. -
@segtteee
Nope, should send the string if port is open etc.
The write function returns how many bytes written.so you can do
qDebug() << "written->" << mSerialport->write("mon 1");
(#include <QDebug>)Can you see on device if it gets the string?
-
@segtteee
It means that it wrote 5 chars to the serial port.
so it seems it does send it :)- The equipment does not carry out orders ("mon 1" or "mon 0") .
And you are sure that is the syntax for the command?
And board are ready to accept commands etc?
Who wrote the code on the board?
- The equipment does not carry out orders ("mon 1" or "mon 0") .