Home > Software engineering >  QSerialport multithreading issues
QSerialport multithreading issues

Time:09-21

Post for the first time, this problem bothering me for a long time, as shown in the following code, the if the serial port initialization and send data in the constructor, then the single chip microcomputer can receive data; But put a serial port initialization and send data in the run (), microcontroller is not receive data, but the qDebug () & lt; & lt;" 333 "; And the qDebug () & lt; & lt;" 444 "; Print as usual, I was using a qt4.8.4
 # include "serialportthread. H" 

SerialportThread: : SerialportThread (QObject * parent) :
QThread (parent)
{
If (0)
{
QDebug () & lt; & lt;" 222 ";
//initialize serial port
Myserialport=new QSerialPort ();

Myserialport - & gt; COM4 setPortName (" ");
Myserialport - & gt; Open (QIODevice: : ReadWrite);
Myserialport - & gt; SetBaudRate (QSerialPort: : Baud115200);
Myserialport - & gt; SetDataBits (QSerialPort: : Data8);
Myserialport - & gt; SetParity (QSerialPort: : NoParity);
Myserialport - & gt; SetStopBits (QSerialPort: : OneStop);
Myserialport - & gt; SetFlowControl (QSerialPort: : NoFlowControl);

Int d=77;
QString STR=QString: : number (d);//use QString: : number () function converts an integer to a static QString
QByteArray TxData=https://bbs.csdn.net/topics/str.toAscii ();//a QString into QByteArray
QDebug () & lt; & lt;" 333 ";
Myserialport - & gt; Write (TxData + "\ r \ n");//note: messages sent must end with \ r \ n
QDebug () & lt; & lt;" 444 ";
}

}
Void SerialportThread: : run ()
{
If (1)
{
QDebug () & lt; & lt;" 222 ";
//initialize serial port
Myserialport=new QSerialPort ();

Myserialport - & gt; COM4 setPortName (" ");
Myserialport - & gt; Open (QIODevice: : ReadWrite);
Myserialport - & gt; SetBaudRate (QSerialPort: : Baud115200);
Myserialport - & gt; SetDataBits (QSerialPort: : Data8);
Myserialport - & gt; SetParity (QSerialPort: : NoParity);
Myserialport - & gt; SetStopBits (QSerialPort: : OneStop);
Myserialport - & gt; SetFlowControl (QSerialPort: : NoFlowControl);

Int d=77;
QString STR=QString: : number (d);//use QString: : number () function converts an integer to a static QString
QByteArray TxData=https://bbs.csdn.net/topics/str.toAscii ();//a QString into QByteArray
QDebug () & lt; & lt;" 333 ";
Myserialport - & gt; Write (TxData + "\ r \ n");//note: messages sent must end with \ r \ n
QDebug () & lt; & lt;" 444 ";
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# # ifndef SERIALPORTTHREAD_H
# define SERIALPORTTHREAD_H

#include
#include
#include
#include

The class SerialportThread: public QThread
{
Q_OBJECT
Public:
Explicit SerialportThread (parent QObject *=0);
QSerialPort * myserialport;
Void the run ();
Signals:

Public slots:

Protected:


Private:


};

# endif//SERIALPORTTHREAD_H

CodePudding user response:

 # include "mainwindow. H" 
# include "ui_mainwindow. H"

MainWindow: : MainWindow (QWidget * parent) :
QMainWindow (parent),
UI (new UI: : MainWindow)
{
The UI - & gt; SetupUi (this);

}

MainWindow: : ~ MainWindow ()
{
Delete the UI;
}

Void MainWindow: : on_pushButton_clicked ()
{
QDebug () & lt; & lt;" 111 ";

Thread=new SerialportThread ();
Thread - & gt; start();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# # ifndef MAINWINDOW_H
# define MAINWINDOW_H

#include
# include "serialportthread. H"
#include
The namespace Ui {
The class MainWindow.
}

The class MainWindow: public QMainWindow
{
Q_OBJECT

Public:
Explicit MainWindow (QWidget * parent=0);
~ MainWindow ();
SerialportThread * thread;
Public slots:
Void on_pushButton_clicked ();
Private:
Ui: : MainWindow * Ui;
};

# endif//MAINWINDOW_H

CodePudding user response:

Pray god to help see ~ ~ ~ ~ ~ ~ ~ very appreciated

CodePudding user response:

Fyi:
//loop to a function every time send 200 bytes (this is the fixed) buffer, 
//a function need to incoming buffer, the circulation of 240 bytes (fixed) new buffer for processing,
//when dealing with each new printing takes two bytes of the buffer
# ifdef _MSC_VER
# pragma warning (4996) disable:
# endif
#include
#include
#include
# ifdef _MSC_VER
#include
#include
#include
# define MYVOID void
# define vsnprintf _vsnprintf
# the else
#include
#include
#include
# define CRITICAL_SECTION pthread_mutex_t
# define MYVOID void *
# endif
//the Log {
# define MAXLOGSIZE 20000000
# define MAXLINSIZE 16000
#include
#include
#include
Char logfilename1 []="MyLog1. Log";
Char logfilename2 []="MyLog2. Log";
The static char logstr [MAXLINSIZE + 1];
Char datestr [16].
Char timestr [16].
Char MSS [4].
CRITICAL_SECTION cs_log;
The FILE * flog;
# ifdef _MSC_VER
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related