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