I am using QModbusTcpClient with Ethernet modbus TCP can read data is normal, but found that increased memory usage,
As long as there is data received will be a few K memory increases, the along while less than 120 m + program USES memory, startup is 8 m,
And I use QModbusRtuSerialMaster using RS485 everything is normal, the memory will not increase!
# # ifndef CMODBUS_H
# define CMODBUS_H
#include
#include
#include
The class CModBus: public CSerialPortConfig
{
Q_OBJECT
Public:
Explicit CModBus (const QString & amp; Capstr);
~ CModBus ();
Void portOpen ();
Void portClose ();
Bool portIsOpen ();
Void the read (const QModbusDataUnit & amp; Mdu, const int & amp; Address);
Void the write (const QModbusDataUnit & amp; Mdu, const int & amp; Address);
Signals:
Void linkState (const int & amp; Address, const bool & amp; St);
Void dataReady (const int & amp; Address, const QModbusDataUnit & amp; Mdu);
Private:
Void readReady ();
Void writeFinished ();
Void newSetttings (const Settings & amp; Ns);
Void errorOccurred (const QModbusDevice: : Error & amp; The error);
Int m_lastAddress=0;
QModbusClient * m_device=nullptr;
};
# endif//CMODBUS_H
# include "CModBus. H"
CModBus: : CModBus (const QString & amp; Capstr) : CSerialPortConfig (capstr)
{
NewSetttings (curSettings ());
The connect (this, & amp; CSerialPortConfig: : sigNewSetttings, this, & amp; CModBus: : newSetttings);
The connect (m_device, & amp; QModbusClient: : errorOccurred, this, & amp; CModBus: : errorOccurred);
M_device - & gt; ConnectDevice ();
If (m_device - & gt; The state ()!=QModbusDevice: : ConnectedState)
{
ErrorAppend (capstr + ":" + portName () + tr (" : the port error!" ));
}
}
CModBus: : ~ CModBus ()
{
}
Void CModBus: : newSetttings (const Settings & amp; Ns)
{
Bool isopen=false;
If (m_device!=nullptr)
{
Isopen=(m_device - & gt; The state ()==QModbusDevice: : ConnectedState);
The delete m_device;
M_device=nullptr;
}
If (ns. TcpEnable)
{
M_device=new QModbusTcpClient (this);
Const QUrl url=QUrl: : fromUserInput (ns. TcpHost);
M_device - & gt; SetConnectionParameter (QModbusDevice: : NetworkPortParameter, url. The port ());
M_device - & gt; SetConnectionParameter (QModbusDevice: : NetworkAddressParameter, url. Host ());
}
The else
{
M_device=new QModbusRtuSerialMaster (this);
M_device - & gt; SetConnectionParameter (QModbusDevice: : SerialPortNameParameter, ns. PortName);
M_device - & gt; SetConnectionParameter (QModbusDevice: : SerialParityParameter, ns. Parity);
M_device - & gt; SetConnectionParameter (QModbusDevice: : SerialBaudRateParameter, ns. Baud);
M_device - & gt; SetConnectionParameter (QModbusDevice: : SerialDataBitsParameter, ns. DataBits);
M_device - & gt; SetConnectionParameter (QModbusDevice: : SerialStopBitsParameter, ns. StopBits);
}
M_device - & gt; SetTimeout (ns. ResponseTime);
M_device - & gt; SetNumberOfRetries (ns. NumberOfRetries);
If (isopen)
{
M_device - & gt; ConnectDevice ();
}
}
Void CModBus: : read (const QModbusDataUnit & amp; Mdu, const int & amp; Address)
{
If ((! M_device) | | (m_device - & gt; The state ()!=QModbusDevice: : ConnectedState))
{
Emit linkState (address, false);
return;
}
QModbusReply * reply=m_device - & gt; SendReadRequest (mdu, address);
If (reply)
{
M_lastAddress=address;
if(! Reply - & gt; IsFinished ())
{
The connect (reply, & amp; QModbusReply: : finished, this, & amp; CModBus: : readReady);
}
The else
{
Emit linkState (address, false);
Delete the reply;
}
}
The else
{
Emit linkState (address, false);
}
}
Void CModBus: : write (const QModbusDataUnit & amp; Mdu, const int & amp; Address)
{
If ((! M_device) | | (m_device - & gt; The state ()!=QModbusDevice: : ConnectedState))
{
Emit linkState (address, false);
return;
}
QModbusReply * reply=m_device - & gt; SendWriteRequest (mdu, address);
If (reply)
{
M_lastAddress=address;
if(! Reply - & gt; IsFinished ())
{
The connect (reply, & amp; QModbusReply: : finished, this, & amp; CModBus: : writeFinished);
}
The else
{
Emit linkState (address, false);
Delete the reply;
}
}
The else
{
Emit linkState (address, false);
}
}
Void CModBus: : readReady ()
{
QModbusReply * reply=qobject_cast & lt; QModbusReply * & gt; (sender ());
if(! The reply)
{
Emit linkState (m_lastAddress, false);
return;
}
Const int address=reply - & gt; ServerAddress ();
Const QModbusDataUnit mdu=reply - & gt; The result ();
If (reply - & gt; Error ()==QModbusDevice: : NoError)
{
Emit dataReady (address, mdu);
Emit linkState (address, true);
}
The else
{
Emit linkState (m_lastAddress, false); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull