How to use serial port on Qt 5.5.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(); }
-
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.
-
Add your user to the dialout group. Don't forget to logout/login before re-running your application.
-
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
-
@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