Qextserial on debian linux and readyRead slot
Hi All,
I would like to use Qextserial in debian linux with readyRead signal. There is no error on init, but it cannot send signal.
Here is my settings:
Code:
mSerialPort = new QextSerialPort("/dev/ttyUSB0", QextSerialPort::EventDriven);
mSerialPort->setBaudRate(BAUD921600);
mSerialPort->setDataBits(DATA_8);
mSerialPort->setStopBits(STOP_1);
mSerialPort->setParity(PAR_NONE);
mSerialPort->setFlowControl(FLOW_OFF);
mSerialPort->setQueryMode(QextSerialPort::EventDriven);
mSerialPort->setRts(true);
mSerialPort->setDtr(true);
QObject::connect(mSerialPort,
SIGNAL(readyRead
()),
this,
SLOT(dataAvailable
()));
if (!mSerialPort->isOpen())
log_exception(PacketDriverException, "cannot open %s", "/dev/ttyUSB0");
I read a post here
http://www.qtcentre.org/threads/1562...th-readyRead()
from 2008, it said I can' t use signal in linux :(
Is it true?
How can I set to use signals?
thx a lot
Zamek
Re: Qextserial on debian linux and readyRead slot
Hi,
This is a deprecated library. Use QtSerialPort instead that is developed by Qt team. It is a part of Qt 5 and also can be used on Qt 4 as a module.
Re: Qextserial on debian linux and readyRead slot
Quote:
Originally Posted by
^NyAw^
Hi,
This is a deprecated library. Use QtSerialPort instead that is developed by Qt team. It is a part of Qt 5 and also can be used on Qt 4 as a module.
Thx for your response. I changed to QtSerialPort, but unfortunately it doesn't works:(
QExtSerial can read but didn't send signal, QtSerial doesn't read in polling mode:(
There is hardware which sends 80bytes of frames with 921600 8N1 no flow.
Here is the code which I tried:
Code:
mSerialPort.setPortName("/dev/ttyUSB0");
if (!mSerialPort.isOpen())
log_exception(PacketDriverException, "cannot open %s", "/dev/ttyUSB0");
if (! (mSerialPort.setBaudRate(921600, QSerialPort::AllDirections) &&
mSerialPort.setDataBits(QSerialPort::Data8) &&
mSerialPort.setStopBits(QSerialPort::OneStop) &&
mSerialPort.setParity(QSerialPort::NoParity) &&
mSerialPort.setFlowControl(QSerialPort::NoFlowControl)))
log_exception(PacketDriverException, "cannot set params of %s", "/dev/ttyUSB0");
mSerialPort.clearError();
mSerialPort.flush();
mSerialPort.clear(QSerialPort::AllDirections);
QObject::connect(&mSerialPort,
SIGNAL(readyRead
()),
this,
SLOT(dataAvailable
()));
QObject::connect(&mSerialPort,
SIGNAL(error
(QSerialPort
::SerialPortError)),
this,
SLOT(error
()));
std::cout<<b.size()<<std::endl;
There is no error and data comings to port. Size of b is 0.
What did I do wrong?
thx
Zamek
Re: Qextserial on debian linux and readyRead slot
Code:
YourClass::dataAvailable()
{
std::cout<<b.size()<<std::endl;
}
Re: Qextserial on debian linux and readyRead slot
Hi,
Quote:
Originally Posted by
kuzulis
Code:
YourClass::dataAvailable()
{
std::cout<<b.size()<<std::endl;
}
Yes my dataAvailable is:
Code:
void PacketDriver::dataAvailable()
{
if (mSerialPort.isReadable())
readChar(mSerialPort.readAll());
}
void PacketDriver::error()
{
QString err
=mSerialPort.
errorString();
qDebug()<<err;
mSerialPort.clearError();
}
but I use readAll at end of open, because there is no signal. After I open the port and there are incoming data I have to read something with readAll.
I started this with QExtSerial, which can read after open, but it cannot send signals. QtSerial cannot read after open and cannot send signal:(
The hardware continuously send 80 bytes of frames at every 5 ms. Cutecom and gtkterm can read it.
Re: Qextserial on debian linux and readyRead slot
So, the readyRead() signal does not emitted never? Can you test it with use the Terminal example from the QtSerialPort?
Re: Qextserial on debian linux and readyRead slot
Quote:
Originally Posted by
kuzulis
So, the readyRead() signal does not emitted never? Can you test it with use the Terminal example from the QtSerialPort?
Hi Kuzulis,
creaderasync example works well. And I try to modify it, because it passing QSerialPort as a pointer in constructor, but I need to instantiate QSerialport in my constructor.
There is the problem, because when I try to instantiate in constructor it doesn't works, but no error!
Here is my test version:
Code:
#include "serialportreader.h"
#include <QCoreApplication>
#include <iostream>
QT_USE_NAMESPACE
#ifdef TEST
SerialPortReader
::SerialPortReader(QObject *parent
)#else
SerialPortReader
::SerialPortReader(QSerialPort
*serialPort,
QObject *parent
)#endif
#ifndef TEST
, m_serialPort(serialPort)
#endif
, m_standardOutput(stdout)
{
#ifdef TEST
m_serialPort = new QSerialPort();
if (!m_serialPort
->open
(QIODevice::ReadWrite)) return;
if (!m_serialPort->setBaudRate(921600))
return;
if (!m_serialPort->setDataBits(QSerialPort::Data8))
return;
if (!m_serialPort->setParity(QSerialPort::NoParity))
return;
if (!m_serialPort->setStopBits(QSerialPort::OneStop))
return;
if (!m_serialPort->setFlowControl(QSerialPort::NoFlowControl))
return;
#endif
connect(m_serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead()));
connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(handleError(QSerialPort::SerialPortError)));
}
SerialPortReader::~SerialPortReader()
{
}
void SerialPortReader::handleReadyRead()
{
m_readData = m_serialPort->readAll();
std::cout<<"read, size:"<<m_readData.length()<<std::endl;
if (!m_timer.isActive())
m_timer.start(5000);
}
void SerialPortReader::handleTimeout()
{
if (m_readData.isEmpty()) {
m_standardOutput <<
QObject::tr("No data was currently available for reading from port %1").
arg(m_serialPort
->portName
()) << endl;
} else {
m_standardOutput <<
QObject::tr("Data successfully received from port %1").
arg(m_serialPort
->portName
()) << endl;
m_standardOutput << m_readData << endl;
}
}
void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
if (serialPortError == QSerialPort::ReadError) {
m_standardOutput <<
QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").
arg(m_serialPort
->portName
()).
arg(m_serialPort
->errorString
()) << endl;
}
}
So, how can I instantiate QSerialport at constructor?
thx
Zamek
Re: Qextserial on debian linux and readyRead slot
Some advance:
Is it true I need to use connect before I open the port? In terminal example does this.
I tried to this, and in open I get an error: "No such file or direcory". I tried /dev/ttyUSB0 and ttyUSB0 too.
Code:
PacketDriver::PacketDriver(const QString& cfgPrefix)
: mFifo(cfgPrefix)
{
mSerialPort = new QSerialPort(this);
connect(mSerialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(error()));
connect(mSerialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
mSerialPort->setPortName("/dev/ttyUSB0");
if (!mSerialPort
->open
(QIODevice::ReadWrite)) log_exception(PacketDriverException, "cannot open /dev/ttyUSB0");
if (!mSerialPort->setBaudRate(921600))
log_exception(PacketDriverException, "cannot set baud 921600");
if (!mSerialPort->setDataBits(QSerialPort::Data8))
log_exception(PacketDriverException, "cannot set databits 8");
if (!mSerialPort->setParity(QSerialPort::NoParity))
log_exception(PacketDriverException, "cannot set noparity");
if (!mSerialPort->setStopBits(QSerialPort::OneStop))
log_exception(PacketDriverException, "cannot set one stopbits");
if (!mSerialPort->setFlowControl(QSerialPort::NoFlowControl))
log_exception(PacketDriverException, "cannot set noflow");
}
void PacketDriver::error()
{
QString err
=mSerialPort
->errorString
();
std::cout<<"Error:"<<err.toStdString()<<std::endl;
}
void PacketDriver::onReadyRead()
{
std::cout<<"dataAvailable";
if (mSerialPort->bytesAvailable()) {
std::cout<<", bytes:"<<bytes.length()<<std::endl;
readChar(bytes);
}
}
Could I debug into QtSerial?
thx
Zamek
Re: Qextserial on debian linux and readyRead slot
Seems, you missed setup a port name, like:
Code:
SerialPortReader::SerialPortReader(...)
{
...
m_serialPort = new QSerialPort();
m_serialPort->setPortName("/dev/ttyUSB0"); // << :)
if (!m_serialPort
->open
(QIODevice::ReadWrite)) return;
...
}
UPD:
Quote:
Is it true I need to use connect before I open the port? In terminal example does this.
Yes, you can... Has no difference do it before or after than open(). But if you do QObject::connect() after open(), don't forget after close() to call QObject::disconnect().
Quote:
I tried to this, and in open I get an error: "No such file or direcory". I tried /dev/ttyUSB0 and ttyUSB0 too.
It because maybe really a port is not present.
Quote:
Could I debug into QtSerial?
Yes, of course.
Re: Qextserial on debian linux and readyRead slot
hi,
Quote:
Originally Posted by
kuzulis
Seems, you missed setup a port name, like:
Yes thx, it was a little mistake, I already found it...
Quote:
It because maybe really a port is not present.
No, the port continuously works, cutecom, gtkterm and qtserialport example terminal can use it and works well. There is a hw, which always sends 80 bytes of frames at every 5 ms.
I used to check with lsof /dev/ttyUSB0 to opened locks. I am the member of dialout groups.
What do I need to set for debugging into source of qtserialport?
When I debug my program, I see the address of serialport, but I can't see the variables and I cannot step into a function.
thx
Zamek
Re: Qextserial on debian linux and readyRead slot
Quote:
What do I need to set for debugging into source of qtserialport?
Build *.so library as "debug" target... Or directly connect the sources via "serialport-lib.pri" file (imho, *.pri - it is preffered and simple to do gebugging).
Re: Qextserial on debian linux and readyRead slot
Hi Kuzulis,
I found the problem. I wrote this test inside an infinite loop, so the main thread couldn't runs and there was no emitted event on received char.
It was good lesson for me: the main thread cannot blocked!
Many thx to you
Zamek