Commit ed3934ef authored by Pavel Vainerman's avatar Pavel Vainerman

add new command: --const-reply

parent d64f52ee
...@@ -16,7 +16,8 @@ MBSlave::MBSlave( ModbusRTU::ModbusAddr addr, const std::string dev, const std:: ...@@ -16,7 +16,8 @@ MBSlave::MBSlave( ModbusRTU::ModbusAddr addr, const std::string dev, const std::
addr(addr), addr(addr),
// prev(ModbusRTU::erNoError), // prev(ModbusRTU::erNoError),
// askCount(0), // askCount(0),
verbose(false) verbose(false),
replyVal(0)
{ {
cout << "$Id: MBSlave.cc,v 1.7 2009/02/24 20:27:24 vpashka Exp $" << endl; cout << "$Id: MBSlave.cc,v 1.7 2009/02/24 20:27:24 vpashka Exp $" << endl;
...@@ -109,7 +110,10 @@ ModbusRTU::mbErrCode MBSlave::readCoilStatus( ReadCoilMessage& query, ...@@ -109,7 +110,10 @@ ModbusRTU::mbErrCode MBSlave::readCoilStatus( ReadCoilMessage& query,
if( query.count <= 1 ) if( query.count <= 1 )
{ {
reply.addData(d); if( replyVal )
reply.addData(replyVal);
else
reply.addData(d);
return ModbusRTU::erNoError; return ModbusRTU::erNoError;
} }
...@@ -117,8 +121,12 @@ ModbusRTU::mbErrCode MBSlave::readCoilStatus( ReadCoilMessage& query, ...@@ -117,8 +121,12 @@ ModbusRTU::mbErrCode MBSlave::readCoilStatus( ReadCoilMessage& query,
int num=0; // int num=0; //
ModbusData reg = query.start; ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ ) for( ; num<query.count; num++, reg++ )
reply.addData(d); {
if( replyVal )
reply.addData(replyVal);
else
reply.addData(d);
}
// , ۣ // , ۣ
// ... // ...
if( reply.bcnt < query.count ) if( reply.bcnt < query.count )
...@@ -144,7 +152,10 @@ ModbusRTU::mbErrCode MBSlave::readInputStatus( ReadInputStatusMessage& query, ...@@ -144,7 +152,10 @@ ModbusRTU::mbErrCode MBSlave::readInputStatus( ReadInputStatusMessage& query,
if( query.count <= 1 ) if( query.count <= 1 )
{ {
reply.addData(d); if( replyVal )
reply.addData(replyVal);
else
reply.addData(d);
return ModbusRTU::erNoError; return ModbusRTU::erNoError;
} }
...@@ -152,8 +163,12 @@ ModbusRTU::mbErrCode MBSlave::readInputStatus( ReadInputStatusMessage& query, ...@@ -152,8 +163,12 @@ ModbusRTU::mbErrCode MBSlave::readInputStatus( ReadInputStatusMessage& query,
int num=0; // int num=0; //
ModbusData reg = query.start; ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ ) for( ; num<query.count; num++, reg++ )
reply.addData(d); {
if( replyVal )
reply.addData(replyVal);
else
reply.addData(d);
}
// , ۣ // , ۣ
// ... // ...
if( reply.bcnt < query.count ) if( reply.bcnt < query.count )
...@@ -173,7 +188,10 @@ mbErrCode MBSlave::readInputRegisters( ReadInputMessage& query, ...@@ -173,7 +188,10 @@ mbErrCode MBSlave::readInputRegisters( ReadInputMessage& query,
if( query.count <= 1 ) if( query.count <= 1 )
{ {
reply.addData(query.start); if( replyVal )
reply.addData(replyVal);
else
reply.addData(query.start);
return ModbusRTU::erNoError; return ModbusRTU::erNoError;
} }
...@@ -181,7 +199,12 @@ mbErrCode MBSlave::readInputRegisters( ReadInputMessage& query, ...@@ -181,7 +199,12 @@ mbErrCode MBSlave::readInputRegisters( ReadInputMessage& query,
int num=0; // int num=0; //
ModbusData reg = query.start; ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ ) for( ; num<query.count; num++, reg++ )
reply.addData(reg); {
if( replyVal )
reply.addData(replyVal);
else
reply.addData(reg);
}
// , ۣ // , ۣ
// ... // ...
...@@ -202,7 +225,10 @@ ModbusRTU::mbErrCode MBSlave::readOutputRegisters( ...@@ -202,7 +225,10 @@ ModbusRTU::mbErrCode MBSlave::readOutputRegisters(
if( query.count <= 1 ) if( query.count <= 1 )
{ {
reply.addData(query.start); if( replyVal )
reply.addData(replyVal);
else
reply.addData(query.start);
return ModbusRTU::erNoError; return ModbusRTU::erNoError;
} }
...@@ -210,8 +236,12 @@ ModbusRTU::mbErrCode MBSlave::readOutputRegisters( ...@@ -210,8 +236,12 @@ ModbusRTU::mbErrCode MBSlave::readOutputRegisters(
int num=0; // int num=0; //
ModbusData reg = query.start; ModbusData reg = query.start;
for( ; num<query.count; num++, reg++ ) for( ; num<query.count; num++, reg++ )
reply.addData(reg); {
if( replyVal )
reply.addData(replyVal);
else
reply.addData(reg);
}
// , ۣ // , ۣ
// ... // ...
if( reply.count < query.count ) if( reply.count < query.count )
......
...@@ -20,6 +20,11 @@ class MBSlave ...@@ -20,6 +20,11 @@ class MBSlave
verbose = state; verbose = state;
} }
inline void setReply( long val )
{
replyVal = val;
}
void execute(); /*!< */ void execute(); /*!< */
...@@ -96,7 +101,7 @@ class MBSlave ...@@ -96,7 +101,7 @@ class MBSlave
typedef std::map<int,std::string> FileList; typedef std::map<int,std::string> FileList;
FileList flist; FileList flist;
#endif #endif
long replyVal;
private: private:
}; };
......
...@@ -16,6 +16,7 @@ static struct option longopts[] = { ...@@ -16,6 +16,7 @@ static struct option longopts[] = {
{ "myaddr", required_argument, 0, 'a' }, { "myaddr", required_argument, 0, 'a' },
{ "speed", required_argument, 0, 's' }, { "speed", required_argument, 0, 's' },
{ "use485F", no_argument, 0, 'y' }, { "use485F", no_argument, 0, 'y' },
{ "const-reply", required_argument, 0, 'c' },
{ NULL, 0, 0, 0 } { NULL, 0, 0, 0 }
}; };
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -29,6 +30,7 @@ static void print_help() ...@@ -29,6 +30,7 @@ static void print_help()
printf("[-s|--speed] speed - 9600,14400,19200,38400,57600,115200. Default: 38400.\n"); printf("[-s|--speed] speed - 9600,14400,19200,38400,57600,115200. Default: 38400.\n");
printf("[-y|--use485F] - use RS485 Fastwel.\n"); printf("[-y|--use485F] - use RS485 Fastwel.\n");
printf("[-v|--verbose] - Print all messages to stdout\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 ) int main( int argc, char **argv )
...@@ -42,10 +44,11 @@ int main( int argc, char **argv ) ...@@ -42,10 +44,11 @@ int main( int argc, char **argv )
int tout = 2000; int tout = 2000;
DebugStream dlog; DebugStream dlog;
int use485 = 0; int use485 = 0;
int replyVal=0;
try try
{ {
while( (opt = getopt_long(argc, argv, "hva:d:s:y",longopts,&optindex)) != -1 ) while( (opt = getopt_long(argc, argv, "hva:d:s:yc:",longopts,&optindex)) != -1 )
{ {
switch (opt) switch (opt)
{ {
...@@ -77,6 +80,10 @@ int main( int argc, char **argv ) ...@@ -77,6 +80,10 @@ int main( int argc, char **argv )
use485 = 1; use485 = 1;
break; break;
case 'c':
replyVal = atoi(optarg);
break;
case '?': case '?':
default: default:
printf("? argumnet\n"); printf("? argumnet\n");
...@@ -97,6 +104,7 @@ int main( int argc, char **argv ) ...@@ -97,6 +104,7 @@ int main( int argc, char **argv )
MBSlave mbs(myaddr,dev,speed,use485); MBSlave mbs(myaddr,dev,speed,use485);
mbs.setLog(dlog); mbs.setLog(dlog);
mbs.setVerbose(verb); mbs.setVerbose(verb);
mbs.setReply(replyVal);
mbs.execute(); mbs.execute();
} }
catch( ModbusRTU::mbException& ex ) catch( ModbusRTU::mbException& ex )
......
...@@ -44,9 +44,10 @@ int main( int argc, char **argv ) ...@@ -44,9 +44,10 @@ int main( int argc, char **argv )
int tout = 2000; int tout = 2000;
DebugStream dlog; DebugStream dlog;
try try
{ {
while( (opt = getopt_long(argc, argv, "hva:d:s:",longopts,&optindex)) != -1 ) while( (opt = getopt_long(argc, argv, "hva:d:s:c:",longopts,&optindex)) != -1 )
{ {
switch (opt) switch (opt)
{ {
......
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