Commit 2d4d7dd1 authored by Pavel Vainerman's avatar Pavel Vainerman

minor fixes, make style

parent 19906299
...@@ -2674,7 +2674,7 @@ bool MBExchange::initDeviceInfo( RTUDeviceMap& m, ModbusRTU::ModbusAddr a, UniXM ...@@ -2674,7 +2674,7 @@ bool MBExchange::initDeviceInfo( RTUDeviceMap& m, ModbusRTU::ModbusAddr a, UniXM
dinfo << myname << "(initDeviceInfo): add addr=" << ModbusRTU::addr2str(a) << endl; dinfo << myname << "(initDeviceInfo): add addr=" << ModbusRTU::addr2str(a) << endl;
int tout = it.getPIntProp("timeout", ptTimeout.getInterval() ); int tout = it.getPIntProp("timeout", ptTimeout.getInterval() );
d->second->resp_Delay.set(0,tout); // ставим время на отпускание.. см. checkRespond() d->second->resp_Delay.set(0, tout); // ставим время на отпускание.. см. checkRespond()
d->second->resp_invert = it.getIntProp("invert"); d->second->resp_invert = it.getIntProp("invert");
return true; return true;
} }
...@@ -3005,6 +3005,7 @@ bool MBExchange::poll() ...@@ -3005,6 +3005,7 @@ bool MBExchange::poll()
<< " for sensors: "; << " for sensors: ";
print_plist(dlog()->level3(), it->second->slst) print_plist(dlog()->level3(), it->second->slst)
<< endl << " err: " << ex << endl; << endl << " err: " << ex << endl;
if( ex.err == ModbusRTU::erTimeOut && !d->ask_every_reg ) if( ex.err == ModbusRTU::erTimeOut && !d->ask_every_reg )
break; break;
} }
...@@ -3070,16 +3071,16 @@ bool MBExchange::RTUDevice::checkRespond() ...@@ -3070,16 +3071,16 @@ bool MBExchange::RTUDevice::checkRespond()
{ {
bool prev = resp_state; bool prev = resp_state;
resp_state = resp_Delay.check( prev_numreply!=numreply ); resp_state = resp_Delay.check( prev_numreply != numreply );
prev_numreply.store(numreply); prev_numreply.store(numreply);
return (prev!=resp_state); return (prev != resp_state);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void MBExchange::updateRespondSensors() void MBExchange::updateRespondSensors()
{ {
for( const auto& it1: rmap ) for( const auto& it1 : rmap )
{ {
RTUDevice* d(it1.second); RTUDevice* d(it1.second);
......
...@@ -13,9 +13,7 @@ using namespace UniSetExtensions; ...@@ -13,9 +13,7 @@ using namespace UniSetExtensions;
MBTCPMaster::MBTCPMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmId, MBTCPMaster::MBTCPMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmId,
const std::shared_ptr<SharedMemory> ic, const std::string& prefix ): const std::shared_ptr<SharedMemory> ic, const std::string& prefix ):
MBExchange(objId, shmId, ic, prefix), MBExchange(objId, shmId, ic, prefix),
force_disconnect(true), force_disconnect(true)
mbtcp(nullptr),
pollThread(0)
{ {
if( objId == DefaultObjectId ) if( objId == DefaultObjectId )
throw UniSetTypes::SystemError("(MBTCPMaster): objId=-1?!! Use --" + prefix + "-name" ); throw UniSetTypes::SystemError("(MBTCPMaster): objId=-1?!! Use --" + prefix + "-name" );
...@@ -55,7 +53,7 @@ MBTCPMaster::MBTCPMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shm ...@@ -55,7 +53,7 @@ MBTCPMaster::MBTCPMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shm
else else
ic->addReadItem( sigc::mem_fun(this, &MBTCPMaster::readItem) ); ic->addReadItem( sigc::mem_fun(this, &MBTCPMaster::readItem) );
pollThread = new ThreadCreator<MBTCPMaster>(this, &MBTCPMaster::poll_thread); pollThread = make_shared<ThreadCreator<MBTCPMaster>>(this, &MBTCPMaster::poll_thread);
pollThread->setFinalAction(this, &MBTCPMaster::final_thread); pollThread->setFinalAction(this, &MBTCPMaster::final_thread);
if( dlog()->is_info() ) if( dlog()->is_info() )
...@@ -71,9 +69,6 @@ MBTCPMaster::~MBTCPMaster() ...@@ -71,9 +69,6 @@ MBTCPMaster::~MBTCPMaster()
if( pollThread->isRunning() ) if( pollThread->isRunning() )
pollThread->join(); pollThread->join();
} }
delete pollThread;
mbtcp.reset();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
std::shared_ptr<ModbusClient> MBTCPMaster::initMB( bool reopen ) std::shared_ptr<ModbusClient> MBTCPMaster::initMB( bool reopen )
...@@ -195,9 +190,6 @@ void MBTCPMaster::sigterm( int signo ) ...@@ -195,9 +190,6 @@ void MBTCPMaster::sigterm( int signo )
if( pollThread->isRunning() ) if( pollThread->isRunning() )
pollThread->join(); pollThread->join();
delete pollThread;
pollThread = 0;
} }
try try
......
...@@ -225,7 +225,7 @@ class MBTCPMaster: ...@@ -225,7 +225,7 @@ class MBTCPMaster:
// т.к. TCP может "зависнуть" на подключении к недоступному узлу // т.к. TCP может "зависнуть" на подключении к недоступному узлу
// делаем опрос в отдельном потоке // делаем опрос в отдельном потоке
ThreadCreator<MBTCPMaster>* pollThread; /*!< поток опроса */ std::shared_ptr<ThreadCreator<MBTCPMaster>> pollThread; /*!< поток опроса */
UniSetTypes::uniset_rwmutex tcpMutex; UniSetTypes::uniset_rwmutex tcpMutex;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
......
...@@ -13,9 +13,7 @@ using namespace UniSetExtensions; ...@@ -13,9 +13,7 @@ using namespace UniSetExtensions;
MBTCPMultiMaster::MBTCPMultiMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmId, MBTCPMultiMaster::MBTCPMultiMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmId,
const std::shared_ptr<SharedMemory> ic, const std::string& prefix ): const std::shared_ptr<SharedMemory> ic, const std::string& prefix ):
MBExchange(objId, shmId, ic, prefix), MBExchange(objId, shmId, ic, prefix),
force_disconnect(true), force_disconnect(true)
pollThread(0),
checkThread(0)
{ {
tcpMutex.setName(myname + "_tcpMutex"); tcpMutex.setName(myname + "_tcpMutex");
...@@ -131,9 +129,9 @@ MBTCPMultiMaster::MBTCPMultiMaster( UniSetTypes::ObjectId objId, UniSetTypes::Ob ...@@ -131,9 +129,9 @@ MBTCPMultiMaster::MBTCPMultiMaster( UniSetTypes::ObjectId objId, UniSetTypes::Ob
else else
ic->addReadItem( sigc::mem_fun(this, &MBTCPMultiMaster::readItem) ); ic->addReadItem( sigc::mem_fun(this, &MBTCPMultiMaster::readItem) );
pollThread = new ThreadCreator<MBTCPMultiMaster>(this, &MBTCPMultiMaster::poll_thread); pollThread = make_shared<ThreadCreator<MBTCPMultiMaster>>(this, &MBTCPMultiMaster::poll_thread);
pollThread->setFinalAction(this, &MBTCPMultiMaster::final_thread); pollThread->setFinalAction(this, &MBTCPMultiMaster::final_thread);
checkThread = new ThreadCreator<MBTCPMultiMaster>(this, &MBTCPMultiMaster::check_thread); checkThread = make_shared<ThreadCreator<MBTCPMultiMaster>>(this, &MBTCPMultiMaster::check_thread);
checkThread->setFinalAction(this, &MBTCPMultiMaster::final_thread); checkThread->setFinalAction(this, &MBTCPMultiMaster::final_thread);
...@@ -153,8 +151,6 @@ MBTCPMultiMaster::~MBTCPMultiMaster() ...@@ -153,8 +151,6 @@ MBTCPMultiMaster::~MBTCPMultiMaster()
if( pollThread->isRunning() ) if( pollThread->isRunning() )
pollThread->join(); pollThread->join();
delete pollThread;
} }
if( checkThread ) if( checkThread )
...@@ -163,15 +159,9 @@ MBTCPMultiMaster::~MBTCPMultiMaster() ...@@ -163,15 +159,9 @@ MBTCPMultiMaster::~MBTCPMultiMaster()
if( checkThread->isRunning() ) if( checkThread->isRunning() )
checkThread->join(); checkThread->join();
delete checkThread;
} }
for( auto& it : mblist ) mbi = mblist.rend();
{
it.mbtcp.reset(); // = nullptr;
mbi = mblist.rend();
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
std::shared_ptr<ModbusClient> MBTCPMultiMaster::initMB( bool reopen ) std::shared_ptr<ModbusClient> MBTCPMultiMaster::initMB( bool reopen )
......
...@@ -286,10 +286,10 @@ class MBTCPMultiMaster: ...@@ -286,10 +286,10 @@ class MBTCPMultiMaster:
// т.к. TCP может "зависнуть" на подключении к недоступному узлу // т.к. TCP может "зависнуть" на подключении к недоступному узлу
// делаем опрос в отдельном потоке // делаем опрос в отдельном потоке
ThreadCreator<MBTCPMultiMaster>* pollThread; /*!< поток опроса */ std::shared_ptr< ThreadCreator<MBTCPMultiMaster> > pollThread; /*!< поток опроса */
UniSetTypes::uniset_rwmutex tcpMutex; UniSetTypes::uniset_rwmutex tcpMutex;
ThreadCreator<MBTCPMultiMaster>* checkThread; /*!< поток проверки связи по другим каналам */ std::shared_ptr< ThreadCreator<MBTCPMultiMaster> > checkThread; /*!< поток проверки связи по другим каналам */
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#endif // _MBTCPMultiMaster_H_ #endif // _MBTCPMultiMaster_H_
......
...@@ -223,6 +223,7 @@ bool RTUExchange::poll() ...@@ -223,6 +223,7 @@ bool RTUExchange::poll()
} }
int prev_numreply = d->numreply; int prev_numreply = d->numreply;
if( d->dtype == MBExchange::dtRTU188 ) if( d->dtype == MBExchange::dtRTU188 )
{ {
if( !d->rtu ) if( !d->rtu )
......
...@@ -77,11 +77,11 @@ namespace ModbusRTU ...@@ -77,11 +77,11 @@ namespace ModbusRTU
typedef unsigned long RegID; typedef unsigned long RegID;
/*! Получение уникального ID (hash?) на основе номера функции и регистра /*! Получение уникального ID (hash?) на основе номера функции и регистра
* Требования к данной функции: * Требования к данной функции:
* 1. ID > диапазона возможных регистров (>65535) * 1. ID > диапазона возможных регистров (>65535)
* 2. одинаковые регистры, но разные функции должны давать разный ID * 2. одинаковые регистры, но разные функции должны давать разный ID
* 3. регистры идущие подряд, должны давать ID идущие тоже подряд * 3. регистры идущие подряд, должны давать ID идущие тоже подряд
*/ */
RegID genRegID( const ModbusRTU::ModbusData r, const int fn ); RegID genRegID( const ModbusRTU::ModbusData r, const int fn );
......
...@@ -118,14 +118,15 @@ TEST_CASE("genRegID", "[modbus][genRegID]" ) ...@@ -118,14 +118,15 @@ TEST_CASE("genRegID", "[modbus][genRegID]" )
int max_reg = numeric_limits<ModbusRTU::ModbusData>::max(); int max_reg = numeric_limits<ModbusRTU::ModbusData>::max();
int max_fn = numeric_limits<ModbusRTU::ModbusByte>::max(); int max_fn = numeric_limits<ModbusRTU::ModbusByte>::max();
ModbusRTU::RegID prevID = ModbusRTU::genRegID(0,0); ModbusRTU::RegID prevID = ModbusRTU::genRegID(0, 0);
for( int f=1; f<max_fn; f++ )
for( int f = 1; f < max_fn; f++ )
{ {
ModbusRTU::RegID minID = ModbusRTU::genRegID(0,f); ModbusRTU::RegID minID = ModbusRTU::genRegID(0, f);
REQUIRE( minID > prevID ); REQUIRE( minID > prevID );
for( int r=1; r<max_reg; r++ ) for( int r = 1; r < max_reg; r++ )
REQUIRE( ModbusRTU::genRegID(r,f) == minID+r ); REQUIRE( ModbusRTU::genRegID(r, f) == minID + r );
} }
} }
#endif #endif
......
...@@ -198,17 +198,19 @@ TEST_CASE("uniset_rwmutex_{wr|r} thread lock", "[mutex][threadlock][basic]" ) ...@@ -198,17 +198,19 @@ TEST_CASE("uniset_rwmutex_{wr|r} thread lock", "[mutex][threadlock][basic]" )
g_mutex.rlock(); g_mutex.rlock();
std::vector< std::future<bool> > vw(3); std::vector< std::future<bool> > vw(3);
for( int w=0; w<3; w++ )
vw.push_back( std::async(std::launch::async, writer_thread,w) ); for( int w = 0; w < 3; w++ )
vw.push_back( std::async(std::launch::async, writer_thread, w) );
std::vector< std::future<bool> > vr(3); std::vector< std::future<bool> > vr(3);
for( int r=0; r<5; r++ )
vr.push_back( std::async(std::launch::async, reader_thread,r) ); for( int r = 0; r < 5; r++ )
vr.push_back( std::async(std::launch::async, reader_thread, r) );
msleep(10); msleep(10);
// read захватывают сразу без задержек.. // read захватывают сразу без задержек..
for( auto&& i: vr ) for( auto && i : vr )
{ {
if( i.valid() ) if( i.valid() )
REQUIRE( i.get() == true ); REQUIRE( i.get() == true );
...@@ -218,7 +220,7 @@ TEST_CASE("uniset_rwmutex_{wr|r} thread lock", "[mutex][threadlock][basic]" ) ...@@ -218,7 +220,7 @@ TEST_CASE("uniset_rwmutex_{wr|r} thread lock", "[mutex][threadlock][basic]" )
g_mutex.unlock(); g_mutex.unlock();
// проверяем что write-ы завершают работу. // проверяем что write-ы завершают работу.
for( auto& i: vw ) for( auto& i : vw )
{ {
if( i.valid() ) if( i.valid() )
REQUIRE( i.get() == true ); REQUIRE( i.get() == true );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment