Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I want make device execute command , when I press the radio-button in QT
Forum Updated to NodeBB v4.3 + New Features

I want make device execute command , when I press the radio-button in QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    segtteee
    wrote on last edited by
    #1

    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);
    }
    
    
    1 Reply Last reply
    -1
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      S 1 Reply Last reply
      2
      • dheerendraD dheerendra

        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();
        });

        S Offline
        S Offline
        segtteee
        wrote on last edited by
        #3

        @dheerendra
        sorry... Callme is what mean?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by dheerendra
          #4

          callme() is function name.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          S 1 Reply Last reply
          1
          • dheerendraD dheerendra

            callme() is function name.

            S Offline
            S Offline
            segtteee
            wrote on last edited by segtteee
            #5

            @dheerendra

            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)
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi
              I assume you want to have
              mSerialport->write("XXX");
              To write the command to the device.

              S 1 Reply Last reply
              1
              • mrjjM mrjj

                Hi
                I assume you want to have
                mSerialport->write("XXX");
                To write the command to the device.

                S Offline
                S Offline
                segtteee
                wrote on last edited by
                #7

                @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)

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  S 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    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.

                    S Offline
                    S Offline
                    segtteee
                    wrote on last edited by
                    #9

                    @mrjj
                    I think mSerialport->write("XXX"); is wrong.
                    my device couldn't recognize command.

                    mrjjM 1 Reply Last reply
                    0
                    • S segtteee

                      @mrjj
                      I think mSerialport->write("XXX"); is wrong.
                      my device couldn't recognize command.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @segtteee
                      Hi
                      In what way wrong?
                      the XXX was just example
                      you need
                      mSerialport->write("mon 1");
                      or what ever real command is. :)

                      S 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @segtteee
                        Hi
                        In what way wrong?
                        the XXX was just example
                        you need
                        mSerialport->write("mon 1");
                        or what ever real command is. :)

                        S Offline
                        S Offline
                        segtteee
                        wrote on last edited by
                        #11

                        @mrjj
                        I wrote mSerialport-> write ("mon 1"); but device doesn't work.
                        So is not the code wrong?

                        mrjjM 1 Reply Last reply
                        0
                        • S segtteee

                          @mrjj
                          I wrote mSerialport-> write ("mon 1"); but device doesn't work.
                          So is not the code wrong?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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?

                          S 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @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?

                            S Offline
                            S Offline
                            segtteee
                            wrote on last edited by
                            #13

                            @mrjj
                            i wrote qDebug() << "written->" << mSerialport->write("mon 1");
                            and i can see 'written-> 5' in application output window.
                            What does this mean?
                            The equipment does not carry out orders ("mon 1" or "mon 0") .

                            mrjjM 1 Reply Last reply
                            0
                            • S segtteee

                              @mrjj
                              i wrote qDebug() << "written->" << mSerialport->write("mon 1");
                              and i can see 'written-> 5' in application output window.
                              What does this mean?
                              The equipment does not carry out orders ("mon 1" or "mon 0") .

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @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?
                              S 1 Reply Last reply
                              1
                              • mrjjM mrjj

                                @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?
                                S Offline
                                S Offline
                                segtteee
                                wrote on last edited by
                                #15

                                @mrjj
                                By writing behind "\ N" I had solved the problem . Thank you!!

                                mrjjM 1 Reply Last reply
                                0
                                • S segtteee

                                  @mrjj
                                  By writing behind "\ N" I had solved the problem . Thank you!!

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @segtteee
                                  ahh, it was expecting a line. not just the text.
                                  super :)

                                  1 Reply Last reply
                                  1

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved