Commit 29b17a49 authored by Pavel Vainerman's avatar Pavel Vainerman

(Modbus): добавил для mbtcptester-а возможность указывать "константный" ответ на все запросы

parent 0214c443
......@@ -15,7 +15,8 @@ MBTCPServer::MBTCPServer( ModbusAddr myaddr, const string inetaddr, int port, bo
addr(myaddr),
// prev(ModbusRTU::erNoError),
// askCount(0),
verbose(verb)
verbose(verb),
replyVal(-1)
{
// int replyTimeout = uni_atoi( conf->getArgParam("--reply-timeout",it.getProp("reply_timeout")).c_str() );
// if( replyTimeout <= 0 )
......@@ -107,7 +108,10 @@ ModbusRTU::mbErrCode MBTCPServer::readCoilStatus( ReadCoilMessage& query,
if( query.count <= 1 )
{
reply.addData(d);
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(d);
return ModbusRTU::erNoError;
}
......@@ -115,7 +119,12 @@ ModbusRTU::mbErrCode MBTCPServer::readCoilStatus( ReadCoilMessage& query,
int num=0; // добавленное количество данных
ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ )
reply.addData(d);
{
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(d);
}
// Если мы в начале проверили, что запрос входит в разрешёный диапазон
// то теоретически этой ситуации возникнуть не может...
......@@ -142,7 +151,10 @@ ModbusRTU::mbErrCode MBTCPServer::readInputStatus( ReadInputStatusMessage& query
if( query.count <= 1 )
{
reply.addData(d);
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(d);
return ModbusRTU::erNoError;
}
......@@ -150,7 +162,12 @@ ModbusRTU::mbErrCode MBTCPServer::readInputStatus( ReadInputStatusMessage& query
int num=0; // добавленное количество данных
ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ )
reply.addData(d);
{
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(d);
}
// Если мы в начале проверили, что запрос входит в разрешёный диапазон
// то теоретически этой ситуации возникнуть не может...
......@@ -171,7 +188,11 @@ mbErrCode MBTCPServer::readInputRegisters( ReadInputMessage& query,
if( query.count <= 1 )
{
reply.addData(query.start);
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(query.start);
return ModbusRTU::erNoError;
}
......@@ -179,7 +200,12 @@ mbErrCode MBTCPServer::readInputRegisters( ReadInputMessage& query,
int num=0; // добавленное количество данных
ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ )
reply.addData(reg);
{
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(reg);
}
// cerr << "************ reply: cnt=" << reply.count << endl;
// cerr << "reply: " << reply << endl;
......@@ -203,7 +229,10 @@ ModbusRTU::mbErrCode MBTCPServer::readOutputRegisters(
if( query.count <= 1 )
{
reply.addData(query.start);
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(query.start);
return ModbusRTU::erNoError;
}
......@@ -211,8 +240,12 @@ ModbusRTU::mbErrCode MBTCPServer::readOutputRegisters(
int num=0; // добавленное количество данных
ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ )
reply.addData(reg);
{
if( replyVal!=-1 )
reply.addData(replyVal);
else
reply.addData(reg);
}
// Если мы в начале проверили, что запрос входит в разрешёный диапазон
// то теоретически этой ситуации возникнуть не может...
if( reply.count < query.count )
......
......@@ -18,6 +18,11 @@ class MBTCPServer
verbose = state;
}
inline void setReply( long val )
{
replyVal = val;
}
inline void setIgnoreAddrMode( bool state )
{
if( sslot )
......@@ -88,6 +93,7 @@ class MBTCPServer
ModbusRTU::ModbusAddr addr; /*!< адрес данного узла */
bool verbose;
long replyVal;
#if 0
typedef std::map<ModbusRTU::mbErrCode,unsigned int> ExchangeErrorMap;
ExchangeErrorMap errmap; /*!< статистика обмена */
......
......@@ -15,6 +15,7 @@ static struct option longopts[] = {
{ "myaddr", required_argument, 0, 'a' },
{ "port", required_argument, 0, 'p' },
{ "ignore-addr", no_argument, 0, 'x' },
{ "const-reply", required_argument, 0, 'c' },
{ NULL, 0, 0, 0 }
};
// --------------------------------------------------------------------------
......@@ -28,6 +29,7 @@ static void print_help()
printf("[-x|--ignore-addr] - Ignore modbus RTU-address.\n");
printf("[-p|--port] port - Server port. Default: 502.\n");
printf("[-v|--verbose] - Print all messages to stdout\n");
printf("[-c|--const-reply] val - Reply 'val' for all queries\n");
}
// --------------------------------------------------------------------------
int main( int argc, char **argv )
......@@ -41,12 +43,13 @@ int main( int argc, char **argv )
int tout = 2000;
DebugStream dlog;
bool ignoreAddr = false;
int replyVal=-1;
ost::Thread::setException(ost::Thread::throwException);
try
{
while( (opt = getopt_long(argc, argv, "ht:va:p:i:bx",longopts,&optindex)) != -1 )
while( (opt = getopt_long(argc, argv, "ht:va:p:i:bxc:",longopts,&optindex)) != -1 )
{
switch (opt)
{
......@@ -77,7 +80,11 @@ int main( int argc, char **argv )
case 'x':
ignoreAddr = true;
break;
case 'c':
replyVal = uni_atoi(optarg);
break;
case '?':
default:
printf("? argumnet\n");
......@@ -99,6 +106,8 @@ int main( int argc, char **argv )
mbs.setLog(dlog);
mbs.setVerbose(verb);
mbs.setIgnoreAddrMode(ignoreAddr);
if( replyVal!=-1 )
mbs.setReply(replyVal);
mbs.execute();
}
catch( ModbusRTU::mbException& ex )
......
......@@ -3,7 +3,7 @@
Name: libuniset
Version: 1.0
Release: alt53
Release: alt54
Summary: UniSet - library for building distributed industrial control systems
License: GPL
Group: Development/C++
......@@ -212,6 +212,9 @@ rm -f %buildroot%_libdir/*.la
%changelog
* Fri Nov 25 2011 Pavel Vainerman <pv@altlinux.ru> 1.0-alt54
- (modbus): added 'const-reply' for modbustcptester
* Fri Nov 25 2011 Pavel Vainerman <pv@altlinux.ru> 1.0-alt53
- (modbus): added information log
......
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