Home > Mobile >  QT serial port to send problem (under Windows)
QT serial port to send problem (under Windows)

Time:10-16

Have any brothers met QT memory consumption situation when a serial port to send? I use QT5.11 bring a serial port example blockingmaster this problem also exists, the longer the greater the memory consumption
About an hour need to consume 1 m memory, the code is as follows, and write under the Windows system clipboard cannot use the
//! [4]
Void MasterThread: : run ()
{
Bool currentPortNameChanged=false;

M_mutex. The lock ();
//! [4]//! [5]
QString currentPortName;
If (currentPortName!={m_portName)
CurrentPortName=m_portName;
CurrentPortNameChanged=true;
}

Int currentWaitTimeout=m_waitTimeout;
QString currentRequest=m_request;
M_mutex. Unlock ();
//! [5]//! [6]
QSerialPort serial;

{if (currentPortName. IsEmpty ())
Emit error (tr (" No port name specified "));
return;
}

If (currentPortNameChanged) {
Serial. The close ();
Serial. SetPortName (currentPortName);

if (! Serial. Open (QIODevice: : ReadWrite)) {
Emit error (tr (" Can 't open % 1, error code % 2 ")
. Arg (m_portName). Arg (serial. The error ()));
return;
}
}

//while (! M_quit) {
While (true) {
//! [6]//! [7]

//! [7]//! [8]
//write request
Const QByteArray requestData=(https://bbs.csdn.net/topics/currentRequest.toUtf8);
Serial. Write (requestData);
{if (serial. WaitForBytesWritten (m_waitTimeout))
//! [8]//! [10]
//read the response
{if (serial. WaitForReadyRead (currentWaitTimeout))
QByteArray responseData=(https://bbs.csdn.net/topics/serial.readAll);
While (serial. WaitForReadyRead (10))
ResponseData +=serial. ReadAll ();

Const QString response=QString: : fromUtf8 (responseData);
//! [12]
Emit this - & gt; The response (response);
//! [10]//! [11]//! [12]
} else {
Emit a timeout (tr (" Wait to read the response timeout % 1 ")
. Arg (QTime: : currentTime (). The toString ()));
}
//! [9]//! [11]
} else {
Emit a timeout (tr (" Wait write request timeout % 1 ")
. Arg (QTime: : currentTime (). The toString ()));
}
//! [9]//! [13]
The lock ();/* m_mutex.
M_cond. Wait (& amp; M_mutex);
If (currentPortName!={m_portName)
CurrentPortName=m_portName;
CurrentPortNameChanged=true;
} else {
CurrentPortNameChanged=false;
}
CurrentWaitTimeout=m_waitTimeout;
CurrentRequest=m_request;
M_mutex. Unlock (); */
}
//! [13]
}

CodePudding user response:

Haven't met?

CodePudding user response:

Haven't met, the building Lord your own problem, but the thread of the serial port to write well is not easy,

CodePudding user response:

Is the thread, qt to open the serial port on the while outside

CodePudding user response:

Windows 1 hour and 1 m? , be persistent, hang up 1 week, for example, memory increase of more than 160 M so? If just add 1 M or a few M after stabilized, it should be a window of caching mechanism, can be allocated in the thread a piece of memory, is used to receive data maintenance, recycling, avoid frequent distribution - the operation of the free memory,

CodePudding user response:

Memory has been increased, can reach hundreds of M

CodePudding user response:

Put the source code, or send a complete source code download address, do you like this, no one can help got you,

QT serial port has a pit, not to say that it has bugs, but says it is easy to get wrong,
You can try using CSerialPort,


Or you don't write QT serial program, it is better to use asynchronous signal channel way,
QT official example toxic,

CodePudding user response:

Like Windows using waitforreadyred this interface will be unknown problems, using asynchronous signal channel
  •  Tags:  
  • Qt
  • Related