-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Support Modbus Meters #272
base: master
Are you sure you want to change the base?
Changes from all commits
ede6873
82311bc
7cd4d06
1801abd
eabf340
85e9afc
0492b2c
cee7339
bbfe9a9
9c1131e
514717b
31c567e
5f73c4b
f8c2366
4b25cb0
d8a3c50
825e8ed
23aa4f2
7ba20dc
6cd280f
a52d7f1
edb2a5a
7e074aa
9f54296
3c41efe
7582d0e
1759b5b
5649f91
9895172
2ff882c
63df238
3e5edc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,7 @@ stamp-h1 | |
|
||
# Debian packaging | ||
/debian/config.log | ||
|
||
.cproject | ||
.project | ||
.settings/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/** | ||
* @file MeterModbus.h | ||
* @brief | ||
* Created on: Apr 25, 2016 | ||
* @author Florian Achleitner <[email protected]> | ||
*/ | ||
#ifndef METERMODBUS_H_ | ||
#define METERMODBUS_H_ | ||
|
||
#include "protocols/Protocol.hpp" | ||
#include <modbus/modbus.h> | ||
#include <errno.h> | ||
#include <map> | ||
|
||
class ModbusException : public std::runtime_error { | ||
int _errno; | ||
std::string _what; | ||
public: | ||
explicit ModbusException(const std::string& arg); | ||
virtual const char *what() const noexcept override { | ||
return _what.c_str(); | ||
} | ||
|
||
}; | ||
|
||
class ModbusConnection | ||
{ | ||
protected: | ||
modbus_t *_ctx; | ||
bool _connected; | ||
public: | ||
typedef vz::shared_ptr<ModbusConnection> Ptr; | ||
ModbusConnection() : | ||
_ctx(NULL), _connected(false) {} | ||
virtual ~ModbusConnection(); | ||
modbus_t *getctx() { | ||
return _ctx; | ||
} | ||
|
||
virtual void open() = 0; | ||
virtual void connect(); | ||
virtual void close(); | ||
virtual void read_registers(int addr, int nb, uint16_t *dest, unsigned slave); | ||
virtual void debug(bool enable); | ||
private: | ||
ModbusConnection(const ModbusConnection&) = delete; | ||
ModbusConnection& operator= (const ModbusConnection&) = delete; | ||
}; | ||
|
||
class ModbusRTUConnection : public ModbusConnection | ||
{ | ||
std::string _device; | ||
int _baud; | ||
public: | ||
ModbusRTUConnection(const std::string &device, int baud) | ||
: _device(device), _baud(baud) {} | ||
virtual void open(); | ||
}; | ||
|
||
class ModbusTCPConnection : public ModbusConnection | ||
{ | ||
std::string _ip; | ||
int _port; | ||
public: | ||
ModbusTCPConnection(const std::string &ip, int port = 502) | ||
: _ip(ip), _port(port) {} | ||
virtual void open(); | ||
}; | ||
|
||
class RegisterMap | ||
{ | ||
public: | ||
typedef vz::shared_ptr<RegisterMap> Ptr; | ||
virtual ~RegisterMap() {} | ||
virtual void read(std::vector<Reading>& rds, ModbusConnection::Ptr conn, unsigned id) = 0; | ||
static Ptr findMap(const std::string &name) { | ||
return maps.at(name)(); | ||
} | ||
private: | ||
template <class T> static Ptr createMap() { | ||
return Ptr(new T()); | ||
} | ||
|
||
static std::map<std::string, Ptr (*)()> maps; | ||
}; | ||
/** | ||
* | ||
*/ | ||
class MeterModbus: public vz::protocol::Protocol | ||
{ | ||
public: | ||
typedef unsigned slaveid_t; | ||
private: | ||
ModbusConnection::Ptr _mbconn; | ||
bool _libmodbus_debug; | ||
typedef std::map<slaveid_t, RegisterMap::Ptr> SlaveRegMaps; | ||
SlaveRegMaps _devices; | ||
|
||
void create_rtu(const std::list<Option> &options); | ||
void create_tcp(const std::list<Option> &options); | ||
public: | ||
MeterModbus(const std::list<Option> &options); | ||
virtual ~MeterModbus(); | ||
virtual int open(); | ||
virtual int close(); | ||
virtual ssize_t read(std::vector<Reading> &rds, size_t n); | ||
}; | ||
|
||
|
||
class OpRegisterMap: public RegisterMap { | ||
public: | ||
virtual ~OpRegisterMap() {} | ||
virtual void read(std::vector<Reading>& rds, ModbusConnection::Ptr conn, unsigned id); | ||
}; | ||
|
||
class IMEmeterRegisterMap: public RegisterMap { | ||
public: | ||
virtual ~IMEmeterRegisterMap() {} | ||
virtual void read(std::vector<Reading>& rds, ModbusConnection::Ptr conn, unsigned id); | ||
}; | ||
|
||
#endif /* METERMODBUS_H_ */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ if( MBUS_FOUND ) | |
target_link_libraries(vzlogger ${MBUS_LIBRARY}) | ||
endif( MBUS_FOUND ) | ||
target_link_libraries(vzlogger ${OCR_LIBRARIES}) | ||
target_link_libraries(vzlogger modbus) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Different from mbus_library? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, modbus and mbus have little in common, except the m and the bus ;) |
||
|
||
if(LOCAL_SUPPORT) | ||
target_link_libraries(vzlogger ${MICROHTTPD_LIBRARY}) | ||
|
@@ -72,6 +73,7 @@ if( TARGET ) | |
endif( TARGET ) | ||
target_link_libraries(vzlogger ${CURL_STATIC_LIBRARIES} ${CURL_LIBRARIES} ${GNUTLS_LIBRARIES} ${OPENSSL_LIBRARIES} ) | ||
|
||
|
||
# add programs to the install target | ||
INSTALL(PROGRAMS | ||
${CMAKE_CURRENT_BINARY_DIR}/vzlogger | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
#ifdef OMS_SUPPORT | ||
#include "protocols/MeterOMS.hpp" | ||
#endif | ||
#include "protocols/MeterModbus.hpp" | ||
//#include <protocols/.h> | ||
|
||
#define METER_DETAIL(NAME, CLASSNAME, DESC, MAX_RDS, PERIODIC) { \ | ||
|
@@ -72,6 +73,7 @@ static const meter_details_t protocols[] = { | |
#ifdef OMS_SUPPORT | ||
METER_DETAIL(oms, OMS, "OMS (M-BUS) protocol based devices", 100, false), // todo what is the max. amount of reading according to spec? | ||
#endif | ||
METER_DETAIL(modbus, Modbus, "Modbus meter", 64, true), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surround with ifdef fpor modbus support throughout the vode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It belongs to this todo to make the whole modbus stuff optional
Still open .. |
||
//{} /* stop condition for iterator */ | ||
METER_DETAIL(none, NULL,NULL, 0,false), | ||
}; | ||
|
@@ -193,6 +195,10 @@ Meter::Meter(std::list<Option> pOptions) : | |
_identifier = ReadingIdentifier::Ptr(new ObisIdentifier()); | ||
break; | ||
#endif | ||
case meter_protocol_modbus: | ||
_protocol = vz::protocol::Protocol::Ptr(new MeterModbus(pOptions)); | ||
_identifier = ReadingIdentifier::Ptr(new ModbusReadingIdentifier()); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indents?