Commit 4fc054de authored by Pavel Vainerman's avatar Pavel Vainerman

(java): добился передачи сообщения об ошибке в java исключениях, доделал тестик UModbus

parent 24c33135
...@@ -668,24 +668,39 @@ include/UniXML.h ...@@ -668,24 +668,39 @@ include/UniXML.h
include/UTCPStream.h include/UTCPStream.h
include/WDTInterface.h include/WDTInterface.h
lib/Makefile.am lib/Makefile.am
python/examples/test.xml wrappers/python/examples/test.xml
python/lib/pyUniSet/Makefile.am wrappers/python/lib/pyUniSet/Makefile.am
python/lib/pyUniSet/PyUInterface.cc wrappers/python/lib/pyUniSet/PyUInterface.cc
python/lib/pyUniSet/PyUInterface.h wrappers/python/lib/pyUniSet/PyUInterface.h
python/lib/pyUniSet/UConnector.cc wrappers/python/lib/pyUniSet/UConnector.cc
python/lib/pyUniSet/UConnector.h wrappers/python/lib/pyUniSet/UConnector.h
python/lib/pyUniSet/UConnector_wrap.cxx wrappers/python/lib/pyUniSet/UConnector_wrap.cxx
python/lib/pyUniSet/UExceptions.h wrappers/python/lib/pyUniSet/UExceptions.h
python/lib/pyUniSet/UExceptions_wrap.cxx wrappers/python/lib/pyUniSet/UExceptions_wrap.cxx
python/lib/pyUniSet/UInterface_wrap.cxx wrappers/python/lib/pyUniSet/UInterface_wrap.cxx
python/lib/pyUniSet/UModbus.cc wrappers/python/lib/pyUniSet/UModbus.cc
python/lib/pyUniSet/UModbus.h wrappers/python/lib/pyUniSet/UModbus.h
python/lib/pyUniSet/UModbus_wrap.cxx wrappers/python/lib/pyUniSet/UModbus_wrap.cxx
python/lib/pyUniSet/UTypes.h wrappers/python/lib/pyUniSet/UTypes.h
python/lib/Makefile.am wrappers/python/lib/Makefile.am
python/tests/UConnector/test.xml wrappers/python/tests/UConnector/test.xml
python/tests/UInterface/test.xml wrappers/python/tests/UInterface/test.xml
python/Makefile.am wrappers/python/Makefile.am
wrappers/java/Makefile.am
wrappers/java/JUInterface.cc
wrappers/java/JUInterface.h
wrappers/java/Makefile.am
wrappers/java/runme_modbus.java
wrappers/java/UConnector.cc
wrappers/java/UConnector.h
wrappers/java/UConnector.i
wrappers/java/UExceptions.h
wrappers/java/UExceptions.i
wrappers/java/UInterface.i
wrappers/java/UModbus.cc
wrappers/java/UModbus.h
wrappers/java/UModbus.i
wrappers/java/UTypes.h
src/Communications/Modbus/Makefile.am src/Communications/Modbus/Makefile.am
src/Communications/Modbus/ModbusClient.cc src/Communications/Modbus/ModbusClient.cc
src/Communications/Modbus/ModbusHelpers.cc src/Communications/Modbus/ModbusHelpers.cc
......
...@@ -17,9 +17,10 @@ extensions/UNetUDP ...@@ -17,9 +17,10 @@ extensions/UNetUDP
extensions/UniNetwork extensions/UniNetwork
include include
include/modbus include/modbus
python/lib/pyUniSet wrappers/python/lib/pyUniSet
wrappers/java
src/Log src/Log
Utilities/codegen Utilities/codegen
Utilities/codegen/tests Utilities/codegen/tests
Utilities/MBTester Utilities/MBTester
Utilities/NullController Utilities/NullController
\ No newline at end of file
...@@ -10,13 +10,13 @@ using namespace std; ...@@ -10,13 +10,13 @@ using namespace std;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static UInterface* ui=0; static UInterface* ui=0;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void JUInterface::uniset_init_params( UTypes::Params* p, const char* xmlfile )throw(UException) void JUInterface::uniset_init_params( UTypes::Params* p, const char* xmlfile )
{ {
JUInterface::uniset_init(p->argc,p->argv,xmlfile); JUInterface::uniset_init(p->argc,p->argv,xmlfile);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void JUInterface::uniset_init( int argc, char* argv[], const char* xmlfile )throw(UException) void JUInterface::uniset_init( int argc, char* argv[], const char* xmlfile )
{ {
try try
{ {
...@@ -34,7 +34,7 @@ void JUInterface::uniset_init( int argc, char* argv[], const char* xmlfile )thro ...@@ -34,7 +34,7 @@ void JUInterface::uniset_init( int argc, char* argv[], const char* xmlfile )thro
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
long JUInterface::getValue( long id )throw(UException) long JUInterface::getValue( long id )
{ {
auto conf = UniSetTypes::uniset_conf(); auto conf = UniSetTypes::uniset_conf();
if( !conf || !ui ) if( !conf || !ui )
...@@ -76,7 +76,7 @@ long JUInterface::getValue( long id )throw(UException) ...@@ -76,7 +76,7 @@ long JUInterface::getValue( long id )throw(UException)
throw UException("(getValue): unknown error"); throw UException("(getValue): unknown error");
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void JUInterface::setValue( long id, long val )throw(UException) void JUInterface::setValue( long id, long val )
{ {
auto conf = UniSetTypes::uniset_conf(); auto conf = UniSetTypes::uniset_conf();
......
...@@ -7,12 +7,12 @@ ...@@ -7,12 +7,12 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace JUInterface namespace JUInterface
{ {
void uniset_init_params( UTypes::Params* p, const char* xmlfile )throw(UException); void uniset_init_params( UTypes::Params* p, const char* xmlfile );
void uniset_init( int argc, char** argv, const char* xmlfile )throw(UException); void uniset_init( int argc, char** argv, const char* xmlfile );
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
long getValue( long id )throw(UException); long getValue( long id );
void setValue( long id, long val )throw(UException); void setValue( long id, long val );
long getSensorID( const char* ); long getSensorID( const char* );
......
...@@ -2,11 +2,12 @@ if DISABLE_JAVA ...@@ -2,11 +2,12 @@ if DISABLE_JAVA
else else
JAVA_CFLAGS=-I/usr/lib/jdk/include -I/usr/lib/jdk/include/linux JAVA_CFLAGS=-I/usr/lib/jdk/include -I/usr/lib/jdk/include/linux
JAVA_LIBS=
#python_SCRIPTS = pyUniSet.java pyUConnector.java pyUModbus.java pyUExceptions.java #python_SCRIPTS = pyUniSet.java pyUConnector.java pyUModbus.java pyUExceptions.java
jUniSet.java: UInterface_wrap.cxx _jUniSet.la jUniSet.java: UInterface_wrap.cxx _jUInterface.la
jUConnector.java: UConnector_wrap.cxx _jUConnector.la jUConnector.java: UConnector_wrap.cxx _jUConnector.la
...@@ -18,12 +19,12 @@ jUModbus.java: _jUModbus.la UModbus_wrap.cxx ...@@ -18,12 +19,12 @@ jUModbus.java: _jUModbus.la UModbus_wrap.cxx
jUExceptions.java: _jUExceptions.la jUExceptions.java: _jUExceptions.la
lib_LTLIBRARIES = _jUConnector.la _jUModbus.la _jUExceptions.la _jUniSet.la lib_LTLIBRARIES = _jUConnector.la _jUModbus.la _jUExceptions.la _jUInterface.la
_jUniSet_la_SOURCES = JUInterface.cc UInterface_wrap.cxx _jUInterface_la_SOURCES = JUInterface.cc UInterface_wrap.cxx
_jUniSet_la_CXXFLAGS = $(UNISET_CFLAGS) $(JAVA_CFLAGS) _jUInterface_la_CXXFLAGS = $(UNISET_CFLAGS) $(JAVA_CFLAGS)
_jUniSet_la_LDFLAGS = -module -avoid-version _jUInterface_la_LDFLAGS = -module -avoid-version
_jUniSet_la_LIBADD = $(UNISET_LIBS) $(JAVA_LIBS) _jUInterface_la_LIBADD = $(UNISET_LIBS) $(JAVA_LIBS)
UInterface_wrap.cxx: UInterface.i JUInterface.h UInterface_wrap.cxx: UInterface.i JUInterface.h
swig -java -c++ UInterface.i swig -java -c++ UInterface.i
...@@ -41,7 +42,7 @@ _jUModbus_la_CXXFLAGS = $(UNISET_CFLAGS) $(UNISET_EXT_CFLAGS) $(COMCPP_CFLAGS) $ ...@@ -41,7 +42,7 @@ _jUModbus_la_CXXFLAGS = $(UNISET_CFLAGS) $(UNISET_EXT_CFLAGS) $(COMCPP_CFLAGS) $
_jUModbus_la_LDFLAGS = -module -avoid-version _jUModbus_la_LDFLAGS = -module -avoid-version
_jUModbus_la_LIBADD = $(UNISET_LIBS) $(UNISET_EXT_LIBS) $(COMCPP_LIBS) $(JAVA_LIBS) _jUModbus_la_LIBADD = $(UNISET_LIBS) $(UNISET_EXT_LIBS) $(COMCPP_LIBS) $(JAVA_LIBS)
UModbus_wrap.cxx: UInterface.i UModbus.h UModbus_wrap.cxx: UInterface.i UModbus.h UModbus.i
swig -java -c++ UModbus.i swig -java -c++ UModbus.i
_jUExceptions_la_SOURCES = UExceptions_wrap.cxx _jUExceptions_la_SOURCES = UExceptions_wrap.cxx
...@@ -53,6 +54,8 @@ UExceptions_wrap.cxx: UExceptions.i UExceptions.h ...@@ -53,6 +54,8 @@ UExceptions_wrap.cxx: UExceptions.i UExceptions.h
swig -java -c++ UExceptions.i swig -java -c++ UExceptions.i
clean-local: clean-local:
rm -rf *.java* *_wrap.cxx *.class rm -rf *_wrap.cxx *.class
rm -rf `ls *.java | $(GREP) -v runme*`
endif endif
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
using namespace std; using namespace std;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
UConnector::UConnector( UTypes::Params* p, const char* xfile )throw(UException): UConnector::UConnector( UTypes::Params* p, const char* xfile ):
conf(0), conf(0),
ui(0), ui(0),
xmlfile(xfile) xmlfile(xfile)
...@@ -23,7 +23,7 @@ xmlfile(xfile) ...@@ -23,7 +23,7 @@ xmlfile(xfile)
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
UConnector::UConnector( int argc, char** argv, const char* xfile )throw(UException): UConnector::UConnector( int argc, char** argv, const char* xfile ):
conf(0), conf(0),
ui(0), ui(0),
xmlfile(xfile) xmlfile(xfile)
...@@ -57,7 +57,7 @@ const char* UConnector::getConfFileName() ...@@ -57,7 +57,7 @@ const char* UConnector::getConfFileName()
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
long UConnector::getValue( long id, long node )throw(UException) long UConnector::getValue( long id, long node )
{ {
if( !conf || !ui ) if( !conf || !ui )
throw USysError(); throw USysError();
...@@ -81,7 +81,7 @@ long UConnector::getValue( long id, long node )throw(UException) ...@@ -81,7 +81,7 @@ long UConnector::getValue( long id, long node )throw(UException)
throw UException("(getValue): unknown error"); throw UException("(getValue): unknown error");
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void UConnector::setValue( long id, long val, long node )throw(UException) void UConnector::setValue( long id, long val, long node )
{ {
if( !conf || !ui ) if( !conf || !ui )
throw USysError(); throw USysError();
......
...@@ -11,15 +11,15 @@ ...@@ -11,15 +11,15 @@
class UConnector class UConnector
{ {
public: public:
UConnector( int argc, char** argv, const char* xmlfile )throw(UException); UConnector( int argc, char** argv, const char* xmlfile );
UConnector( UTypes::Params* p, const char* xmlfile )throw(UException); UConnector( UTypes::Params* p, const char* xmlfile );
~UConnector(); ~UConnector();
inline const char* getUIType(){ return "uniset"; } inline const char* getUIType(){ return "uniset"; }
const char* getConfFileName(); const char* getConfFileName();
long getValue( long id, long node )throw(UException); long getValue( long id, long node );
void setValue( long id, long val, long node )throw(UException); void setValue( long id, long val, long node );
long getSensorID( const char* ); long getSensorID( const char* );
long getNodeID( const char* ); long getNodeID( const char* );
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
%} %}
%include "UTypes.h" %include "UTypes.h"
%include UExceptions.i
%include "UConnector.h" %include "UConnector.h"
%pragma(java) jniclasscode=%{ %pragma(java) jniclasscode=%{
...@@ -15,4 +17,4 @@ ...@@ -15,4 +17,4 @@
System.exit(1); System.exit(1);
} }
} }
%} %}
\ No newline at end of file
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#include <string> #include <string>
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
struct UException: struct UException
public std::exception // : public std::exception
{ {
UException(){} UException(){}
UException( const std::string& e ):err(e){} UException( const std::string& e ):err(e){}
...@@ -12,7 +12,7 @@ struct UException: ...@@ -12,7 +12,7 @@ struct UException:
virtual ~UException() noexcept {} virtual ~UException() noexcept {}
const char* getError(){ return err.c_str(); } const char* getError(){ return err.c_str(); }
virtual const char* what() const noexcept { return err.c_str(); } // virtual const char* what() const noexcept { return err.c_str(); }
std::string err; std::string err;
}; };
......
...@@ -4,6 +4,25 @@ ...@@ -4,6 +4,25 @@
%} %}
%include "UExceptions.h" %include "UExceptions.h"
%exception {
try {
$action
}
catch( const UException& e ) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.err.c_str());
}
catch (const std::exception & e)
{
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException,std::string(std::string("C++ std::exception: ") + e.what()).c_str());
}
catch (...)
{
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException,"C++ anonymous exception");
}
};
/* /*
%pragma(java) jniclasscode=%{ %pragma(java) jniclasscode=%{
static { static {
...@@ -16,56 +35,3 @@ ...@@ -16,56 +35,3 @@
} }
%} %}
*/ */
/*
// Allow C++ exceptions to be handled in Java
%typemap(throws, throws="java.io.IOException") CustomException {
jclass excep = jenv->FindClass("java/io/IOException");
if (excep)
jenv->ThrowNew(excep, $1.what());
return $null;
}
// Force the CustomException Java class to extend java.lang.Exception
%typemap(javabase) CustomException "java.lang.Exception";
// Override getMessage()
%typemap(javacode) CustomException %{
public String getMessage() {
return what();
}
%}
*/
/*
%define WRAP_THROW_EXCEPTION( MATCH, CPPTYPE, JTYPE, JNITYPE )
%javaexception(JTYPE) MATCH {
try {
$action
}
catch ( CPPTYPE & e ) {
jclass eclass = jenv->FindClass(JNITYPE);
if ( eclass ) {
jenv->ThrowNew( eclass, e.what() );
}
}
}
%enddef
// Override getMessage()
%typemap(javacode) UException %{
public String getMessage() {
return getError();
}
%}
%typemap(javacode) UTimeout %{
public String getMessage() {
return getError();
}
%}
WRAP_THROW_EXCEPTION( UModbus::prepare, UTimeOut,"java/lang/Exception","java/lang/Exception");
WRAP_THROW_EXCEPTION( UModbus::mbread, UException,"java/lang/Exception","java/lang/Exception");
WRAP_THROW_EXCEPTION( UModbus::getWord, UException,"java/lang/Exception","java/lang/Exception");
*/
\ No newline at end of file
%module UniSet %module mUInterface
%{ %{
#include "JUInterface.h" #include "JUInterface.h"
%} %}
%include "JUInterface.h"
%include "UTypes.h" %include "UTypes.h"
%include "UExceptions.h" %include "UExceptions.h"
%include UExceptions.i
%include "JUInterface.h"
%pragma(java) jniclasscode=%{ %pragma(java) jniclasscode=%{
static { static {
try { try {
System.loadLibrary("UniSet"); System.loadLibrary("mUInterface");
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e); System.err.println("Native code library failed to load. \n" + e);
System.exit(1); System.exit(1);
......
#include <ostream> #include <ostream>
#include <cmath> #include <cmath>
#include <jni.h>
#include "UModbus.h" #include "UModbus.h"
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
using namespace std; using namespace std;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#if 0
UModbus::UModbus( UTypes::Params* p )throw(UException):
mb(0),
port(502),
ip(""),
tout_msec(5000)
{
try
{
mb = new ModbusTCPMaster();
}
catch(...)
{
throw UException();
}
}
//---------------------------------------------------------------------------
UModbus::UModbus( int argc, char** argv )throw(UException):
mb(0),
port(502),
ip(""),
tout_msec(5000)
{
try
{
mb = new ModbusTCPMaster();
}
catch(...)
{
throw UException();
}
}
#endif
// --------------------------------------------------------------------------
UModbus::UModbus(): UModbus::UModbus():
mb(0), mb(0),
port(502), port(502),
...@@ -52,7 +19,7 @@ UModbus::~UModbus() ...@@ -52,7 +19,7 @@ UModbus::~UModbus()
delete mb; delete mb;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
void UModbus::prepare( const char* _ip, int _port )throw(UException) void UModbus::prepare( const char* _ip, int _port )
{ {
if( !mb ) if( !mb )
throw UException("(connect): mb=NULL?!"); throw UException("(connect): mb=NULL?!");
...@@ -72,7 +39,7 @@ void UModbus::prepare( const char* _ip, int _port )throw(UException) ...@@ -72,7 +39,7 @@ void UModbus::prepare( const char* _ip, int _port )throw(UException)
port = _port; port = _port;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
void UModbus::connect( const char* _ip, int _port )throw(UException) void UModbus::connect( const char* _ip, int _port )
{ {
if( !mb ) if( !mb )
throw UException("(connect): mb=NULL?!"); throw UException("(connect): mb=NULL?!");
...@@ -108,23 +75,23 @@ void UModbus::connect( const char* _ip, int _port )throw(UException) ...@@ -108,23 +75,23 @@ void UModbus::connect( const char* _ip, int _port )throw(UException)
} }
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
long UModbus::getWord( int addr, int mbreg, int mbfunc )throw(UException) long UModbus::getWord( int addr, int mbreg, int mbfunc )
{ {
return mbread(addr,mbreg,mbfunc,"signed"); return mbread(addr,mbreg,mbfunc,"signed");
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
long UModbus::getByte( int addr, int mbreg, int mbfunc )throw(UException) long UModbus::getByte( int addr, int mbreg, int mbfunc )
{ {
return mbread(addr,mbreg,mbfunc,"byte"); return mbread(addr,mbreg,mbfunc,"byte");
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
bool UModbus::getBit( int addr, int mbreg, int mbfunc )throw(UException) bool UModbus::getBit( int addr, int mbreg, int mbfunc )
{ {
return mbread(addr,mbreg,mbfunc,"unsigned"); return mbread(addr,mbreg,mbfunc,"unsigned");
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
long UModbus::mbread( int mbaddr, int mbreg, int mbfunc, const char* s_vtype, int nbit, long UModbus::mbread( int mbaddr, int mbreg, int mbfunc, const char* s_vtype, int nbit,
const char* new_ip, int new_port )throw(UException) const char* new_ip, int new_port )
{ {
using namespace VTypes; using namespace VTypes;
...@@ -202,6 +169,13 @@ long UModbus::mbread( int mbaddr, int mbreg, int mbfunc, const char* s_vtype, in ...@@ -202,6 +169,13 @@ long UModbus::mbread( int mbaddr, int mbreg, int mbfunc, const char* s_vtype, in
} }
catch( ModbusRTU::mbException& ex ) catch( ModbusRTU::mbException& ex )
{ {
// jclass eclass = JNIEnv.FindClass("java/lang/IOException");
// jclass eclass = env->FindClass("java/lang/IOException");
// if ( eclass ) {
// JNIEnv->ThrowNew( eclass, ex.what() );
// }
if( ex.err != ModbusRTU::erTimeOut ) if( ex.err != ModbusRTU::erTimeOut )
throw UTimeOut(); throw UTimeOut();
...@@ -209,6 +183,12 @@ long UModbus::mbread( int mbaddr, int mbreg, int mbfunc, const char* s_vtype, in ...@@ -209,6 +183,12 @@ long UModbus::mbread( int mbaddr, int mbreg, int mbfunc, const char* s_vtype, in
err << ex; err << ex;
throw UException(err.str()); throw UException(err.str());
} }
catch( const std::exception& ex )
{
std::ostringstream err;
err << ex.what();
throw UException(err.str());
}
catch(...) catch(...)
{ {
throw UException("(mbread): catch..."); throw UException("(mbread): catch...");
...@@ -279,8 +259,15 @@ long UModbus::data2value( VTypes::VType vtype, ModbusRTU::ModbusData* data ) ...@@ -279,8 +259,15 @@ long UModbus::data2value( VTypes::VType vtype, ModbusRTU::ModbusData* data )
return 0; return 0;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* new_ip, int new_port )throw(UException) bool UModbus::setWord( int addr, int mbreg, int val, int mbfunc )
{ {
return mbwrite(addr,mbreg,val,mbfunc,0,0);
}
//---------------------------------------------------------------------------
bool UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* new_ip, int new_port )
{
// ost::Thread::setException(ost::Thread::throwException);
const char* n_ip = (new_ip != 0) ? new_ip : ip.c_str(); const char* n_ip = (new_ip != 0) ? new_ip : ip.c_str();
int n_port = ( new_port > 0 ) ? new_port : port; int n_port = ( new_port > 0 ) ? new_port : port;
...@@ -293,6 +280,7 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n ...@@ -293,6 +280,7 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n
case ModbusRTU::fnWriteOutputSingleRegister: case ModbusRTU::fnWriteOutputSingleRegister:
{ {
ModbusRTU::WriteSingleOutputRetMessage ret = mb->write06(mbaddr,mbreg,val); ModbusRTU::WriteSingleOutputRetMessage ret = mb->write06(mbaddr,mbreg,val);
return ( ret.start == mbreg && ret.data==val );
} }
break; break;
...@@ -301,12 +289,14 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n ...@@ -301,12 +289,14 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n
ModbusRTU::WriteOutputMessage msg(mbaddr,mbreg); ModbusRTU::WriteOutputMessage msg(mbaddr,mbreg);
msg.addData(val); msg.addData(val);
ModbusRTU::WriteOutputRetMessage ret = mb->write10(msg); ModbusRTU::WriteOutputRetMessage ret = mb->write10(msg);
return ( ret.start == mbreg && ret.quant==1 );
} }
break; break;
case ModbusRTU::fnForceSingleCoil: case ModbusRTU::fnForceSingleCoil:
{ {
ModbusRTU::ForceSingleCoilRetMessage ret = mb->write05(mbaddr,mbreg,val); ModbusRTU::ForceSingleCoilRetMessage ret = mb->write05(mbaddr,mbreg,val);
return ( ret.start == mbreg && ret.data == val );
} }
break; break;
...@@ -315,6 +305,7 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n ...@@ -315,6 +305,7 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n
ModbusRTU::ForceCoilsMessage msg(mbaddr,mbreg); ModbusRTU::ForceCoilsMessage msg(mbaddr,mbreg);
msg.addBit( (val ? true : false) ); msg.addBit( (val ? true : false) );
ModbusRTU::ForceCoilsRetMessage ret = mb->write0F(msg); ModbusRTU::ForceCoilsRetMessage ret = mb->write0F(msg);
return ( ret.start == mbreg && ret.quant==1 );
} }
break; break;
...@@ -327,7 +318,7 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n ...@@ -327,7 +318,7 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n
break; break;
} }
} }
catch( ModbusRTU::mbException& ex ) catch( const ModbusRTU::mbException& ex )
{ {
if( ex.err != ModbusRTU::erTimeOut ) if( ex.err != ModbusRTU::erTimeOut )
throw UTimeOut(); throw UTimeOut();
...@@ -336,9 +327,17 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n ...@@ -336,9 +327,17 @@ void UModbus::mbwrite( int mbaddr, int mbreg, int val, int mbfunc, const char* n
err << ex; err << ex;
throw UException(err.str()); throw UException(err.str());
} }
catch( const std::exception& ex )
{
std::ostringstream err;
err << ex.what();
throw UException(err.str());
}
catch(...) catch(...)
{ {
throw UException("(mbwrite): catch..."); throw UException("(mbwrite): catch...");
} }
return false;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
class UModbus class UModbus
{ {
public: public:
// UModbus( int argc, char** argv )throw(UException);
// UModbus( UTypes::Params* p )throw(UException);
UModbus(); UModbus();
~UModbus(); ~UModbus();
...@@ -23,9 +21,9 @@ class UModbus ...@@ -23,9 +21,9 @@ class UModbus
inline bool isWriteFunction( int mbfunc ){ return ModbusRTU::isWriteFunction((ModbusRTU::SlaveFunctionCode)mbfunc); } inline bool isWriteFunction( int mbfunc ){ return ModbusRTU::isWriteFunction((ModbusRTU::SlaveFunctionCode)mbfunc); }
// выставление паметров связи, без установления соединения (!) // выставление паметров связи, без установления соединения (!)
void prepare( const char* ip, int port )throw(UException); void prepare( const char* ip, int port );
void connect( const char* ip, int port )throw(UException); void connect( const char* ip, int port );
inline int conn_port(){ return port; } inline int conn_port(){ return port; }
inline std::string conn_ip(){ return ip; } inline std::string conn_ip(){ return ip; }
inline bool isConnection(){ return (mb && mb->isConnection()); } inline bool isConnection(){ return (mb && mb->isConnection()); }
...@@ -39,18 +37,21 @@ class UModbus ...@@ -39,18 +37,21 @@ class UModbus
*/ */
long mbread( int addr, int mbreg, int mbfunc, long mbread( int addr, int mbreg, int mbfunc,
const char* vtype, int nbit=-1, const char* vtype, int nbit=-1,
const char* ip=0, int port=-1 )throw(UException); const char* ip=0, int port=-1 );
long getWord( int addr, int mbreg, int mbfunc=0x4 )throw(UException); long getWord( int addr, int mbreg, int mbfunc=0x4 );
long getByte( int addr, int mbreg, int mbfunc=0x4 )throw(UException); long getByte( int addr, int mbreg, int mbfunc=0x4 );
bool getBit( int addr, int mbreg, int mbfunc=0x2 )throw(UException); bool getBit( int addr, int mbreg, int mbfunc=0x2 );
/*! Функция записи регистров 0x06 или 0x10 задаётся параметром \a mbfunc. /*! Функция записи регистров 0x06 или 0x10 задаётся параметром \a mbfunc.
* Если не указывать ip и порт, будут использованы, те * Если не указывать ip и порт, будут использованы, те
* чтобы были заданы в UModbus::connect(). Если заданы другие ip и port, * чтобы были заданы в UModbus::connect(). Если заданы другие ip и port,
* будет сделана переподключение.. * будет сделана переподключение..
* \return TRUE - если получен положительный ответ
*/ */
void mbwrite( int addr, int mbreg, int val, int mbfunc, const char* ip=0, int port=-1 )throw(UException); bool mbwrite( int addr, int mbreg, int val, int mbfunc=0x6, const char* ip=0, int port=-1 );
bool setWord( int addr, int mbreg, int val, int mbfunc=0x6 );
protected: protected:
long data2value( VTypes::VType vt, ModbusRTU::ModbusData* data ); long data2value( VTypes::VType vt, ModbusRTU::ModbusData* data );
...@@ -59,7 +60,7 @@ class UModbus ...@@ -59,7 +60,7 @@ class UModbus
// DebugStream dlog; // DebugStream dlog;
ModbusTCPMaster* mb; ModbusTCPMaster* mb;
int port; int port;
string ip; std::string ip;
int tout_msec; int tout_msec;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
%pragma(java) jniclasscode=%{ %pragma(java) jniclasscode=%{
static { static {
try { try {
System.loadLibrary(".libs/_jUModbus.so"); System.loadLibrary("mUModbus");
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e); System.err.println("Native code library failed to load. \n" + e);
System.exit(1); System.exit(1);
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
%} %}
*/ */
%include "UModbus.h"
%include "UTypes.h" %include "UTypes.h"
%include "UExceptions.h" %include "UExceptions.h"
%include UExceptions.i
%include "UModbus.h"
...@@ -9,13 +9,13 @@ namespace UTypes ...@@ -9,13 +9,13 @@ namespace UTypes
struct Params struct Params
{ {
static const int max = 20; static const int maxnum = 20;
Params():argc(0){ memset(argv,0,sizeof(argv)); } Params():argc(0){ memset(argv,0,sizeof(argv)); }
bool add( char* s ) bool add( char* s )
{ {
if( argc < Params::max ) if( argc < Params::maxnum )
{ {
argv[argc++] = s; argv[argc++] = s;
return true; return true;
...@@ -24,7 +24,7 @@ namespace UTypes ...@@ -24,7 +24,7 @@ namespace UTypes
} }
int argc; int argc;
char* argv[max]; char* argv[maxnum];
static Params inst(){ return Params(); } static Params inst(){ return Params(); }
}; };
......
#!/bin/sh
javac runme_modbus.java && java runme_modbus
\ No newline at end of file
public class runme_modbus {
public static void main(String argv[]) {
System.load("/home/pv/Projects/uniset-2.0/wrappers/java/.libs/_jUModbus.so");
System.load("/home/pv/Projects/uniset-2.0/wrappers/java/.libs/_jUExceptions.so");
UModbus u = new UModbus();
u.connect("localhost",2048);
for( int i=0; i<10; i++ )
{
try
{
System.out.println("read..32=" + u.getWord(1,32));
System.out.println("write 40=4 " + ( u.setWord(1,40,4) ? "ok":"failed" ) );
}
catch( Exception e )
{
System.err.println(e.getMessage());
}
}
}
}
\ No newline at end of file
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