Commit a2756ef7 authored by Pavel Vainerman's avatar Pavel Vainerman

testing

parent 2cebcea4
...@@ -179,20 +179,20 @@ int main( int argc, char **argv ) ...@@ -179,20 +179,20 @@ int main( int argc, char **argv )
<< " myaddr=" << ModbusRTU::addr2str(myaddr) << " myaddr=" << ModbusRTU::addr2str(myaddr)
<< " timeout=" << tout << " msec " << " timeout=" << tout << " msec "
<< endl; << endl;
}
ModbusTCPMaster mb;
if( verb )
dlog.addLevel( Debug::type(Debug::CRIT | Debug::WARN | Debug::INFO) ); dlog.addLevel( Debug::type(Debug::CRIT | Debug::WARN | Debug::INFO) );
}
ModbusTCPMaster mb;
mb.setLog(dlog); mb.setLog(dlog);
ost::Thread::setException(ost::Thread::throwException); // ost::Thread::setException(ost::Thread::throwException);
ost::InetAddress ia(iaddr.c_str()); ost::InetAddress ia(iaddr.c_str());
mb.connect(ia,port);
mb.setTimeout(tout); mb.setTimeout(tout);
mb.connect(ia,port);
while( 1)
{
switch(cmd) switch(cmd)
{ {
...@@ -370,6 +370,9 @@ int main( int argc, char **argv ) ...@@ -370,6 +370,9 @@ int main( int argc, char **argv )
return 1; return 1;
} }
msleep(200);
} // end of while
mb.disconnect(); mb.disconnect();
} }
catch( ModbusRTU::mbException& ex ) catch( ModbusRTU::mbException& ex )
......
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
<sensors name="Sensors"> <sensors name="Sensors">
<item name="Input1_S" textname=" 1" node="" iotype="DI" priority="Medium" default="1" /> <item name="Input1_S" textname=" 1" node="" iotype="DI" priority="Medium" default="1" />
<item name="Input2_S" textname=" 2" node="" iotype="DI" priority="Medium" mbtype="rtu" mbaddr="0x31" mbfunc="0x04" mbreg="0x02" rs="1" /> <item name="Input2_S" textname=" 2" node="" iotype="DI" priority="Medium" mbtype="rtu" mbaddr="0x31" mbfunc="0x04" mbreg="0x02" rs="1" />
<item name="Input3_S" textname=" 3" node="" iotype="DI" priority="Medium" udp="1"/> <item name="Input3_S" textname=" 3" node="" iotype="DI" priority="Medium" mbtcp="1" mbaddr="0x31" mbfunc="0x04" mbreg="0x02"/>
<item name="Input4_S" textname=" 4" node="" iotype="DI" priority="Medium" udp="1"/> <item name="Input4_S" textname=" 4" node="" iotype="DI" priority="Medium" udp="1"/>
<item name="Input5_S" textname=" 5" node="" iotype="DI" priority="Medium" udp="2"/> <item name="Input5_S" textname=" 5" node="" iotype="DI" priority="Medium" udp="2"/>
<item name="Input6_S" textname=" 6" node="" iotype="DI" priority="Medium" udp="2"> <item name="Input6_S" textname=" 6" node="" iotype="DI" priority="Medium" udp="2">
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
using namespace std; using namespace std;
using namespace UniSetTypes; using namespace UniSetTypes;
using namespace UniSetExtentions; using namespace UniSetExtentions;
using namespace ModbusRTU;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
MBMaster::MBMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmId, SharedMemory* ic, MBMaster::MBMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmId, SharedMemory* ic,
std::string prefix): std::string prefix):
...@@ -18,6 +19,7 @@ maxItem(0), ...@@ -18,6 +19,7 @@ maxItem(0),
mb(0), mb(0),
shm(0), shm(0),
initPause(0), initPause(0),
mbregFromID(false),
force(false), force(false),
force_out(false), force_out(false),
activated(false), activated(false),
...@@ -55,13 +57,16 @@ prefix(prefix) ...@@ -55,13 +57,16 @@ prefix(prefix)
recv_timeout = atoi(conf->getArgParam("--" + prefix + "-recv-timeout",it.getProp("recv_timeout")).c_str()); recv_timeout = atoi(conf->getArgParam("--" + prefix + "-recv-timeout",it.getProp("recv_timeout")).c_str());
if( recv_timeout <= 0 ) if( recv_timeout <= 0 )
recv_timeout = 100; recv_timeout = 2000;
string saddr = conf->getArgParam("--" + prefix + "-my-addr",it.getProp("addr")); string saddr = conf->getArgParam("--" + prefix + "-my-addr",it.getProp("addr"));
myaddr = ModbusRTU::str2mbAddr(saddr); myaddr = ModbusRTU::str2mbAddr(saddr);
if( saddr.empty() ) if( saddr.empty() )
myaddr = 0x00; myaddr = 0x00;
mbregFromID = atoi(conf->getArgParam("--" + prefix + "-reg-from-id",it.getProp("reg_from_id")).c_str());
dlog[Debug::INFO] << myname << "(init): mbregFromID=" << mbregFromID << endl;
polltime = atoi(conf->getArgParam("--" + prefix + "-polltime",it.getProp("polltime")).c_str()); polltime = atoi(conf->getArgParam("--" + prefix + "-polltime",it.getProp("polltime")).c_str());
if( !polltime ) if( !polltime )
polltime = 100; polltime = 100;
...@@ -133,6 +138,8 @@ prefix(prefix) ...@@ -133,6 +138,8 @@ prefix(prefix)
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
MBMaster::~MBMaster() MBMaster::~MBMaster()
{ {
if( mb )
mb->disconnect();
delete mb; delete mb;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -205,6 +212,7 @@ void MBMaster::init_mb() ...@@ -205,6 +212,7 @@ void MBMaster::init_mb()
mb = new ModbusTCPMaster(); mb = new ModbusTCPMaster();
mb->connect(ia,port); mb->connect(ia,port);
mb->setTimeout(recv_timeout); mb->setTimeout(recv_timeout);
mb->setLog(dlog);
} }
catch( Exception& ex ) catch( Exception& ex )
{ {
...@@ -241,24 +249,23 @@ void MBMaster::poll() ...@@ -241,24 +249,23 @@ void MBMaster::poll()
{ {
if( it->stype == UniversalIO::AnalogInput ) if( it->stype == UniversalIO::AnalogInput )
{ {
long val = callItem(it); long val = readReg(it);
IOBase::processingAsAI( ib, val, shm, force ); IOBase::processingAsAI( ib, val, shm, force );
} }
else if( it->stype == UniversalIO::DigitalInput ) else if( it->stype == UniversalIO::DigitalInput )
{ {
bool set = callItem(it) ? true : false; bool set = readReg(it) ? true : false;
IOBase::processingAsDI( ib, set, shm, force ); IOBase::processingAsDI( ib, set, shm, force );
} }
else if( it->stype == UniversalIO::AnalogOutput ) else if( it->stype == UniversalIO::AnalogOutput )
{ {
cerr << myname << "(poll): AnalogOutput !!! (sid=" << it->si.id << ")" << endl; long val = IOBase::processingAsAO(ib,shm,force_out);
// IOBase::processingAO( &ib, val ); writeReg(it,val);
} }
else if( it->stype == UniversalIO::DigitalOutput ) else if( it->stype == UniversalIO::DigitalOutput )
{ {
// if( force_out ) long val = IOBase::processingAsDO(ib,shm,force_out) ? 1 : 0;
// it->value = shm->localGetState(it->dit,it->si.id); writeReg(it,val);
// bool set IOBase::processingAO( &ib, val );
} }
} }
catch(ModbusRTU::mbException& ex ) catch(ModbusRTU::mbException& ex )
...@@ -293,25 +300,82 @@ void MBMaster::poll() ...@@ -293,25 +300,82 @@ void MBMaster::poll()
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
long MBMaster::callItem( MBMap::iterator& p ) long MBMaster::readReg( MBMap::iterator& p )
{ {
unsigned short v1=0, v2=0; try
{
if( p->mbfunc == ModbusRTU::fnReadInputRegisters ) if( p->mbfunc == ModbusRTU::fnReadInputRegisters )
{ {
ModbusRTU::ReadInputRetMessage ret = mb->read04(p->mbaddr, p->mbreg,1); // if( dlog.debugging(Debug::LEVEL3) )
// dlog[Debug::LEVEL3] << " read from " << ModbusRTU::addr2str(p->mbaddr) << " reg=" << ModbusRTU::dat2str(p->mbreg) << endl;
cerr << " read from " << ModbusRTU::addr2str(p->mbaddr) << " reg=" << ModbusRTU::dat2str(p->mbreg) << endl;
ModbusRTU::ReadInputRetMessage ret = mb->read04(p->mbaddr,p->mbreg,1);
return ret.data[0]; return ret.data[0];
} }
else if( p->mbfunc == ModbusRTU::fnReadOutputRegisters )
if( p->mbfunc == ModbusRTU::fnReadOutputRegisters )
{ {
ModbusRTU::ReadOutputRetMessage ret = mb->read03(p->mbaddr, p->mbreg,1); ModbusRTU::ReadOutputRetMessage ret = mb->read03(p->mbaddr,p->mbreg,1);
return ret.data[0]; return ret.data[0];
} }
else
cerr << myname << "(callItem): " << (int)p->mbfunc << endl; cerr << myname << "(readReg): " << (int)p->mbfunc << endl;
// return 0; }
catch( ModbusRTU::mbException& ex )
{
dlog[Debug::CRIT] << "(readReg): " << ex << endl;
}
catch(SystemError& err)
{
dlog[Debug::CRIT] << "(readReg): " << err << endl;
}
catch(Exception& ex)
{
dlog[Debug::CRIT] << "(readReg): " << ex << endl;
}
catch( ost::SockException& e )
{
dlog[Debug::CRIT] << "(readReg): " << e.getString() << ": " << e.getSystemErrorString() << endl;
}
return 0; return 0;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool MBMaster::writeReg( MBMap::iterator& p, long val )
{
if( p->mbfunc == fnWriteOutputRegisters )
{
ModbusRTU::WriteOutputMessage msg(p->mbaddr,p->mbreg);
msg.addData(val);
ModbusRTU::WriteOutputRetMessage ret = mb->write10(msg);
return true;
}
if( p->mbfunc == ModbusRTU::fnForceSingleCoil )
{
ModbusRTU::ForceSingleCoilRetMessage ret = mb->write05(p->mbaddr,p->mbreg,(bool)val);
return false;
}
if( p->mbfunc == fnWriteOutputSingleRegister )
{
ModbusRTU::WriteSingleOutputRetMessage ret = mb->write06(p->mbaddr,p->mbreg,val);
return true;
}
if( p->mbfunc == fnForceMultipleCoils )
{
ModbusRTU::ForceCoilsMessage msg(p->mbaddr,p->mbreg);
ModbusRTU::DataBits16 b(val);
msg.addData(b);
ModbusRTU::ForceCoilsRetMessage ret = mb->write0F(msg);
return true;
}
cerr << myname << "(writeReg): " << (int)p->mbfunc << endl;
return false;
}
// -----------------------------------------------------------------------------
void MBMaster::processingMessage(UniSetTypes::VoidMessage *msg) void MBMaster::processingMessage(UniSetTypes::VoidMessage *msg)
{ {
try try
...@@ -365,6 +429,13 @@ void MBMaster::sysCommand(UniSetTypes::SystemMessage *sm) ...@@ -365,6 +429,13 @@ void MBMaster::sysCommand(UniSetTypes::SystemMessage *sm)
{ {
case SystemMessage::StartUp: case SystemMessage::StartUp:
{ {
if( mbmap.empty() )
{
dlog[Debug::CRIT] << myname << "(sysCommand): mbmap EMPTY! terminated..." << endl;
raise(SIGTERM);
return;
}
waitSMReady(); waitSMReady();
// ģ // ģ
...@@ -461,9 +532,7 @@ void MBMaster::initOutput() ...@@ -461,9 +532,7 @@ void MBMaster::initOutput()
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
void MBMaster::askSensors( UniversalIO::UIOCommand cmd ) void MBMaster::askSensors( UniversalIO::UIOCommand cmd )
{ {
#warning testid if( !shm->waitSMworking(test_id,activateTimeout,50) )
UniSetTypes::ObjectId testid = 4100; // (notRespondSensor!=DefaultObjectId) ? notRespondSensor : 4100; //TestMode_S
if( !shm->waitSMworking(testid,activateTimeout,50) )
{ {
ostringstream err; ostringstream err;
err << myname err << myname
...@@ -498,7 +567,27 @@ void MBMaster::askSensors( UniversalIO::UIOCommand cmd ) ...@@ -498,7 +567,27 @@ void MBMaster::askSensors( UniversalIO::UIOCommand cmd )
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
void MBMaster::sensorInfo( UniSetTypes::SensorMessage* sm ) void MBMaster::sensorInfo( UniSetTypes::SensorMessage* sm )
{ {
MBMap::iterator it=mbmap.begin();
for( ; it!=mbmap.end(); ++it )
{
if( it->stype != UniversalIO::DigitalOutput && it->stype!=UniversalIO::AnalogOutput )
continue;
if( it->si.id == sm->id )
{
if( it->stype == UniversalIO::DigitalOutput )
{
uniset_spin_lock lock(it->val_lock);
it->value = sm->state ? 1 : 0;
}
else if( it->stype == UniversalIO::AnalogOutput )
{
uniset_spin_lock lock(it->val_lock);
it->value = sm->value;
}
break;
}
}
} }
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
bool MBMaster::activateObject() bool MBMaster::activateObject()
...@@ -600,28 +689,54 @@ bool MBMaster::readItem( UniXML& xml, UniXML_iterator& it, xmlNode* sec ) ...@@ -600,28 +689,54 @@ bool MBMaster::readItem( UniXML& xml, UniXML_iterator& it, xmlNode* sec )
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
bool MBMaster::initItem( UniXML_iterator& it ) bool MBMaster::initItem( UniXML_iterator& it )
{ {
cerr << "********** init item *************" << endl;
MBProperty p; MBProperty p;
if( !IOBase::initItem( static_cast<IOBase*>(&p),it,shm,&dlog,myname) ) if( !IOBase::initItem( static_cast<IOBase*>(&p),it,shm,&dlog,myname) )
return false; return false;
cerr << "********** init item(2) *************" << endl;
string addr = it.getProp("mbaddr"); string addr = it.getProp("mbaddr");
if( addr.empty() ) if( addr.empty() )
return true; return true;
if( mbregFromID )
p.mbreg = p.si.id;
else
{
string r = it.getProp("mbreg");
if( r.empty() )
{
dlog[Debug::CRIT] << myname << "(initItem): Unknown 'mbreg' for " << it.getProp("name") << endl;
return false;
}
p.mbreg = ModbusRTU::str2mbData(r);
}
p.mbaddr = ModbusRTU::str2mbAddr(addr); p.mbaddr = ModbusRTU::str2mbAddr(addr);
if( p.stype == UniversalIO::AnalogInput ) string stype( it.getProp("mb_iotype") );
if( stype.empty() )
stype = it.getProp("iotype");
if( stype == "AI" )
{
p.stype = UniversalIO::AnalogInput;
p.mbfunc = ModbusRTU::fnReadInputRegisters; p.mbfunc = ModbusRTU::fnReadInputRegisters;
else if ( p.stype == UniversalIO::DigitalInput ) }
else if ( stype == "DI" )
{
p.stype = UniversalIO::DigitalInput;
p.mbfunc = ModbusRTU::fnReadInputRegisters; p.mbfunc = ModbusRTU::fnReadInputRegisters;
else if ( p.stype == UniversalIO::AnalogOutput ) }
else if ( stype == "AO" )
{
p.stype = UniversalIO::AnalogOutput;
p.mbfunc = ModbusRTU::fnWriteOutputRegisters; p.mbfunc = ModbusRTU::fnWriteOutputRegisters;
else if ( p.stype == UniversalIO::DigitalOutput ) }
else if ( stype == "DO" )
{
p.stype = UniversalIO::DigitalOutput;
p.mbfunc = ModbusRTU::fnWriteOutputRegisters; p.mbfunc = ModbusRTU::fnWriteOutputRegisters;
}
string f = it.getProp("mbfunc"); string f = it.getProp("mbfunc");
if( !f.empty() ) if( !f.empty() )
...@@ -629,13 +744,33 @@ bool MBMaster::initItem( UniXML_iterator& it ) ...@@ -629,13 +744,33 @@ bool MBMaster::initItem( UniXML_iterator& it )
p.mbfunc = (ModbusRTU::SlaveFunctionCode)UniSetTypes::uni_atoi(f.c_str()); p.mbfunc = (ModbusRTU::SlaveFunctionCode)UniSetTypes::uni_atoi(f.c_str());
if( p.mbfunc == ModbusRTU::fnUnknown ) if( p.mbfunc == ModbusRTU::fnUnknown )
{ {
dlog[Debug::CRIT] << myname << "(initCommParam): mbfunc ='" << f dlog[Debug::CRIT] << myname << "(initItem): mbfunc ='" << f
<< "' " << it.getProp("name") << endl; << "' " << it.getProp("name") << endl;
return false; return false;
} }
} }
if( p.mbfunc == ModbusRTU::fnReadCoilStatus ||
p.mbfunc == ModbusRTU::fnReadInputStatus )
{
string nb = it.getProp("nbit");
if( nb.empty() )
{
dlog[Debug::CRIT] << myname << "(initItem): Unknown nbit. for "
<< it.getProp("name")
<< " mbfunc=" << p.mbfunc
<< endl;
return false;
}
p.nbit = UniSetTypes::uni_atoi(nb.c_str());
}
if( dlog.debugging(Debug::INFO) )
dlog[Debug::INFO] << myname << "(initItem): add " << p << endl;
// //
// 10 ( ) // 10 ( )
// resize // resize
......
...@@ -43,10 +43,11 @@ class MBMaster: ...@@ -43,10 +43,11 @@ class MBMaster:
ModbusRTU::ModbusAddr mbaddr; /*!< */ ModbusRTU::ModbusAddr mbaddr; /*!< */
ModbusRTU::ModbusData mbreg; /*!< */ ModbusRTU::ModbusData mbreg; /*!< */
ModbusRTU::SlaveFunctionCode mbfunc; /*!< / */ ModbusRTU::SlaveFunctionCode mbfunc; /*!< / */
short nbit; /*!< bit number (for func=[0x01,0x02]) */
MBProperty(): MBProperty():
mbaddr(0),mbreg(0), mbaddr(0),mbreg(0),
mbfunc(ModbusRTU::fnUnknown) mbfunc(ModbusRTU::fnUnknown),nbit(0)
{} {}
friend std::ostream& operator<<( std::ostream& os, MBProperty& p ); friend std::ostream& operator<<( std::ostream& os, MBProperty& p );
...@@ -80,7 +81,8 @@ class MBMaster: ...@@ -80,7 +81,8 @@ class MBMaster:
void askSensors( UniversalIO::UIOCommand cmd ); void askSensors( UniversalIO::UIOCommand cmd );
void initOutput(); void initOutput();
void waitSMReady(); void waitSMReady();
long callItem( MBMap::iterator& p ); long readReg( MBMap::iterator& p );
bool writeReg( MBMap::iterator& p, long val );
virtual bool activateObject(); virtual bool activateObject();
...@@ -100,6 +102,7 @@ class MBMaster: ...@@ -100,6 +102,7 @@ class MBMaster:
bool initPause; bool initPause;
UniSetTypes::uniset_mutex mutex_start; UniSetTypes::uniset_mutex mutex_start;
bool mbregFromID;
bool force; /*!< , SM, */ bool force; /*!< , SM, */
bool force_out; /*!< , */ bool force_out; /*!< , */
int polltime; /*!< , [] */ int polltime; /*!< , [] */
...@@ -113,6 +116,7 @@ class MBMaster: ...@@ -113,6 +116,7 @@ class MBMaster:
Trigger trTimeout; Trigger trTimeout;
PassiveTimer ptTimeout; PassiveTimer ptTimeout;
bool activated; bool activated;
int activateTimeout; int activateTimeout;
std::string prefix; std::string prefix;
......
#!/bin/sh #!/bin/sh
uniset-start.sh -f ./uniset-mbtcpmaster --mbtcp-name MBMaster1 --confile test.xml \ uniset-start.sh -f ./uniset-mbtcpmaster --mbtcp-name MBMaster1 --confile test.xml \
--dlog-add-levels info,crit,warn --mbtcp-reg-from-id 1 \ --dlog-add-levels info,crit,warn \
--mbtcp-iaddr 127.0.0.1 --mbtcp-port 30000 --mbtcp-iaddr 127.0.0.1 --mbtcp-port 2048 \
--mbtcp-filter-field mbtcp --mbtcp-filter-value 1
#--mbtcp-reg-from-id 1
\ No newline at end of file
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "PassiveTimer.h" #include "PassiveTimer.h"
#include "Trigger.h" #include "Trigger.h"
#include "Mutex.h" #include "Mutex.h"
#include "Calibration.h"
#include "SMInterface.h" #include "SMInterface.h"
#include "SharedMemory.h" #include "SharedMemory.h"
#include "IOBase.h" #include "IOBase.h"
...@@ -23,12 +22,13 @@ class MBSlave: ...@@ -23,12 +22,13 @@ class MBSlave:
public UniSetObject_LT public UniSetObject_LT
{ {
public: public:
MBSlave( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmID, SharedMemory* ic=0 ); MBSlave( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmID, SharedMemory* ic=0, std::string prefix="mbs" );
virtual ~MBSlave(); virtual ~MBSlave();
/*! */ /*! */
static MBSlave* init_mbslave( int argc, char* argv[], static MBSlave* init_mbslave( int argc, char* argv[],
UniSetTypes::ObjectId shmID, SharedMemory* ic=0 ); UniSetTypes::ObjectId shmID, SharedMemory* ic=0,
std::string prefix="mbs" );
/*! help- */ /*! help- */
static void help_print( int argc, char* argv[] ); static void help_print( int argc, char* argv[] );
...@@ -172,12 +172,12 @@ class MBSlave: ...@@ -172,12 +172,12 @@ class MBSlave:
bool pingOK; bool pingOK;
int wait_msec; int wait_msec;
bool force; /*!< , SM, */ bool force; /*!< , SM, */
bool force_out; /*!< , */
bool mbregFromID; bool mbregFromID;
typedef std::map<int,std::string> FileList; typedef std::map<int,std::string> FileList;
FileList flist; FileList flist;
std::string prefix;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#endif // _MBSlave_H_ #endif // _MBSlave_H_
......
#!/bin/sh #!/bin/sh
uniset-start.sh -f ./uniset-mbslave --mbs-name MBSlave1 --confile test.xml --dlog-add-levels info,crit,warn \ uniset-start.sh -f ./uniset-mbslave --mbs-name MBSlave1 --confile test.xml --dlog-add-levels info,crit,warn \
--mbs-type TCP --mbs-inet-addr 127.0.0.2 --mbs-inet-port 2048 --mbs-reg-from-id 1 --mbs-type TCP --mbs-inet-addr 127.0.0.1 --mbs-inet-port 2048 \
\ No newline at end of file --mbs-filter-field rs --mbs-filter-value 1
# --mbs-force 1
#--mbs-reg-from-id 1 \
\ No newline at end of file
...@@ -252,17 +252,19 @@ bool IOBase::initItem( IOBase* b, UniXML_iterator& it, SMInterface* shm, ...@@ -252,17 +252,19 @@ bool IOBase::initItem( IOBase* b, UniXML_iterator& it, SMInterface* shm,
{ {
string sname( it.getProp("name") ); string sname( it.getProp("name") );
ObjectId sid = UniSetTypes::uni_atoi(it.getProp("id").c_str()); ObjectId sid = DefaultObjectId;
if( sid <=0 ) if( it.getProp("id").empty() )
sid = conf->getSensorID(sname);
else
{ {
if( dlog ) sid = UniSetTypes::uni_atoi(it.getProp("id").c_str());
dlog[Debug::CRIT] << myname << "(readItem): ID : " if( sid <=0 )
<< sname << endl; sid = DefaultObjectId;
return false;
} }
if( sid == DefaultObjectId ) if( sid == DefaultObjectId )
{ {
if( dlog )
dlog[Debug::CRIT] << myname << "(readItem): (-1) ID : " dlog[Debug::CRIT] << myname << "(readItem): (-1) ID : "
<< sname << endl; << sname << endl;
return false; return false;
......
...@@ -439,16 +439,16 @@ void SMInterface::initAIterator( IOController::AIOStateList::iterator& it ) ...@@ -439,16 +439,16 @@ void SMInterface::initAIterator( IOController::AIOStateList::iterator& it )
{ {
if( ic ) if( ic )
it = ic->aioEnd(); it = ic->aioEnd();
else // else
cerr << "(SMInterface::initAIterator): ic=NULL" << endl; // cerr << "(SMInterface::initAIterator): ic=NULL" << endl;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
void SMInterface::initDIterator( IOController::DIOStateList::iterator& it ) void SMInterface::initDIterator( IOController::DIOStateList::iterator& it )
{ {
if( ic ) if( ic )
it = ic->dioEnd(); it = ic->dioEnd();
else // else
cerr << "(SMInterface::initDIterator): ic=NULL" << endl; // cerr << "(SMInterface::initDIterator): ic=NULL" << endl;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
bool SMInterface::waitSMready( int ready_timeout, int pmsec ) bool SMInterface::waitSMready( int ready_timeout, int pmsec )
......
...@@ -25,6 +25,8 @@ class ModbusTCPMaster: ...@@ -25,6 +25,8 @@ class ModbusTCPMaster:
protected: protected:
void reconnect();
virtual int getNextData( unsigned char* buf, int len ); virtual int getNextData( unsigned char* buf, int len );
virtual void setChannelTimeout( int msec ); virtual void setChannelTimeout( int msec );
virtual ModbusRTU::mbErrCode sendData( unsigned char* buf, int len ); virtual ModbusRTU::mbErrCode sendData( unsigned char* buf, int len );
...@@ -35,6 +37,8 @@ class ModbusTCPMaster: ...@@ -35,6 +37,8 @@ class ModbusTCPMaster:
ost::TCPStream* tcp; ost::TCPStream* tcp;
static int nTransaction; static int nTransaction;
std::queue<unsigned char> qrecv; std::queue<unsigned char> qrecv;
PassiveTimer ptTimeout;
std::string iaddr;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#endif // ModbusTCPMaster_H_ #endif // ModbusTCPMaster_H_
......
...@@ -13,7 +13,8 @@ using namespace ModbusRTU; ...@@ -13,7 +13,8 @@ using namespace ModbusRTU;
using namespace UniSetTypes; using namespace UniSetTypes;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
ModbusTCPMaster::ModbusTCPMaster(): ModbusTCPMaster::ModbusTCPMaster():
tcp(0) tcp(0),
iaddr("")
{ {
setCRCNoCheckit(true); setCRCNoCheckit(true);
} }
...@@ -46,13 +47,16 @@ int ModbusTCPMaster::nTransaction = 0; ...@@ -46,13 +47,16 @@ int ModbusTCPMaster::nTransaction = 0;
mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg, mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
ModbusMessage& reply, int timeout ) ModbusMessage& reply, int timeout )
{ {
if( !isConnection() )
// if( !isConnection() )
if( iaddr.empty() )
{ {
dlog[Debug::WARN] << "(query): not connection to server..." << endl; dlog[Debug::WARN] << "(query): not connection to server..." << endl;
return erHardwareError; return erHardwareError;
} }
PassiveTimer ptTimeout; reconnect();
if( timeout<=0 || timeout == UniSetTimer::WaitUpTime ) if( timeout<=0 || timeout == UniSetTimer::WaitUpTime )
{ {
...@@ -66,6 +70,9 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg, ...@@ -66,6 +70,9 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
try try
{ {
if( nTransaction >= numeric_limits<int>::max() )
nTransaction = 0;
ModbusTCP::MBAPHeader mh; ModbusTCP::MBAPHeader mh;
mh.tID = ++nTransaction; mh.tID = ++nTransaction;
mh.pID = 0; mh.pID = 0;
...@@ -159,6 +166,17 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg, ...@@ -159,6 +166,17 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
return erHardwareError; return erHardwareError;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void ModbusTCPMaster::reconnect()
{
if( tcp )
{
tcp->disconnect();
delete tcp;
}
tcp = new ost::TCPStream(iaddr.c_str());
}
// -------------------------------------------------------------------------
void ModbusTCPMaster::connect( ost::InetAddress addr, int port ) void ModbusTCPMaster::connect( ost::InetAddress addr, int port )
{ {
if( !tcp ) if( !tcp )
...@@ -169,7 +187,8 @@ void ModbusTCPMaster::connect( ost::InetAddress addr, int port ) ...@@ -169,7 +187,8 @@ void ModbusTCPMaster::connect( ost::InetAddress addr, int port )
if( dlog.debugging(Debug::INFO) ) if( dlog.debugging(Debug::INFO) )
dlog[Debug::INFO] << "(ModbusTCPMaster): connect to " << s.str() << endl; dlog[Debug::INFO] << "(ModbusTCPMaster): connect to " << s.str() << endl;
tcp = new ost::TCPStream(s.str().c_str()); iaddr = s.str();
tcp = new ost::TCPStream(iaddr.c_str());
} }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
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