Commit 732fb855 authored by Pavel Vainerman's avatar Pavel Vainerman

Добавил режим игнорирования адреса (только для MmodbusTCPServer-а).

Позволяет при помощи uniset-mbtcpserver-echo имитировать "шлюзы" (один ip и много rtu-адресов)
parent a4db3837
...@@ -20,6 +20,12 @@ class MBTCPServer ...@@ -20,6 +20,12 @@ class MBTCPServer
verbose = state; verbose = state;
} }
inline void setIgnoreAddrMode( bool state )
{
if( sslot )
sslot->setIgnoreAddrMode(state);
}
void execute(); /*!< основной цикл работы */ void execute(); /*!< основной цикл работы */
void setLog( DebugStream& dlog ); void setLog( DebugStream& dlog );
......
...@@ -14,6 +14,7 @@ static struct option longopts[] = { ...@@ -14,6 +14,7 @@ static struct option longopts[] = {
{ "verbose", no_argument, 0, 'v' }, { "verbose", no_argument, 0, 'v' },
{ "myaddr", required_argument, 0, 'a' }, { "myaddr", required_argument, 0, 'a' },
{ "port", required_argument, 0, 'p' }, { "port", required_argument, 0, 'p' },
{ "ignore-addr", required_argument, 0, 'x' },
{ NULL, 0, 0, 0 } { NULL, 0, 0, 0 }
}; };
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -24,6 +25,7 @@ static void print_help() ...@@ -24,6 +25,7 @@ static void print_help()
printf("[-v|--verbose] - Print all messages to stdout\n"); printf("[-v|--verbose] - Print all messages to stdout\n");
printf("[-i|--iaddr] ip - Server listen ip. Default 127.0.0.1\n"); printf("[-i|--iaddr] ip - Server listen ip. Default 127.0.0.1\n");
printf("[-a|--myaddr] addr - Modbus address for master. Default: 0x01.\n"); printf("[-a|--myaddr] addr - Modbus address for master. Default: 0x01.\n");
printf("[-x|--ignore-addr] - Ignore modbus RTU-address.\n");
printf("[-p|--port] port - Server port. Default: 502.\n"); printf("[-p|--port] port - Server port. Default: 502.\n");
printf("[-v|--verbose] - Print all messages to stdout\n"); printf("[-v|--verbose] - Print all messages to stdout\n");
} }
...@@ -38,12 +40,13 @@ int main( int argc, char **argv ) ...@@ -38,12 +40,13 @@ int main( int argc, char **argv )
ModbusRTU::ModbusAddr myaddr = 0x01; ModbusRTU::ModbusAddr myaddr = 0x01;
int tout = 2000; int tout = 2000;
DebugStream dlog; DebugStream dlog;
bool ignoreAddr = false;
ost::Thread::setException(ost::Thread::throwException); ost::Thread::setException(ost::Thread::throwException);
try try
{ {
while( (opt = getopt_long(argc, argv, "ht:va:p:i:b",longopts,&optindex)) != -1 ) while( (opt = getopt_long(argc, argv, "ht:va:p:i:bx",longopts,&optindex)) != -1 )
{ {
switch (opt) switch (opt)
{ {
...@@ -70,6 +73,10 @@ int main( int argc, char **argv ) ...@@ -70,6 +73,10 @@ int main( int argc, char **argv )
case 'v': case 'v':
verb = 1; verb = 1;
break; break;
case 'x':
ignoreAddr = true;
break;
case '?': case '?':
default: default:
...@@ -91,6 +98,7 @@ int main( int argc, char **argv ) ...@@ -91,6 +98,7 @@ int main( int argc, char **argv )
MBTCPServer mbs(myaddr,iaddr,port,verb); MBTCPServer mbs(myaddr,iaddr,port,verb);
mbs.setLog(dlog); mbs.setLog(dlog);
mbs.setVerbose(verb); mbs.setVerbose(verb);
mbs.setIgnoreAddrMode(ignoreAddr);
mbs.execute(); mbs.execute();
} }
catch( ModbusRTU::mbException& ex ) catch( ModbusRTU::mbException& ex )
......
...@@ -24,6 +24,9 @@ class ModbusTCPServer: ...@@ -24,6 +24,9 @@ class ModbusTCPServer:
virtual ~ModbusTCPServer(); virtual ~ModbusTCPServer();
virtual ModbusRTU::mbErrCode receive( ModbusRTU::ModbusAddr addr, timeout_t msecTimeout ); virtual ModbusRTU::mbErrCode receive( ModbusRTU::ModbusAddr addr, timeout_t msecTimeout );
inline void setIgnoreAddrMode( bool st ){ ignoreAddr = st; }
inline bool getIgnoreAddrMode(){ return ignoreAddr; }
protected: protected:
...@@ -37,13 +40,14 @@ class ModbusTCPServer: ...@@ -37,13 +40,14 @@ class ModbusTCPServer:
virtual ModbusRTU::mbErrCode tcp_processing( ost::TCPStream& tcp, ModbusTCP::MBAPHeader& mhead ); virtual ModbusRTU::mbErrCode tcp_processing( ost::TCPStream& tcp, ModbusTCP::MBAPHeader& mhead );
ost::tpport_t port; ost::tpport_t port;
ost::TCPStream tcp; ost::TCPStream tcp;
ost::InetAddress iaddr; ost::InetAddress iaddr;
std::queue<unsigned char> qrecv; std::queue<unsigned char> qrecv;
ModbusTCP::MBAPHeader curQueryHeader; ModbusTCP::MBAPHeader curQueryHeader;
bool ignoreAddr;
private: private:
}; };
......
...@@ -1242,7 +1242,6 @@ mbErrCode ModbusServer::send( ModbusMessage& msg ) ...@@ -1242,7 +1242,6 @@ mbErrCode ModbusServer::send( ModbusMessage& msg )
return erHardwareError; return erHardwareError;
} }
// Пауза, чтобы не ловить свою посылку
if( aftersend_msec >= 0 ) if( aftersend_msec >= 0 )
msleep(aftersend_msec); msleep(aftersend_msec);
......
...@@ -10,7 +10,8 @@ using namespace UniSetTypes; ...@@ -10,7 +10,8 @@ using namespace UniSetTypes;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
ModbusTCPServer::ModbusTCPServer( ost::InetAddress &ia, int port ): ModbusTCPServer::ModbusTCPServer( ost::InetAddress &ia, int port ):
TCPSocket(ia,port), TCPSocket(ia,port),
iaddr(ia) iaddr(ia),
ignoreAddr(false)
{ {
setCRCNoCheckit(true); setCRCNoCheckit(true);
} }
...@@ -73,14 +74,19 @@ mbErrCode ModbusTCPServer::receive( ModbusRTU::ModbusAddr addr, timeout_t timeou ...@@ -73,14 +74,19 @@ mbErrCode ModbusTCPServer::receive( ModbusRTU::ModbusAddr addr, timeout_t timeou
{ {
// check addr // check addr
unsigned char _addr = qrecv.front(); unsigned char _addr = qrecv.front();
if( _addr != addr )
// для режима игнорирования RTU-адреса
// просто подменяем его на то который пришёл
// чтобы проверка всегда была успешной...
if( ignoreAddr )
addr = _addr;
else if( _addr != addr )
{ {
tmProcessing.setTiming(replyTimeout_ms); tmProcessing.setTiming(replyTimeout_ms);
ErrorRetMessage em( buf.addr, buf.func, erBadReplyNodeAddress ); ErrorRetMessage em( buf.addr, buf.func, erBadReplyNodeAddress );
buf = em.transport_msg(); buf = em.transport_msg();
send(buf); send(buf);
printProcessingTime(); printProcessingTime();
usleep(1000);
tcp.disconnect(); tcp.disconnect();
return res; return res;
} }
...@@ -97,8 +103,9 @@ mbErrCode ModbusTCPServer::receive( ModbusRTU::ModbusAddr addr, timeout_t timeou ...@@ -97,8 +103,9 @@ mbErrCode ModbusTCPServer::receive( ModbusRTU::ModbusAddr addr, timeout_t timeou
send(buf); send(buf);
printProcessingTime(); printProcessingTime();
} }
else if( aftersend_msec >= 0 )
msleep(aftersend_msec);
usleep(1000);
tcp.disconnect(); tcp.disconnect();
return res; return res;
} }
......
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