Commit b9c92a58 authored by Pavel Vainerman's avatar Pavel Vainerman

(Modbus): рефакторинг кода и тестов, корректировка под 64bit

parent de872cfc
......@@ -505,6 +505,7 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
%exclude %_pkgconfigdir/libUniSet2.pc
# history of current unpublished changes
# Modbus: refactoring code and test (for 64bit)
%changelog
* Mon Dec 12 2016 Pavel Vainerman <pv@altlinux.ru> 2.6-alt6
......
// -------------------------------------------------------------------------
#include <sstream>
#include <limits>
#include <Poco/Net/NetException.h>
#include "UniSetTypes.h"
#include "MBTCPTestServer.h"
......@@ -23,7 +24,7 @@ MBTCPTestServer::MBTCPTestServer( const std::unordered_set<ModbusAddr>& _vaddr,
sslot(NULL),
vaddr(_vaddr),
verbose(verb),
replyVal(-1),
replyVal(std::numeric_limits<uint32_t>::max()),
forceSingleCoilCmd(false),
lastWriteOutputSingleRegister(0),
lastForceCoilsQ(0, 0),
......@@ -122,7 +123,7 @@ ModbusRTU::mbErrCode MBTCPTestServer::readCoilStatus( ReadCoilMessage& query,
if( query.count <= 1 )
{
if( replyVal != -1 )
if( replyVal != std::numeric_limits<uint32_t>::max() )
reply.addData(replyVal);
else
reply.addData(d);
......@@ -136,7 +137,7 @@ ModbusRTU::mbErrCode MBTCPTestServer::readCoilStatus( ReadCoilMessage& query,
for( ; num < query.count; num++, reg++ )
{
if( replyVal != -1 )
if( replyVal != std::numeric_limits<uint32_t>::max() )
reply.addData(replyVal);
else
reply.addData(d);
......@@ -167,7 +168,7 @@ ModbusRTU::mbErrCode MBTCPTestServer::readInputStatus( ReadInputStatusMessage& q
d.b[3] = 1;
d.b[7] = 1;
if( replyVal == -1 )
if( replyVal == std::numeric_limits<uint32_t>::max() )
{
size_t bnum = 0;
size_t i = 0;
......@@ -207,7 +208,7 @@ mbErrCode MBTCPTestServer::readInputRegisters( ReadInputMessage& query,
if( query.count <= 1 )
{
if( replyVal != -1 )
if( replyVal != std::numeric_limits<uint32_t>::max() )
reply.addData(replyVal);
else
reply.addData(query.start);
......@@ -221,7 +222,7 @@ mbErrCode MBTCPTestServer::readInputRegisters( ReadInputMessage& query,
for( ; num < query.count; num++, reg++ )
{
if( replyVal != -1 )
if( replyVal != std::numeric_limits<uint32_t>::max() )
reply.addData(replyVal);
else
reply.addData(reg);
......@@ -252,7 +253,7 @@ ModbusRTU::mbErrCode MBTCPTestServer::readOutputRegisters(
if( query.count <= 1 )
{
if( replyVal != -1 )
if( replyVal != std::numeric_limits<uint32_t>::max() )
reply.addData(replyVal);
else
reply.addData(query.start);
......@@ -266,7 +267,7 @@ ModbusRTU::mbErrCode MBTCPTestServer::readOutputRegisters(
for( ; num < query.count; num++, reg++ )
{
if( replyVal != -1 )
if( replyVal != std::numeric_limits<uint32_t>::max() )
reply.addData(replyVal);
else
reply.addData(reg);
......
......@@ -19,7 +19,7 @@ class MBTCPTestServer
verbose = state;
}
inline void setReply( long val )
inline void setReply( uint32_t val )
{
replyVal = val;
}
......@@ -47,7 +47,7 @@ class MBTCPTestServer
{
return forceSingleCoilCmd;
}
inline int getLastWriteOutputSingleRegister()
inline int16_t getLastWriteOutputSingleRegister()
{
return lastWriteOutputSingleRegister;
}
......@@ -131,9 +131,9 @@ class MBTCPTestServer
std::unordered_set<uniset::ModbusRTU::ModbusAddr> vaddr; /*!< адреса данного узла */
bool verbose;
long replyVal;
uint32_t replyVal;
bool forceSingleCoilCmd;
int lastWriteOutputSingleRegister;
int16_t lastWriteOutputSingleRegister;
uniset::ModbusRTU::ForceCoilsMessage lastForceCoilsQ;
uniset::ModbusRTU::WriteOutputMessage lastWriteOutputQ;
float f2_test_value = {0.0};
......
......@@ -15,7 +15,7 @@ cd -
--mbtcp-filter-value 1 \
--mbtcp-gateway-iaddr localhost \
--mbtcp-gateway-port 20048 \
--mbtcp-polltime 50 --mbtcp-recv-timeout 500
--mbtcp-polltime 50 --mbtcp-recv-timeout 500
#--mbtcp-log-add-levels any
#--mbtcp-default-mbinit-ok 1
#--dlog-add-levels any
......
......@@ -222,12 +222,12 @@ TEST_CASE("MBTCPMaster: 0x03 (read register outputs or memories or read word out
REQUIRE( ui->getValue(1004) == -10 );
REQUIRE( ui->getValue(1005) == -10 );
REQUIRE( ui->getValue(1006) == -10 );
mbs->setReply(0);
mbs->setReply(1);
msleep(polltime + 200);
REQUIRE( ui->getValue(1003) == 0 );
REQUIRE( ui->getValue(1004) == 0 );
REQUIRE( ui->getValue(1005) == 0 );
REQUIRE( ui->getValue(1006) == 0 );
REQUIRE( ui->getValue(1003) == 1 );
REQUIRE( ui->getValue(1004) == 1 );
REQUIRE( ui->getValue(1005) == 1 );
REQUIRE( ui->getValue(1006) == 1 );
mbs->setReply(65535);
msleep(polltime + 200);
REQUIRE( ui->getValue(1003) == -1 );
......@@ -236,10 +236,14 @@ TEST_CASE("MBTCPMaster: 0x03 (read register outputs or memories or read word out
REQUIRE( ui->getValue(1006) == -1 );
REQUIRE( ui->getValue(1007) == 65535 ); // unsigned
mbs->setReply(0xffff);
mbs->setReply( std::numeric_limits<uint16_t>::max() );
msleep(polltime + 200);
REQUIRE( (uint16_t)ui->getValue(1009) == std::numeric_limits<uint16_t>::max() ); // U2
mbs->setReply( std::numeric_limits<int16_t>::max() );
msleep(polltime + 200);
REQUIRE( ui->getValue(1008) == 0xffffffff ); // I2
REQUIRE( ui->getValue(1009) == 0xffffffff ); // U2
REQUIRE( (int16_t)ui->getValue(1008) == std::numeric_limits<int16_t>::max() ); // I2
mbs->setReply(0xff);
msleep(polltime + 200);
REQUIRE( ui->getValue(1008) == 0x00ff00ff ); // I2
......@@ -281,10 +285,14 @@ TEST_CASE("MBTCPMaster: 0x04 (read input registers or memories or read word outp
REQUIRE( ui->getValue(1013) == -1 );
REQUIRE( ui->getValue(1014) == 65535 ); // unsigned
mbs->setReply(0xffff);
mbs->setReply( std::numeric_limits<uint16_t>::max() );
msleep(polltime + 200);
REQUIRE( ui->getValue(1015) == 0xffffffff ); // I2
REQUIRE( ui->getValue(1016) == 0xffffffff ); // U2
REQUIRE( (uint16_t)ui->getValue(1009) == std::numeric_limits<uint16_t>::max() ); // U2
mbs->setReply( std::numeric_limits<int16_t>::max() );
msleep(polltime + 200);
REQUIRE( (int16_t)ui->getValue(1008) == std::numeric_limits<int16_t>::max() ); // I2
mbs->setReply(0xff);
msleep(polltime + 200);
REQUIRE( ui->getValue(1015) == 0x00ff00ff ); // I2
......@@ -457,23 +465,23 @@ TEST_CASE("MBTCPMaster: 0x10 (write register outputs or memories)", "[modbus][0x
REQUIRE( q.addr == slaveADDR );
REQUIRE( q.start == 31 );
REQUIRE( q.quant == 6 );
REQUIRE( q.data[0] == (unsigned short)(-100) );
REQUIRE( q.data[2] == (unsigned short)(-10) );
REQUIRE( q.data[3] == (unsigned short)(-32767) );
REQUIRE( q.data[0] == (uint16_t)(-100) );
REQUIRE( q.data[2] == (uint16_t)(-10) );
REQUIRE( q.data[3] == (uint16_t)(-32767) );
}
SECTION("I2")
{
ui->setValue(1023, 0xffffffff);
REQUIRE( ui->getValue(1023) == 0xffffffff );
ui->setValue(1023, std::numeric_limits<uint32_t>::max());
REQUIRE( (uint32_t)ui->getValue(1023) == std::numeric_limits<uint32_t>::max() );
msleep(polltime + 200);
ModbusRTU::WriteOutputMessage q = mbs->getLastWriteOutput();
REQUIRE( q.addr == slaveADDR );
REQUIRE( q.start == 31 );
REQUIRE( q.quant == 6 );
REQUIRE( q.data[4] == 0xffff );
REQUIRE( q.data[5] == 0xffff );
REQUIRE( q.data[4] == std::numeric_limits<uint16_t>::max() );
REQUIRE( q.data[5] == std::numeric_limits<uint16_t>::max() );
}
}
// -----------------------------------------------------------------------------
......
......@@ -127,6 +127,8 @@ TEST_CASE("MBTCPMultiMaster: rotate channel", "[modbus][mbmaster][mbtcpmultimast
InitTest();
CHECK( ui->isExist(mbID) );
mbs1->setReply(0);
msleep(polltime + 1000);
REQUIRE( ui->getValue(1003) == 0 );
mbs1->setReply(100);
mbs2->setReply(10);
......
......@@ -19,6 +19,7 @@
#define _RTUTypes_H_
// -----------------------------------------------------------------------------
#include <string>
#include <stdint.h>
#include <cmath>
#include <cstring>
#include <ostream>
......@@ -61,7 +62,7 @@ namespace uniset
/*! тип хранения в памяти */
typedef union
{
unsigned short v[f2Size];
uint16_t v[f2Size];
float val; //
} F2mem;
// ------------------------------------------
......@@ -147,7 +148,7 @@ namespace uniset
/*! тип хранения в памяти */
typedef union
{
unsigned short v[f4Size];
uint16_t v[f4Size];
float val; //
} F4mem;
// ------------------------------------------
......@@ -202,8 +203,8 @@ namespace uniset
/*! тип хранения в памяти */
typedef union
{
unsigned short w;
unsigned char b[bsize];
uint16_t w;
uint8_t b[bsize];
} Bytemem;
// ------------------------------------------
// конструкторы на разные случаи...
......@@ -212,15 +213,11 @@ namespace uniset
raw.w = 0;
}
Byte( unsigned char b1, unsigned char b2 ) noexcept
Byte( uint8_t b1, uint8_t b2 ) noexcept
{
raw.b[0] = b1;
raw.b[1] = b2;
}
Byte( const long& val ) noexcept
{
raw.w = val;
}
Byte( const ModbusRTU::ModbusData dat ) noexcept
{
......@@ -240,16 +237,12 @@ namespace uniset
return vtByte;
}
// ------------------------------------------
operator long()
{
return lroundf(raw.w);
}
operator unsigned short()
operator uint16_t()
{
return raw.w;
}
unsigned char operator[]( const int i )
uint8_t operator[]( const size_t i )
{
return raw.b[i];
}
......@@ -293,7 +286,7 @@ namespace uniset
return raw;
}
unsigned short raw;
uint16_t raw;
};
// --------------------------------------------------------------------------
class Signed
......@@ -332,7 +325,7 @@ namespace uniset
return raw;
}
signed short raw;
int16_t raw;
};
// --------------------------------------------------------------------------
class I2
......@@ -344,8 +337,8 @@ namespace uniset
/*! тип хранения в памяти */
typedef union
{
unsigned short v[i2Size];
int val; //
uint16_t v[i2Size];
int32_t val; //
} I2mem;
// ------------------------------------------
// конструкторы на разные случаи...
......@@ -354,7 +347,7 @@ namespace uniset
memset(raw.v, 0, sizeof(raw.v));
}
I2( int v ) noexcept
I2( int32_t v ) noexcept
{
raw.val = v;
}
......@@ -377,7 +370,7 @@ namespace uniset
return vtI2;
}
// ------------------------------------------
operator int()
operator int32_t()
{
return raw.val;
}
......@@ -394,7 +387,7 @@ namespace uniset
raw_backorder.val = 0;
}
I2r( const int v ) noexcept: I2(v)
I2r( const int32_t v ) noexcept: I2(v)
{
raw_backorder = raw;
std::swap(raw_backorder.v[0], raw_backorder.v[1]);
......@@ -421,8 +414,8 @@ namespace uniset
/*! тип хранения в памяти */
typedef union
{
unsigned short v[u2Size];
unsigned int val; //
uint16_t v[u2Size];
uint32_t val; //
} U2mem;
// ------------------------------------------
// конструкторы на разные случаи...
......@@ -431,7 +424,7 @@ namespace uniset
memset(raw.v, 0, sizeof(raw.v));
}
U2( unsigned int v ) noexcept
U2( uint32_t v ) noexcept
{
raw.val = v;
}
......@@ -454,11 +447,21 @@ namespace uniset
return vtU2;
}
// ------------------------------------------
operator unsigned int()
operator uint32_t()
{
return raw.val;
}
operator long()
{
return raw.val;
}
operator unsigned long()
{
return (uint32_t)raw.val;
}
U2mem raw;
};
// --------------------------------------------------------------------------
......@@ -471,7 +474,7 @@ namespace uniset
raw_backorder.val = 0;
}
U2r( int v ) noexcept: U2(v)
U2r( int32_t v ) noexcept: U2(v)
{
raw_backorder = raw;
std::swap(raw_backorder.v[0], raw_backorder.v[1]);
......
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