Commit f16a3d82 authored by Pavel Vainerman's avatar Pavel Vainerman

(ModbusMaster): вынес ещё одну общую функцию в базовый класс

parent d7a65833
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
Name: libuniset Name: libuniset
Version: 1.0 Version: 1.0
Release: alt49 Release: alt50
Summary: UniSet - library for building distributed industrial control systems Summary: UniSet - library for building distributed industrial control systems
License: GPL License: GPL
Group: Development/C++ Group: Development/C++
...@@ -212,6 +212,9 @@ rm -f %buildroot%_libdir/*.la ...@@ -212,6 +212,9 @@ rm -f %buildroot%_libdir/*.la
%changelog %changelog
* Mon Oct 31 2011 Pavel Vainerman <pv@altlinux.ru> 1.0-alt50
- ModbusMaster extensions code refactoring
* Tue Oct 25 2011 Pavel Vainerman <pv@altlinux.ru> 1.0-alt49 * Tue Oct 25 2011 Pavel Vainerman <pv@altlinux.ru> 1.0-alt49
- added support 'const' and [private|protecte|public] - added support 'const' and [private|protecte|public]
for <variables> in uniset-codegen for <variables> in uniset-codegen
......
...@@ -58,6 +58,9 @@ pollActivated(false) ...@@ -58,6 +58,9 @@ pollActivated(false)
recv_timeout = conf->getArgPInt("--" + prefix + "-recv-timeout",it.getProp("recv_timeout"), 500); recv_timeout = conf->getArgPInt("--" + prefix + "-recv-timeout",it.getProp("recv_timeout"), 500);
int tout = conf->getArgPInt("--" + prefix + "-timeout",it.getProp("timeout"), 5000); int tout = conf->getArgPInt("--" + prefix + "-timeout",it.getProp("timeout"), 5000);
// для совместимости со старым RTUExchange
// надо обратывать и all-timeout
tout = conf->getArgPInt("--" + prefix + "-all-timeout",it.getProp("all_timeout"), tout);
ptTimeout.setTiming(tout); ptTimeout.setTiming(tout);
noQueryOptimization = conf->getArgInt("--" + prefix + "-no-query-optimization",it.getProp("no_query_optimization")); noQueryOptimization = conf->getArgInt("--" + prefix + "-no-query-optimization",it.getProp("no_query_optimization"));
...@@ -189,6 +192,8 @@ void MBExchange::step() ...@@ -189,6 +192,8 @@ void MBExchange::step()
if( !checkProcActive() ) if( !checkProcActive() )
return; return;
updateRespondSensors();
if( sidHeartBeat!=DefaultObjectId && ptHeartBeat.checkTime() ) if( sidHeartBeat!=DefaultObjectId && ptHeartBeat.checkTime() )
{ {
try try
...@@ -219,7 +224,7 @@ void MBExchange::setProcActive( bool st ) ...@@ -219,7 +224,7 @@ void MBExchange::setProcActive( bool st )
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void MBExchange::sigterm( int signo ) void MBExchange::sigterm( int signo )
{ {
dlog[Debug::WARN] << myname << ": ********* SIGTERM(" << signo <<") ********" << endl; dlog[Debug::WARN] << myname << ": ********* SIGTERM(" << signo << ") ********" << endl;
setProcActive(false); setProcActive(false);
UniSetObject_LT::sigterm(signo); UniSetObject_LT::sigterm(signo);
} }
...@@ -2659,3 +2664,73 @@ bool MBExchange::RTUDevice::checkRespond() ...@@ -2659,3 +2664,73 @@ bool MBExchange::RTUDevice::checkRespond()
return ( prev != resp_state ); return ( prev != resp_state );
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void MBExchange::updateRespondSensors()
{
bool chanTimeout = false;
{
uniset_mutex_lock l(pollMutex);
chanTimeout = pollActivated && ptTimeout.checkTime();
}
for( MBExchange::RTUDeviceMap::iterator it1=rmap.begin(); it1!=rmap.end(); ++it1 )
{
RTUDevice* d(it1->second);
if( chanTimeout )
it1->second->resp_real = false;
if( dlog.debugging(Debug::LEVEL4) )
{
dlog[Debug::LEVEL4] << myname << ": check respond addr=" << ModbusRTU::addr2str(d->mbaddr)
<< " respond_id=" << d->resp_id
<< " real=" << d->resp_real
<< " state=" << d->resp_state
<< endl;
}
if( d->checkRespond() && d->resp_id != DefaultObjectId )
{
try
{
bool set = d->resp_invert ? !d->resp_state : d->resp_state;
shm->localSaveState(d->resp_dit,d->resp_id,set,getId());
}
catch( Exception& ex )
{
dlog[Debug::CRIT] << myname << "(step): (respond) " << ex << std::endl;
}
}
}
}
// -----------------------------------------------------------------------------
void MBExchange::execute()
{
no_extimer = true;
try
{
askTimer(tmExchange,0);
}
catch(...){}
initMB(false);
while(1)
{
try
{
step();
}
catch( Exception& ex )
{
dlog[Debug::CRIT] << myname << "(execute): " << ex << std::endl;
}
catch(...)
{
dlog[Debug::CRIT] << myname << "(execute): catch ..." << endl;
}
msleep(polltime);
}
}
// -----------------------------------------------------------------------------
...@@ -189,6 +189,8 @@ class MBExchange: ...@@ -189,6 +189,8 @@ class MBExchange:
tmExchange tmExchange
}; };
void execute();
protected: protected:
virtual void step(); virtual void step();
virtual void processingMessage( UniSetTypes::VoidMessage *msg ); virtual void processingMessage( UniSetTypes::VoidMessage *msg );
...@@ -236,6 +238,7 @@ class MBExchange: ...@@ -236,6 +238,7 @@ class MBExchange:
void updateMTR(RegMap::iterator& it); void updateMTR(RegMap::iterator& it);
void updateRTU188(RegMap::iterator& it); void updateRTU188(RegMap::iterator& it);
void updateRSProperty( RSProperty* p, bool write_only=false ); void updateRSProperty( RSProperty* p, bool write_only=false );
virtual void updateRespondSensors();
bool checkUpdateSM( bool wrFunc ); bool checkUpdateSM( bool wrFunc );
bool checkPoll( bool wrFunc ); bool checkPoll( bool wrFunc );
......
...@@ -117,54 +117,6 @@ void MBTCPMaster::sysCommand( UniSetTypes::SystemMessage *sm ) ...@@ -117,54 +117,6 @@ void MBTCPMaster::sysCommand( UniSetTypes::SystemMessage *sm )
pollThread->start(); pollThread->start();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void MBTCPMaster::step()
{
updateRespondSensors();
MBExchange::step();
}
// -----------------------------------------------------------------------------
void MBTCPMaster::updateRespondSensors()
{
bool tcpIsTimeout = false;
{
uniset_mutex_lock l(tcpMutex);
tcpIsTimeout = pollActivated && ptTimeout.checkTime();
}
if( dlog.debugging(Debug::LEVEL4) )
dlog[Debug::LEVEL4] << myname << ": tcpTimeout=" << tcpIsTimeout << endl;
for( MBTCPMaster::RTUDeviceMap::iterator it1=rmap.begin(); it1!=rmap.end(); ++it1 )
{
RTUDevice* d(it1->second);
if( tcpIsTimeout )
d->resp_real = false;
if( dlog.debugging(Debug::LEVEL4) )
{
dlog[Debug::LEVEL4] << myname << ": check respond addr=" << ModbusRTU::addr2str(d->mbaddr)
<< " respond_id=" << d->resp_id
<< " real=" << d->resp_real
<< " state=" << d->resp_state
<< endl;
}
if( d->checkRespond() && d->resp_id != DefaultObjectId )
{
try
{
bool set = d->resp_invert ? !d->resp_state : d->resp_state;
shm->localSaveState(d->resp_dit,d->resp_id,set,getId());
}
catch( Exception& ex )
{
dlog[Debug::CRIT] << myname << "(step): (respond) " << ex << std::endl;
}
}
}
}
// -----------------------------------------------------------------------------
void MBTCPMaster::poll_thread() void MBTCPMaster::poll_thread()
{ {
{ {
...@@ -193,39 +145,6 @@ void MBTCPMaster::poll_thread() ...@@ -193,39 +145,6 @@ void MBTCPMaster::poll_thread()
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void MBTCPMaster::sigterm( int signo )
{
dlog[Debug::WARN] << myname << ": ********* SIGTERM(" << signo <<") ********" << endl;
setProcActive(false);
/*! \todo Доделать выставление безопасного состояния на выходы.
И нужно ли это. Ведь может не хватить времени на "обмен"
*/
// выставление безопасного состояния на выходы....
/*
RSMap::iterator it=rsmap.begin();
for( ; it!=rsmap.end(); ++it )
{
// if( it->stype!=UniversalIO::DigitalOutput && it->stype!=UniversalIO::AnalogOutput )
// continue;
if( it->safety == NoSafetyState )
continue;
try
{
}
catch( UniSetTypes::Exception& ex )
{
dlog[Debug::WARN] << myname << "(sigterm): " << ex << std::endl;
}
catch(...){}
}
*/
UniSetObject_LT::sigterm(signo);
}
// ------------------------------------------------------------------------------------------
void MBTCPMaster::help_print( int argc, const char* const* argv ) void MBTCPMaster::help_print( int argc, const char* const* argv )
{ {
cout << "Default: prefix='mbtcp'" << endl; cout << "Default: prefix='mbtcp'" << endl;
...@@ -264,34 +183,3 @@ MBTCPMaster* MBTCPMaster::init_mbmaster( int argc, const char* const* argv, ...@@ -264,34 +183,3 @@ MBTCPMaster* MBTCPMaster::init_mbmaster( int argc, const char* const* argv,
return new MBTCPMaster(ID,icID,ic,prefix); return new MBTCPMaster(ID,icID,ic,prefix);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void MBTCPMaster::execute()
{
no_extimer = true;
try
{
askTimer(tmExchange,0);
}
catch(...){}
initMB(false);
while(1)
{
try
{
step();
}
catch( Exception& ex )
{
dlog[Debug::CRIT] << myname << "(execute): " << ex << std::endl;
}
catch(...)
{
dlog[Debug::CRIT] << myname << "(execute): catch ..." << endl;
}
msleep(polltime);
}
}
// -----------------------------------------------------------------------------
...@@ -194,8 +194,6 @@ class MBTCPMaster: ...@@ -194,8 +194,6 @@ class MBTCPMaster:
/*! глобальная функция для вывода help-а */ /*! глобальная функция для вывода help-а */
static void help_print( int argc, const char* const* argv ); static void help_print( int argc, const char* const* argv );
void execute();
protected: protected:
virtual void sysCommand( UniSetTypes::SystemMessage *sm ); virtual void sysCommand( UniSetTypes::SystemMessage *sm );
...@@ -205,14 +203,10 @@ class MBTCPMaster: ...@@ -205,14 +203,10 @@ class MBTCPMaster:
int port; int port;
int recv_timeout; int recv_timeout;
virtual void step(); // virtual void step();
virtual ModbusClient* initMB( bool reopen=false ); virtual ModbusClient* initMB( bool reopen=false );
void poll_thread(); void poll_thread();
void updateRespondSensors();
virtual void sigterm( int signo );
bool force_disconnect; bool force_disconnect;
private: private:
......
...@@ -15,8 +15,7 @@ mbrtu(0), ...@@ -15,8 +15,7 @@ mbrtu(0),
defSpeed(ComPort::ComSpeed0), defSpeed(ComPort::ComSpeed0),
use485F(false), use485F(false),
transmitCtl(false), transmitCtl(false),
rs_pre_clean(false), rs_pre_clean(false)
allNotRespond(false)
{ {
prop_prefix = ""; prop_prefix = "";
...@@ -26,7 +25,6 @@ allNotRespond(false) ...@@ -26,7 +25,6 @@ allNotRespond(false)
UniXML_iterator it(cnode); UniXML_iterator it(cnode);
// ---------- init RS ---------- // ---------- init RS ----------
// UniXML_iterator it(cnode);
devname = conf->getArgParam("--"+prefix+"-dev",it.getProp("device")); devname = conf->getArgParam("--"+prefix+"-dev",it.getProp("device"));
if( devname.empty() ) if( devname.empty() )
throw UniSetTypes::SystemError(myname+"(RTUExchange): Unknown device..." ); throw UniSetTypes::SystemError(myname+"(RTUExchange): Unknown device..." );
...@@ -39,14 +37,9 @@ allNotRespond(false) ...@@ -39,14 +37,9 @@ allNotRespond(false)
transmitCtl = conf->getArgInt("--"+prefix+"-transmit-ctl",it.getProp("transmitCtl")); transmitCtl = conf->getArgInt("--"+prefix+"-transmit-ctl",it.getProp("transmitCtl"));
defSpeed = ComPort::getSpeed(speed); defSpeed = ComPort::getSpeed(speed);
int alltout = conf->getArgPInt("--"+prefix+"-all-timeout",it.getProp("all_timeout"), 2000);
ptAllNotRespond.setTiming(alltout);
sleepPause_usec = conf->getArgPInt("--" + prefix + "-sleepPause-usec",it.getProp("slepePause"), 100); sleepPause_usec = conf->getArgPInt("--" + prefix + "-sleepPause-usec",it.getProp("slepePause"), 100);
rs_pre_clean = conf->getArgInt("--"+prefix+"-pre-clean",it.getProp("pre_clean")); rs_pre_clean = conf->getArgInt("--"+prefix+"-pre-clean",it.getProp("pre_clean"));
noQueryOptimization = conf->getArgInt("--"+prefix+"-no-query-optimization",it.getProp("no_query_optimization"));
if( shm->isLocalwork() ) if( shm->isLocalwork() )
{ {
readConfiguration(); readConfiguration();
...@@ -120,6 +113,13 @@ ModbusClient* RTUExchange::initMB( bool reopen ) ...@@ -120,6 +113,13 @@ ModbusClient* RTUExchange::initMB( bool reopen )
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void RTUExchange::step() void RTUExchange::step()
{ {
try
{
if( sidExchangeMode != DefaultObjectId && force_out )
exchangeMode = shm->localGetValue(aitExchangeMode,sidExchangeMode);
}
catch(...){}
{ {
uniset_mutex_lock l(pollMutex,2000); uniset_mutex_lock l(pollMutex,2000);
poll(); poll();
...@@ -127,16 +127,15 @@ void RTUExchange::step() ...@@ -127,16 +127,15 @@ void RTUExchange::step()
MBExchange::step(); MBExchange::step();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void RTUExchange::poll() void RTUExchange::poll()
{ {
if( trAllNotRespond.hi(allNotRespond) ) if( trAllNotRespond.hi(allNotRespond) )
ptAllNotRespond.reset(); ptTimeout.reset();
if( allNotRespond && mb && ptAllNotRespond.checkTime() ) if( allNotRespond && mb && ptTimeout.checkTime() )
{ {
ptAllNotRespond.reset(); ptTimeout.reset();
initMB(true); initMB(true);
} }
......
...@@ -46,7 +46,7 @@ class RTUExchange: ...@@ -46,7 +46,7 @@ class RTUExchange:
bool rs_pre_clean; bool rs_pre_clean;
bool allNotRespond; bool allNotRespond;
Trigger trAllNotRespond; Trigger trAllNotRespond;
PassiveTimer ptAllNotRespond; // PassiveTimer ptAllNotRespond;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#endif // _RS_EXCHANGE_H_ #endif // _RS_EXCHANGE_H_
......
...@@ -10,5 +10,6 @@ uniset-start.sh -f ./uniset-rtuexchange --confile test.xml \ ...@@ -10,5 +10,6 @@ uniset-start.sh -f ./uniset-rtuexchange --confile test.xml \
--dlog-add-levels info,crit,warn,level4,level3 \ --dlog-add-levels info,crit,warn,level4,level3 \
--rs-force 0 \ --rs-force 0 \
--rs-force-out 0 \ --rs-force-out 0 \
--rs-polltime 3000
#,level3 #,level3
# --rs-force 1 \ # --rs-force 1 \
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