I want to know how to assign a value to a dial
-
I am receiving the value from the equipment while doing serial communication. like
QByteArray device_data(mSerialport->readAll());
ui->data_show->setText(device_data);
i want recive data at Qdial , but ui->dial-> " ??" What commands should be entered in ?"?
(data_show is qlabel) -
@jsulm
These values appear consecutively in application output.
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(mSerialport,SIGNAL(readyRead()),this,SLOT(read_device_data())); connect(ui->radioButton_on,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_on_clicked())); connect(ui->radioButton_off,SIGNAL(clicked(bool)),this,SLOT(on_radioButton_off_clicked())); } 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::read_device_data() { QByteArray device_data(mSerialport->readAll()); ui->data_show->setText(device_data); ui->dial->setRange(0,3.5); qDebug() << device_data; ui->dial->setValue(device_data.toInt()); } void MainWindow::on_radioButton_on_clicked() { qDebug() << "written->" << mSerialport->write("mon 1\n"); } void MainWindow::on_radioButton_off_clicked() { qDebug() << "written->" << mSerialport->write("mon 0\n"); }
-
@segtteee said in I want to know how to assign a value to a dial:
These values appear consecutively in application output
What are these values?
Can you show what you see in application output?
Also it looks like you want to use floating point numbers, not integers. What does your device send to you exactly? What is the format of that numbers?
Also you should not set the range in read_device_data() - it is enough to do it once in the constructor.
And keep in mind that readAll() will not necessarily give you everything your device sent to you. Because the data can be split into several pieces. In this case you would need to buffer the data until you got the whole packet. -
-
@segtteee Good to know that the data is actually a string containing a floating point number and a newline...
You should do it this way:- Set range to 0-350 (because QDial only accepts integer numbers, see its documentation)
- Remove new line from each number string
- Convert to double
- Multiply by 100 and set it in dial
double number = QString(device_data).remove('\n').toDouble() * 100; qDebug() << number; // Make sure conversion to double worked ui->dial->setValue(number);