123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #pragma once
-
- #include <string>
- #include <modbuspp.h>
- #include "ParameterInterface.h"
-
- class ModbusInterface {
- private:
- unsigned int slaveAddressID;
- bool open;
- protected:
- Modbus::Master* my_modbus = nullptr;
- Modbus::Slave* my_slave = nullptr;
-
- //IP (TCP) or serial port (RTU)
- std::string device;
-
- void assignSlave();
-
- public:
- ModbusInterface(u_int _id, std::string _device) : slaveAddressID(_id), device(_device) { };
- virtual ~ModbusInterface();
- virtual void modbus_init() = 0;
- virtual std::string getConnectionType() = 0;
-
- bool openConnection();
- void disconnect();
- bool getIsOpen() const { return open; }
-
- unsigned int getID(){ return slaveAddressID; }
-
- //Read and write functions
- bool readBit(ParameterSpecification ¶m);
- bool readBits(ParameterSpecification ¶m);
- uint8_t readByte(ParameterSpecification ¶m);
- uint16_t readRegister(ParameterSpecification ¶m);
- uint32_t readDRegister(ParameterSpecification ¶m);
- uint64_t readQRegister(ParameterSpecification ¶m);
-
- bool writeBit(const uint16_t address, bool bit);
- bool writeByte(const uint16_t address, uint8_t byte);
- bool writeRegister(const uint16_t address, uint16_t word);
- bool writeDRegister(const uint16_t address, uint32_t dword);
- bool writeQRegister(const uint16_t address, uint64_t qword);
- };
-
-
- #define _MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \
- (((int64_t)tab_int16[(index) ] << 48) + \
- ((int64_t)tab_int16[(index) + 1] << 32) + \
- ((int64_t)tab_int16[(index) + 2] << 16) + \
- (int64_t)tab_int16[(index) + 3])
- #define _MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
- #define _MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
|