Commit f16a3d82 authored by Pavel Vainerman's avatar Pavel Vainerman

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

parent d7a65833
......@@ -3,7 +3,7 @@
Name: libuniset
Version: 1.0
Release: alt49
Release: alt50
Summary: UniSet - library for building distributed industrial control systems
License: GPL
Group: Development/C++
......@@ -212,6 +212,9 @@ rm -f %buildroot%_libdir/*.la
%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
- added support 'const' and [private|protecte|public]
for <variables> in uniset-codegen
......
......@@ -58,6 +58,9 @@ pollActivated(false)
recv_timeout = conf->getArgPInt("--" + prefix + "-recv-timeout",it.getProp("recv_timeout"), 500);
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);
noQueryOptimization = conf->getArgInt("--" + prefix + "-no-query-optimization",it.getProp("no_query_optimization"));
......@@ -189,6 +192,8 @@ void MBExchange::step()
if( !checkProcActive() )
return;
updateRespondSensors();
if( sidHeartBeat!=DefaultObjectId && ptHeartBeat.checkTime() )
{
try
......@@ -219,7 +224,7 @@ void MBExchange::setProcActive( bool st )
// -----------------------------------------------------------------------------
void MBExchange::sigterm( int signo )
{
dlog[Debug::WARN] << myname << ": ********* SIGTERM(" << signo <<") ********" << endl;
dlog[Debug::WARN] << myname << ": ********* SIGTERM(" << signo << ") ********" << endl;
setProcActive(false);
UniSetObject_LT::sigterm(signo);
}
......@@ -2659,3 +2664,73 @@ bool MBExchange::RTUDevice::checkRespond()
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:
tmExchange
};
void execute();
protected:
virtual void step();
virtual void processingMessage( UniSetTypes::VoidMessage *msg );
......@@ -236,6 +238,7 @@ class MBExchange:
void updateMTR(RegMap::iterator& it);
void updateRTU188(RegMap::iterator& it);
void updateRSProperty( RSProperty* p, bool write_only=false );
virtual void updateRespondSensors();
bool checkUpdateSM( bool wrFunc );
bool checkPoll( bool wrFunc );
......
......@@ -117,54 +117,6 @@ void MBTCPMaster::sysCommand( UniSetTypes::SystemMessage *sm )
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()
{
{
......@@ -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 )
{
cout << "Default: prefix='mbtcp'" << endl;
......@@ -264,34 +183,3 @@ MBTCPMaster* MBTCPMaster::init_mbmaster( int argc, const char* const* argv,
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:
/*! глобальная функция для вывода help-а */
static void help_print( int argc, const char* const* argv );
void execute();
protected:
virtual void sysCommand( UniSetTypes::SystemMessage *sm );
......@@ -205,14 +203,10 @@ class MBTCPMaster:
int port;
int recv_timeout;
virtual void step();
// virtual void step();
virtual ModbusClient* initMB( bool reopen=false );
void poll_thread();
void updateRespondSensors();
virtual void sigterm( int signo );
bool force_disconnect;
private:
......
......@@ -15,8 +15,7 @@ mbrtu(0),
defSpeed(ComPort::ComSpeed0),
use485F(false),
transmitCtl(false),
rs_pre_clean(false),
allNotRespond(false)
rs_pre_clean(false)
{
prop_prefix = "";
......@@ -26,7 +25,6 @@ allNotRespond(false)
UniXML_iterator it(cnode);
// ---------- init RS ----------
// UniXML_iterator it(cnode);
devname = conf->getArgParam("--"+prefix+"-dev",it.getProp("device"));
if( devname.empty() )
throw UniSetTypes::SystemError(myname+"(RTUExchange): Unknown device..." );
......@@ -39,14 +37,9 @@ allNotRespond(false)
transmitCtl = conf->getArgInt("--"+prefix+"-transmit-ctl",it.getProp("transmitCtl"));
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);
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() )
{
readConfiguration();
......@@ -120,6 +113,13 @@ ModbusClient* RTUExchange::initMB( bool reopen )
// -----------------------------------------------------------------------------
void RTUExchange::step()
{
try
{
if( sidExchangeMode != DefaultObjectId && force_out )
exchangeMode = shm->localGetValue(aitExchangeMode,sidExchangeMode);
}
catch(...){}
{
uniset_mutex_lock l(pollMutex,2000);
poll();
......@@ -127,16 +127,15 @@ void RTUExchange::step()
MBExchange::step();
}
// -----------------------------------------------------------------------------
void RTUExchange::poll()
{
if( trAllNotRespond.hi(allNotRespond) )
ptAllNotRespond.reset();
ptTimeout.reset();
if( allNotRespond && mb && ptAllNotRespond.checkTime() )
if( allNotRespond && mb && ptTimeout.checkTime() )
{
ptAllNotRespond.reset();
ptTimeout.reset();
initMB(true);
}
......
......@@ -46,7 +46,7 @@ class RTUExchange:
bool rs_pre_clean;
bool allNotRespond;
Trigger trAllNotRespond;
PassiveTimer ptAllNotRespond;
// PassiveTimer ptAllNotRespond;
};
// -----------------------------------------------------------------------------
#endif // _RS_EXCHANGE_H_
......
......@@ -10,5 +10,6 @@ uniset-start.sh -f ./uniset-rtuexchange --confile test.xml \
--dlog-add-levels info,crit,warn,level4,level3 \
--rs-force 0 \
--rs-force-out 0 \
--rs-polltime 3000
#,level3
# --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