Commit a042ea14 authored by Pavel Vainerman's avatar Pavel Vainerman

(ModbusTCP): backport-тировал UTCPStream из uniset-2.0,

необходимый для корректной работы MBTCPMultiMaster
parent feac37eb
......@@ -8,7 +8,7 @@
Name: libuniset
Version: 1.7
Release: alt13
Release: alt14
Summary: UniSet - library for building distributed industrial control systems
......@@ -335,6 +335,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
%exclude %_pkgconfigdir/libUniSet.pc
%changelog
* Sat Jun 14 2014 Pavel Vainerman <pv@altlinux.ru> 1.7-alt14
- backport UTCPStream for MBTCPMultiMaster
* Tue Apr 29 2014 Pavel Vainerman <pv@altlinux.ru> 1.7-alt13
- (uniset-codegen): add "force" parameters for vartype="out"
......
......@@ -55,8 +55,9 @@
<item addr="0x01" invert="0" modeSensor="MB1_Mode_AS" respondSensor="RespondRTU_S" timeout="5000"/>
</DeviceList>
<GateList>
<item ip="127.0.0.1" port="2048" recv_timeout="200"/>
<item ip="127.0.0.1" port="2049" recv_timeout="200"/>
<item ip="192.168.56.60" port="2048" recv_timeout="200"/>
<item ip="10.10.2.1" port="2049" recv_timeout="200"/>
<item ip="localhost" port="2048" recv_timeout="200"/>
</GateList>
</MBMultiMaster1>
<MBSlave2 addr="0x3">
......
......@@ -13,7 +13,7 @@
--mbtcp-recv-timeout 3000 \
--mbtcp-timeout 2000 \
--mbtcp-force-disconnect 1 \
--mbtcp-polltime 3000 \
--mbtcp-polltime 100 \
--mbtcp-force-out 1 \
$*
......
......@@ -6,6 +6,7 @@
#include <cc++/socket.h>
#include "ModbusTypes.h"
#include "ModbusClient.h"
#include "UTCPStream.h"
// -------------------------------------------------------------------------
/*! Modbus TCP master interface */
class ModbusTCPMaster:
......@@ -42,11 +43,12 @@ class ModbusTCPMaster:
ModbusRTU::ModbusMessage& reply, timeout_t timeout );
private:
ost::TCPStream* tcp;
UTCPStream* tcp;
ModbusRTU::ModbusData nTransaction;
std::queue<unsigned char> qrecv;
PassiveTimer ptTimeout;
std::string iaddr;
int port;
bool force_disconnect;
};
// -------------------------------------------------------------------------
......
// -------------------------------------------------------------------------
#ifndef UTCPStream_H_
#define UTCPStream_H_
// -------------------------------------------------------------------------
#include <string>
#include <cc++/socket.h>
// -------------------------------------------------------------------------
class UTCPStream:
public ost::TCPStream
{
public:
UTCPStream();
virtual ~UTCPStream();
void create( const std::string& hname, int port, bool throwflag=false, timeout_t timer=0 );
protected:
private:
};
// -------------------------------------------------------------------------
#endif // UTCPStream_H_
// -------------------------------------------------------------------------
......@@ -5,7 +5,7 @@ noinst_LTLIBRARIES = libModbus.la
libModbus_la_SOURCES = ModbusTypes.cc ModbusHelpers.cc \
ModbusClient.cc ModbusServer.cc ModbusServerSlot.cc \
ModbusRTUSlave.cc ModbusRTUSlaveSlot.cc ModbusRTUMaster.cc \
ModbusTCPCore.cc ModbusTCPServer.cc ModbusTCPServerSlot.cc ModbusTCPMaster.cc
ModbusTCPCore.cc ModbusTCPServer.cc ModbusTCPServerSlot.cc ModbusTCPMaster.cc UTCPStream.cc
libModbus_la_CXXFLAGS = -I$(top_builddir)/include/Communications/modbus $(SIGC_CFLAGS) $(COMCPP_CFLAGS)
libModbus_la_LIBADD = $(SIGC_LIBS) $(COMCPP_LIBS)
......
......@@ -14,6 +14,7 @@ ModbusTCPMaster::ModbusTCPMaster():
tcp(0),
nTransaction(0),
iaddr(""),
port(0),
force_disconnect(true)
{
setCRCNoCheckit(true);
......@@ -272,18 +273,17 @@ void ModbusTCPMaster::cleanInputStream()
while( ret > 0);
}
// -------------------------------------------------------------------------
bool ModbusTCPMaster::checkConnection( const std::string ip, int port, int timeout_msec )
bool ModbusTCPMaster::checkConnection( const std::string ip, int _port, int timeout_msec )
{
try
{
ostringstream s;
s << ip << ":" << port;
s << ip << ":" << _port;
// Проверяем просто попыткой создать соединение..
// ost::Thread::setException(ost::Thread::throwException);
// TCPStream (const char *name, Family family=IPV4, unsigned mss=536, bool throwflag=false, timeout_t timer=0)
ost::TCPStream t(s.str().c_str(),ost::Socket::IPV4,536,true,timeout_msec);
UTCPStream t;
t.create(ip,_port,true,timeout_msec);
t.disconnect();
return true;
}
......@@ -311,10 +311,9 @@ void ModbusTCPMaster::reconnect()
try
{
// TCPStream (const char *name, Family family=IPV4, unsigned mss=536, bool throwflag=false, timeout_t timer=0)
tcp = new ost::TCPStream(iaddr.c_str(),ost::Socket::IPV4,536,true,500);
tcp = new UTCPStream();
tcp->create(iaddr,port,true,500);
tcp->setTimeout(replyTimeOut_ms);
tcp->setKeepAlive(true);
}
catch( std::exception& e )
{
......@@ -342,7 +341,7 @@ void ModbusTCPMaster::connect( const std::string addr, int port )
connect(ia,port);
}
// -------------------------------------------------------------------------
void ModbusTCPMaster::connect( ost::InetAddress addr, int port )
void ModbusTCPMaster::connect( ost::InetAddress addr, int _port )
{
if( tcp )
{
......@@ -354,19 +353,20 @@ void ModbusTCPMaster::connect( ost::InetAddress addr, int port )
// if( !tcp )
// {
ostringstream s;
s << addr << ":" << port;
s << addr;
if( dlog.debugging(Debug::INFO) )
dlog[Debug::INFO] << "(ModbusTCPMaster): connect to " << s.str() << endl;
dlog[Debug::INFO] << "(ModbusTCPMaster): connect to " << addr << ":" << _port << endl;
iaddr = s.str();
port = _port;
ost::Thread::setException(ost::Thread::throwException);
try
{
tcp = new ost::TCPStream(iaddr.c_str(),ost::Socket::IPV4,536,true,500);
tcp = new UTCPStream();
tcp->create(iaddr,port,true,500);
tcp->setTimeout(replyTimeOut_ms);
tcp->setKeepAlive(true);
}
catch( std::exception& e )
{
......
#include <iostream>
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <cstring>
#include <cc++/socket.h>
#include "modbus/UTCPStream.h"
#include "PassiveTimer.h"
#include "UniSetTypes.h"
// -------------------------------------------------------------------------
using namespace std;
// -------------------------------------------------------------------------
UTCPStream::~UTCPStream()
{
}
// -------------------------------------------------------------------------
UTCPStream::UTCPStream():
TCPStream(ost::Socket::IPV4,true)
{
}
// -------------------------------------------------------------------------
void UTCPStream::create( const std::string& hname, int port, bool throwflag, timeout_t t )
{
family = ost::Socket::IPV4;
timeout = t;
unsigned mss = 536;
setError(throwflag);
ost::IPV4Host h(hname.c_str());
connect(h,port,mss);
setKeepAlive(true);
setLinger(true);
//setCompletion(false);
}
// -------------------------------------------------------------------------
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