Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModbusInterface.h 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. #include <string>
  3. #include <modbuspp.h>
  4. #include "ParameterInterface.h"
  5. class ModbusInterface {
  6. private:
  7. unsigned int slaveAddressID;
  8. bool open;
  9. protected:
  10. Modbus::Master* my_modbus = nullptr;
  11. Modbus::Slave* my_slave = nullptr;
  12. //IP (TCP) or serial port (RTU)
  13. std::string device;
  14. void assignSlave();
  15. public:
  16. ModbusInterface(u_int _id, std::string _device) : slaveAddressID(_id), device(_device) { };
  17. virtual ~ModbusInterface();
  18. virtual void modbus_init() = 0;
  19. virtual std::string getConnectionType() = 0;
  20. bool openConnection();
  21. void disconnect();
  22. bool getIsOpen() const { return open; }
  23. unsigned int getID(){ return slaveAddressID; }
  24. //Read and write functions
  25. bool readBit(ParameterSpecification &param);
  26. bool readBits(ParameterSpecification &param);
  27. uint8_t readByte(ParameterSpecification &param);
  28. uint16_t readRegister(ParameterSpecification &param);
  29. uint32_t readDRegister(ParameterSpecification &param);
  30. uint64_t readQRegister(ParameterSpecification &param);
  31. bool writeBit(const uint16_t address, bool bit);
  32. bool writeByte(const uint16_t address, uint8_t byte);
  33. bool writeRegister(const uint16_t address, uint16_t word);
  34. bool writeDRegister(const uint16_t address, uint32_t dword);
  35. bool writeQRegister(const uint16_t address, uint64_t qword);
  36. };
  37. #define _MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \
  38. (((int64_t)tab_int16[(index) ] << 48) + \
  39. ((int64_t)tab_int16[(index) + 1] << 32) + \
  40. ((int64_t)tab_int16[(index) + 2] << 16) + \
  41. (int64_t)tab_int16[(index) + 3])
  42. #define _MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
  43. #define _MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])