@@ -57,7 +57,6 @@ QObject(parent), mInitialized(false), mTimer(new QTimer(this)), yield_user(0), y
5757Tsmppt::~Tsmppt ()
5858{
5959 QLOG_DEBUG () << " Tsmppt::~Tsmppt()" ;
60- modbus_close (mCtx );
6160 modbus_free (mCtx );
6261}
6362
@@ -95,7 +94,10 @@ bool Tsmppt::initialize()
9594
9695 uint16_t regs[6 ];
9796 if (!readInputRegisters (REG_V_PU, 6 , regs))
97+ {
98+ modbus_close (mCtx );
9899 return false ;
100+ }
99101
100102 // Voltage scaling:
101103 m_v_pu = (float )regs[1 ];
@@ -117,17 +119,26 @@ bool Tsmppt::initialize()
117119
118120 // Read hardware version:
119121 if (!readInputRegisters (REG_EHW_VERSION, 1 , regs))
122+ {
123+ modbus_close (mCtx );
120124 return false ;
125+ }
121126 m_hw_ver = QString::number (regs[0 ] >> 8 ) + " ." + QString::number (regs[0 ] & 0xff );
122127
123128 // Read model
124129 if (!readInputRegisters (REG_EMODEL, 1 , regs))
130+ {
131+ modbus_close (mCtx );
125132 return false ;
133+ }
126134 m_model = regs[0 ];
127135
128136 // Read serial number:
129137 if (!readInputRegisters (REG_ESERIAL, 4 , regs))
138+ {
139+ modbus_close (mCtx );
130140 return false ;
141+ }
131142 m_serial = (uint64_t )((regs[0 ] & 0xff ) - 0x30 ) * 10000000 ;
132143 m_serial += (uint64_t )((regs[0 ] >> 8 ) - 0x30 ) * 1000000 ;
133144 m_serial += (uint64_t )((regs[1 ] & 0xff ) - 0x30 ) * 100000 ;
@@ -137,6 +148,7 @@ bool Tsmppt::initialize()
137148 m_serial += (uint64_t )((regs[3 ] & 0xff ) - 0x30 ) * 10 ;
138149 m_serial += (uint64_t )((regs[3 ] >> 8 ) - 0x30 );
139150
151+ modbus_close (mCtx );
140152 mInitialized = true ;
141153 emit tsmpptConnected ();
142154 QLOG_DEBUG () << " Tsmppt::initialize(end)" ;
@@ -149,6 +161,12 @@ void Tsmppt::updateValues()
149161
150162 QLOG_DEBUG () << " Tsmppt::updateValues()" ;
151163
164+ if (modbus_connect (mCtx ) == -1 )
165+ {
166+ QLOG_ERROR () << " MODBUS:" << modbus_strerror (errno);
167+ return ;
168+ }
169+
152170 if (readInputRegisters (REG_FIRST_DYN, REG_LAST_DYN-REG_FIRST_DYN+1 , reg))
153171 {
154172 // Battery voltage:
@@ -215,6 +233,7 @@ void Tsmppt::updateValues()
215233 setTimeInAbsorption (reg[REG_T_ABS-REG_FIRST_DYN]/60 );
216234 setTimeInFloat (reg[REG_T_FLOAT-REG_FIRST_DYN]/60 );
217235 }
236+ modbus_close (mCtx );
218237}
219238
220239QString Tsmppt::firmwareVersion () const
0 commit comments