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. How to use serial port on Qt 5.5.1
Qt 6.11 is out! See what's new in the release blog

How to use serial port on Qt 5.5.1

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 3.5k Views 2 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.
  • N Offline
    N Offline
    nistar
    wrote on last edited by
    #1

    I use Ubuntu 14.04 and Qt 5.5.1.
    I want to do a GUI, for write the command to serial port.
    Before I did a program of Qt 5.5.0, but now it don't function.

    When it run, there is a error "QIODevice::write (QSerialPort): device not open"
    I don't know why?
    When I use Qt 5.5.0, there isn't error.

    //  .pro
    QT       += serialport
    
    // controlget.h
    #ifndef CONTROLGET_H
    #define CONTROLGET_H
    
    #include <QWidget>
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QDebug>
    #include <QString>
    
    namespace Ui {
    
    class controlget;
    }
    
    class controlget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit controlget(QWidget *parent = 0);
        ~controlget();
    
    private slots:
        void writeCom();
        void setTarget(unsigned char channelNumber, unsigned short target );
    
    private:
        Ui::controlget *ui;
        unsigned short target;
        unsigned char channelNumber;
        QSerialPort *myCom;
        QByteArray command;
    };
    
    #endif // CONTROLGET_H
    
    
    // controlget.cpp
    #include "controlget.h"
    #include "ui_controlget.h"
    #include "iostream"
    
    controlget::controlget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::controlget)
    {
        ui->setupUi(this);
        ui->slider->setMinimum(1000);
        ui->slider->setMaximum(2000);
        ui->slider->setValue(1500);
        ui->lineEdit->setText("1500");
    
    
        myCom= new QSerialPort();
        myCom->setPortName("/dev/ttyACM0");
        myCom->open(QIODevice::WriteOnly);
        myCom->setBaudRate(9600);
        myCom->setDataBits(QSerialPort::Data8);
        myCom->setParity(QSerialPort::NoParity);
        myCom->setStopBits(QSerialPort::OneStop);
        myCom->setFlowControl(QSerialPort::NoFlowControl);
    
    
        unsigned short targetInit=6000;
        setTarget(0x01, targetInit);
    
        targetInit=8000;    // model of Speed(4000) or Position(8000)
        setTarget(0x00, targetInit);
    
        connect(ui->slider, SIGNAL(valueChanged(int)), this, SLOT(writeCom()));
    }
    
    controlget::~controlget()
    {
        delete ui;
    }
    
    void controlget::writeCom(){
        int pos = ui->slider->value();
        QString str = QString("%1").arg(pos);
        ui->lineEdit->setText(str);
        target=pos*4;
    
        setTarget(0x01, target );
    }
    
    void controlget::setTarget(unsigned char channelNumber, unsigned short target )
    {
        if (target<4000){
            target=4000;
        }
        if(target>8000){
            target=8000;
        }
    
        command.resize(4);
        command[0]=0x84;
        command[1]=channelNumber;
        command[2]=(char)(target & 0x7F);
        command[3]=(char)((target >> 7) & 0x7F);
    
        myCom->write(command);
    
        QString string = QString(command.toHex());
        qDebug()<<string;
    }
    
    
    //main.cpp
    #include "controlget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        controlget w;
        w.show();
    
        return a.exec();
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please check that open returns true.

      Most likely, you don't have the permissions to access the serial port.

      Add yourself to a group that has access to it if it's the case.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        Please check that open returns true.

        Most likely, you don't have the permissions to access the serial port.

        Add yourself to a group that has access to it if it's the case.

        N Offline
        N Offline
        nistar
        wrote on last edited by
        #3

        @SGaist How to have the the permissions to access the serial port on Ubuntu?

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Please check that open returns true.

          Most likely, you don't have the permissions to access the serial port.

          Add yourself to a group that has access to it if it's the case.

          N Offline
          N Offline
          nistar
          wrote on last edited by
          #4

          @SGaist

          ls -al /dev/ttyACM0
          

          crw-rw---- 1 root dialout 166, 0 ene 6 18:53 /dev/ttyACM0

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Add your user to the dialout group. Don't forget to logout/login before re-running your application.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SweetOrange
              wrote on last edited by
              #6

              Because I am doing something similar I have a few beginners questions.
              Under public: in class definition there is the constructor explicit controlget(QWidget *parent = 0);
              So what does this statement do? You set a pointer to Qwidget equal to 0. What does this mean?
              Also under private: in class definition the statement Ui::controlget *ui is a bit unclear to me. Does this set a pointer ui to the class controlget?
              In implementation file also is unclear the statement of constructor
              controlget::controlget(QWidget *parent) : QWidget(parent), ui(new Ui::controlget)

              thanks

              mrjjM 1 Reply Last reply
              0
              • S SweetOrange

                Because I am doing something similar I have a few beginners questions.
                Under public: in class definition there is the constructor explicit controlget(QWidget *parent = 0);
                So what does this statement do? You set a pointer to Qwidget equal to 0. What does this mean?
                Also under private: in class definition the statement Ui::controlget *ui is a bit unclear to me. Does this set a pointer ui to the class controlget?
                In implementation file also is unclear the statement of constructor
                controlget::controlget(QWidget *parent) : QWidget(parent), ui(new Ui::controlget)

                thanks

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

                @SweetOrange
                --Under public: in class definition there is the constructor explicit controlget(QWidget *parent = 0);
                Its the constructor. the QWidget *parent = 0 is a default value. So if you dont give it one , it will
                use NULL for parent. Mena have no aprent.

                --Ui::controlget *ui is a bit unclear to me.
                Its gives access to the objects you put in the UI in the designer.
                The Designer creates a UI class that has all these objects and this is
                the variable to acces it.

                -In implementation file also is unclear the statement of constructor
                controlget::controlget(QWidget *parent) << this is our constructor
                : QWidget(parent), << here we give the parent variable to our baseClass. (base class is QWidget)
                ui(new Ui::controlget) << this NEWs (creates) a UI class for us and assign to -Ui::controlget *ui variable.

                thanks

                1 Reply Last reply
                0

                • Login

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