Commit 08f00add authored by Pavel Vainerman's avatar Pavel Vainerman

(ModbusTCPMaster): подправил вылавливание исключений..

parent dbe1f291
...@@ -112,7 +112,6 @@ std::shared_ptr<ModbusClient> MBTCPMaster::initMB( bool reopen ) ...@@ -112,7 +112,6 @@ std::shared_ptr<ModbusClient> MBTCPMaster::initMB( bool reopen )
mbtcp->setTimeout(recv_timeout); mbtcp->setTimeout(recv_timeout);
mbtcp->setSleepPause(sleepPause_usec); mbtcp->setSleepPause(sleepPause_usec);
mbtcp->setAfterSendPause(aftersend_pause); mbtcp->setAfterSendPause(aftersend_pause);
dinfo << myname << "(init): ipaddr=" << iaddr << " port=" << port << endl; dinfo << myname << "(init): ipaddr=" << iaddr << " port=" << port << endl;
...@@ -123,6 +122,14 @@ std::shared_ptr<ModbusClient> MBTCPMaster::initMB( bool reopen ) ...@@ -123,6 +122,14 @@ std::shared_ptr<ModbusClient> MBTCPMaster::initMB( bool reopen )
catch( ModbusRTU::mbException& ex ) catch( ModbusRTU::mbException& ex )
{ {
dwarn << "(init): " << ex << endl; dwarn << "(init): " << ex << endl;
mb = nullptr;
mbtcp = nullptr;
}
catch( const ost::Exception& e )
{
dwarn << myname << "(init): Can`t create socket " << iaddr << ":" << port << " err: " << e.getString() << endl;
mb = nullptr;
mbtcp = nullptr;
} }
catch(...) catch(...)
{ {
......
...@@ -278,7 +278,7 @@ bool MBTCPMultiMaster::MBSlaveInfo::init() ...@@ -278,7 +278,7 @@ bool MBTCPMultiMaster::MBSlaveInfo::init()
{ {
try try
{ {
// ost::Thread::setException(ost::Thread::throwException); ost::Thread::setException(ost::Thread::throwException);
dinfo << myname << "(init): connect..." << endl; dinfo << myname << "(init): connect..." << endl;
...@@ -288,7 +288,7 @@ bool MBTCPMultiMaster::MBSlaveInfo::init() ...@@ -288,7 +288,7 @@ bool MBTCPMultiMaster::MBSlaveInfo::init()
if( recv_timeout > 0 ) if( recv_timeout > 0 )
mbtcp->setTimeout(recv_timeout); mbtcp->setTimeout(recv_timeout);
// if( !initOK ) // if( !initOK )
{ {
mbtcp->setSleepPause(sleepPause_usec); mbtcp->setSleepPause(sleepPause_usec);
mbtcp->setAfterSendPause(aftersend_pause); mbtcp->setAfterSendPause(aftersend_pause);
...@@ -304,6 +304,10 @@ bool MBTCPMultiMaster::MBSlaveInfo::init() ...@@ -304,6 +304,10 @@ bool MBTCPMultiMaster::MBSlaveInfo::init()
{ {
dwarn << "(init): " << ex << endl; dwarn << "(init): " << ex << endl;
} }
catch( const ost::Exception& e )
{
dwarn << myname << "(init): Can`t create socket " << ip << ":" << port << " err: " << e.getString() << endl;
}
catch(...) catch(...)
{ {
dwarn << "(init): " << myname << " catch ..." << endl; dwarn << "(init): " << myname << " catch ..." << endl;
......
...@@ -33,7 +33,7 @@ MBTCPTestServer::MBTCPTestServer( ModbusAddr myaddr, const string& inetaddr, int ...@@ -33,7 +33,7 @@ MBTCPTestServer::MBTCPTestServer( ModbusAddr myaddr, const string& inetaddr, int
ost::InetAddress ia(inetaddr.c_str()); ost::InetAddress ia(inetaddr.c_str());
if( verbose ) if( verbose )
cout << "(init): " cout << "(MBTCPTestServer::init): "
<< " addr: " << ia << ":" << port << endl; << " addr: " << ia << ":" << port << endl;
{ {
...@@ -42,9 +42,24 @@ MBTCPTestServer::MBTCPTestServer( ModbusAddr myaddr, const string& inetaddr, int ...@@ -42,9 +42,24 @@ MBTCPTestServer::MBTCPTestServer( ModbusAddr myaddr, const string& inetaddr, int
myname = s.str(); myname = s.str();
} }
sslot = new ModbusTCPServerSlot(ia,port); try
{
// sslot->initLog(conf,name,logfile); ost::Thread::setException(ost::Thread::throwException);
sslot = new ModbusTCPServerSlot(ia,port);
}
catch( const ost::SockException& e )
{
ostringstream err;
err << "(MBTCPTestServer::init): Can`t create socket " << addr << ":" << port << " err: " << e.getString() << endl;
cerr << err.str() << endl;
throw SystemError(err.str());
}
catch( const std::exception& ex )
{
cerr << "(MBTCPTestServer::init): Can`t create socket " << addr << ":" << port << " err: " << ex.what() << endl;
throw ex;
}
// sslot->initLog(conf,name,logfile);
sslot->connectReadCoil( sigc::mem_fun(this, &MBTCPTestServer::readCoilStatus) ); sslot->connectReadCoil( sigc::mem_fun(this, &MBTCPTestServer::readCoilStatus) );
sslot->connectReadInputStatus( sigc::mem_fun(this, &MBTCPTestServer::readInputStatus) ); sslot->connectReadInputStatus( sigc::mem_fun(this, &MBTCPTestServer::readInputStatus) );
......
...@@ -37,8 +37,8 @@ ...@@ -37,8 +37,8 @@
<item addr="0x01" invert="1" respondSensor="Slave_Not_Respond_S" timeout="1000"/> <item addr="0x01" invert="1" respondSensor="Slave_Not_Respond_S" timeout="1000"/>
</DeviceList> </DeviceList>
<GateList> <GateList>
<item ip="127.0.0.1" port="20050" recv_timeout="200" invert="1" respondSensor="Slave1_Not_Respond_S"/> <item ip="127.0.0.1" port="20053" recv_timeout="200" invert="1" respondSensor="Slave1_Not_Respond_S"/>
<item ip="127.0.0.1" port="20052" recv_timeout="200" invert="1" respondSensor="Slave2_Not_Respond_S"/> <item ip="127.0.0.1" port="20055" recv_timeout="200" invert="1" respondSensor="Slave2_Not_Respond_S"/>
</GateList> </GateList>
</MBTCPMultiMaster1> </MBTCPMultiMaster1>
</settings> </settings>
......
...@@ -37,7 +37,22 @@ static void InitTest() ...@@ -37,7 +37,22 @@ static void InitTest()
if( !mbs ) if( !mbs )
{ {
mbs = make_shared<MBTCPTestServer>(slaveADDR,addr,port,false); try
{
mbs = make_shared<MBTCPTestServer>(slaveADDR,addr,port,false);
}
catch( const ost::SockException& e )
{
ostringstream err;
err << "(mbs): Can`t create socket " << addr << ":" << port << " err: " << e.getString() << endl;
cerr << err.str() << endl;
throw SystemError(err.str());
}
catch( const std::exception& ex )
{
cerr << "(mbs): Can`t create socket " << addr << ":" << port << " err: " << ex.what() << endl;
throw ex;
}
//mbs->setVerbose(true); //mbs->setVerbose(true);
CHECK( mbs!= nullptr ); CHECK( mbs!= nullptr );
mbs->runThread(); mbs->runThread();
......
...@@ -21,9 +21,9 @@ using namespace std; ...@@ -21,9 +21,9 @@ using namespace std;
using namespace UniSetTypes; using namespace UniSetTypes;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
static ModbusRTU::ModbusAddr slaveaddr = 0x01; // conf->getArgInt("--mbs-my-addr"); static ModbusRTU::ModbusAddr slaveaddr = 0x01; // conf->getArgInt("--mbs-my-addr");
static int port = 20050; // conf->getArgInt("--mbs-inet-port"); static int port = 20053; // conf->getArgInt("--mbs-inet-port");
static string addr("127.0.0.1"); // conf->getArgParam("--mbs-inet-addr"); static string addr("127.0.0.1"); // conf->getArgParam("--mbs-inet-addr");
static int port2 = 20052; static int port2 = 20055;
static string addr2("127.0.0.1"); static string addr2("127.0.0.1");
static ModbusRTU::ModbusAddr slaveADDR = 0x01; static ModbusRTU::ModbusAddr slaveADDR = 0x01;
static shared_ptr<MBTCPTestServer> mbs1; static shared_ptr<MBTCPTestServer> mbs1;
...@@ -52,7 +52,23 @@ static void InitTest() ...@@ -52,7 +52,23 @@ static void InitTest()
if( !mbs1 ) if( !mbs1 )
{ {
mbs1 = make_shared<MBTCPTestServer>(slaveADDR,addr,port,false); try
{
ost::Thread::setException(ost::Thread::throwException);
mbs1 = make_shared<MBTCPTestServer>(slaveADDR,addr,port,false);
}
catch( const ost::SockException& e )
{
ostringstream err;
err << "(mb1): Can`t create socket " << addr << ":" << port << " err: " << e.getString() << endl;
cerr << err.str() << endl;
throw SystemError(err.str());
}
catch( const std::exception& ex )
{
cerr << "(mb1): Can`t create socket " << addr << ":" << port << " err: " << ex.what() << endl;
throw ex;
}
CHECK( mbs1!= nullptr ); CHECK( mbs1!= nullptr );
mbs1->setReply(0); mbs1->setReply(0);
mbs1->runThread(); mbs1->runThread();
...@@ -65,7 +81,23 @@ static void InitTest() ...@@ -65,7 +81,23 @@ static void InitTest()
if( !mbs2 ) if( !mbs2 )
{ {
mbs2 = make_shared<MBTCPTestServer>(slaveADDR,addr2,port2,false); try
{
ost::Thread::setException(ost::Thread::throwException);
mbs2 = make_shared<MBTCPTestServer>(slaveADDR,addr2,port2,false);
}
catch( const ost::SockException& e )
{
ostringstream err;
err << "(mb2): Can`t create socket " << addr << ":" << port << " err: " << e.getString() << endl;
cerr << err.str() << endl;
throw SystemError(err.str());
}
catch( const std::exception& ex )
{
cerr << "(mb2): Can`t create socket " << addr << ":" << port << " err: " << ex.what() << endl;
throw ex;
}
CHECK( mbs2!= nullptr ); CHECK( mbs2!= nullptr );
mbs2->setReply(0); mbs2->setReply(0);
mbs2->runThread(); mbs2->runThread();
......
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