PDA

View Full Version : QSerialPort not working, but shows no errors (Ubuntu)



Brixus
13th October 2014, 17:57
Hello,

I need to use QSerialPort together with a USB serial adapter on Ubuntu.
This is a FT232RL which is mounted as "ttyUSB0".

Using CuteCom I can use the serial adapter and it is working fine.

Sadly with QT I am not able to access ttyUSB0.

I get no errors while compiling and executing the programm leads to an empty console window which freezes :-(

If I change ttyUSB0 to some not existing thing like ttyUSB, I get the error that he could not open the device.
So this is actually good.

But I have no Idea, how to solve my problem :-(

It would be really nice if you could help me.

Thank you very much :-)


This is the code I tried:


#include <iostream>

#include <QApplication>
#include <QSerialPort>

using namespace std;

QSerialPort serial;


int main (int argc, char *argv[])
{
QApplication app(argc, argv);
serial.setPortName("/dev/ttyUSB0");
serial.open(QIODevice::WriteOnly);
serial.setBaudRate(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);



for (int i=0; i<100; i++)
{
serial.write("test");
serial.flush();
cout << i << endl;
}

serial.close();
return 0;
}

And this is my .pro file:

QT += core serialport widgets

QT -= gui

TARGET = Timer
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp
QMAKE_CXXFLAGS += -std=c++0x

wysota
14th October 2014, 06:05
Shouldn't you be setting the options before opening the port? And I would suggest to avoid making the port object a global variable.

Lesiok
14th October 2014, 09:07
As I recall QSerialPort needs an event loop or the use of method waitForBytesWritten();

ChrisW67
14th October 2014, 21:21
Also check that the user you are running as has read and write permission to that port. Usually this means a particular group membership. ( Cutecom may be working only because it is marked setuid or setgid.)

Brixus
17th October 2014, 21:24
Sorry for the late reaction.

I had no Internet on the last days :-(


I found out the problem.

The last hint with the permissions was quiet good. :-)

There was a lockfile on the port.
After deleting the lockfile in the folder /var/lock everything worked well :-)

Thank you very much!