Hi,
I have created a Thread class:
#ifndef SERIALTHREAD_H
#define SERIALTHREAD_H
#include <QThread>
#include <qDebug>
#include "qextserialport.h"
class SerialThread
: public QThread{
Q_OBJECT
public:
portName=name;
}
void run();
private:
QextSerialPort *port;
private slots:
void onReadyRead();
};
#endif // SERIALTHREAD_H
#ifndef SERIALTHREAD_H
#define SERIALTHREAD_H
#include <QThread>
#include <qDebug>
#include "qextserialport.h"
class SerialThread : public QThread
{
Q_OBJECT
public:
SerialThread(QString name) {
portName=name;
}
void run();
private:
QString portName;
QextSerialPort *port;
private slots:
void onReadyRead();
};
#endif // SERIALTHREAD_H
To copy to clipboard, switch view to plain text mode
#include "SerialThread.h"
void SerialThread::run() {
port = new QextSerialPort(portName, QextSerialPort::EventDriven);
connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
exec();
}
void SerialThread::onReadyRead()
{
int a = port->bytesAvailable();
bytes.resize(a);
port->read(bytes.data(), bytes.size());
qDebug() << "bytes read:" << bytes.size();
qDebug() << "bytes:" << bytes;
}
#include "SerialThread.h"
void SerialThread::run() {
port = new QextSerialPort(portName, QextSerialPort::EventDriven);
connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open(QIODevice::ReadWrite);
exec();
}
void SerialThread::onReadyRead()
{
QByteArray bytes;
int a = port->bytesAvailable();
bytes.resize(a);
port->read(bytes.data(), bytes.size());
qDebug() << "bytes read:" << bytes.size();
qDebug() << "bytes:" << bytes;
}
To copy to clipboard, switch view to plain text mode
In the MainWindow I call this Thread:
MainWindow::MainWindow() {
setupUi(this);
t1 = new SerialThread("/dev/cu.usbserial-ftDIHUS6");
t1->start();
}
MainWindow::MainWindow() {
setupUi(this);
t1 = new SerialThread("/dev/cu.usbserial-ftDIHUS6");
t1->start();
}
To copy to clipboard, switch view to plain text mode
it seems to be a problem with the signals. When I send:
u I get:
bytes read: 1
bytes: "u"
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 1
bytes: "u"
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
To copy to clipboard, switch view to plain text mode
Without Threads it works so what's the problem?
Bookmarks