UniSet  2.6.0
UModbus.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // -------------------------------------------------------------------------
00017 #ifndef UModbus_H_
00018 #define UModbus_H_
00019 // --------------------------------------------------------------------------
00020 #include "Configuration.h"
00021 #include "UInterface.h"
00022 #include "modbus/ModbusTCPMaster.h"
00023 #include "modbus/ModbusTypes.h"
00024 #include "extensions/VTypes.h"
00025 #include "Debug.h"
00026 #include "UTypes.h"
00027 #include "UExceptions.h"
00028 // --------------------------------------------------------------------------
00029 class UModbus
00030 {
00031     public:
00032 
00033         UModbus();
00034         ~UModbus();
00035 
00036         inline std::string getUIType()
00037         {
00038             return string("modbus");
00039         }
00040 
00041         inline bool isWriteFunction( int mbfunc )
00042         {
00043             return uniset::ModbusRTU::isWriteFunction((uniset::ModbusRTU::SlaveFunctionCode)mbfunc);
00044         }
00045 
00046         // выставление паметров связи, без установления соединения (!)
00047         void prepare( const std::string& ip, int port )throw(UException);
00048 
00049         void connect( const std::string& ip, int port )throw(UException);
00050         inline int conn_port()
00051         {
00052             return port;
00053         }
00054         inline std::string conn_ip()
00055         {
00056             return ip;
00057         }
00058         inline bool isConnection()
00059         {
00060             return (mb && mb->isConnection());
00061         }
00062 
00063         inline void setTimeout( int msec )
00064         {
00065             tout_msec = msec;
00066         }
00067 
00073         long mbread( int addr, int mbreg, int mbfunc,
00074                      const std::string& vtype, int nbit = -1,
00075                      const std::string& ip = "", int port = -1 )throw(UException);
00076 
00077         long getWord( int addr, int mbreg, int mbfunc = 0x4 )throw(UException);
00078         long getByte( int addr, int mbreg, int mbfunc = 0x4 )throw(UException);
00079         bool getBit( int addr, int mbreg, int mbfunc = 0x2 )throw(UException);
00080 
00086         void mbwrite( int addr, int mbreg, int val, int mbfunc, const std::string& ip = "", int port = -1 )throw(UException);
00087 
00088     protected:
00089         long data2value( uniset::VTypes::VType vt, uniset::ModbusRTU::ModbusData* data );
00090 
00091     private:
00092         // DebugStream dlog;
00093         uniset::ModbusTCPMaster* mb;
00094         int port;
00095         string ip;
00096         int tout_msec;
00097 };
00098 //---------------------------------------------------------------------------
00099 #endif
00100 //---------------------------------------------------------------------------