Исправления
This commit is contained in:
138
Modbus.h
138
Modbus.h
@@ -8,6 +8,7 @@
|
||||
#include "mbed.h"
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#include <byteswap.h>
|
||||
#endif
|
||||
@@ -39,66 +40,70 @@
|
||||
|
||||
struct TRegister;
|
||||
|
||||
typedef uint16_t (*cbModbus)(TRegister* reg, uint16_t val); // Callback function Type
|
||||
typedef uint16_t (*cbModbus)(TRegister *reg, uint16_t val); // Callback function Type
|
||||
|
||||
struct TAddress {
|
||||
enum RegType { COIL,
|
||||
enum RegType {
|
||||
COIL,
|
||||
ISTS,
|
||||
IREG,
|
||||
HREG };
|
||||
HREG
|
||||
};
|
||||
RegType type;
|
||||
uint16_t address;
|
||||
bool operator==(const TAddress& obj) const
|
||||
{ // TAddress == TAddress
|
||||
|
||||
bool operator==(const TAddress &obj) const { // TAddress == TAddress
|
||||
return type == obj.type && address == obj.address;
|
||||
}
|
||||
bool operator!=(const TAddress& obj) const
|
||||
{ // TAddress != TAddress
|
||||
|
||||
bool operator!=(const TAddress &obj) const { // TAddress != TAddress
|
||||
return type != obj.type || address != obj.address;
|
||||
}
|
||||
TAddress& operator++()
|
||||
{ // ++TAddress
|
||||
|
||||
TAddress &operator++() { // ++TAddress
|
||||
address++;
|
||||
return *this;
|
||||
}
|
||||
TAddress operator++(int)
|
||||
{ // TAddress++
|
||||
|
||||
const TAddress operator++(int) { // TAddress++
|
||||
TAddress result(*this);
|
||||
++(*this);
|
||||
return result;
|
||||
}
|
||||
TAddress& operator+=(const int& inc)
|
||||
{ // TAddress += integer
|
||||
|
||||
TAddress &operator+=(const int &inc) { // TAddress += integer
|
||||
address += inc;
|
||||
return *this;
|
||||
}
|
||||
const TAddress operator+(const int& inc) const
|
||||
{ // TAddress + integer
|
||||
|
||||
TAddress operator+(const int &inc) const { // TAddress + integer
|
||||
TAddress result(*this);
|
||||
result.address += inc;
|
||||
return result;
|
||||
}
|
||||
bool isCoil()
|
||||
{
|
||||
|
||||
bool isCoil() const {
|
||||
return type == COIL;
|
||||
}
|
||||
bool isIsts()
|
||||
{
|
||||
|
||||
bool isIsts() const {
|
||||
return type == ISTS;
|
||||
}
|
||||
bool isIreg()
|
||||
{
|
||||
|
||||
bool isIreg() const {
|
||||
return type == IREG;
|
||||
}
|
||||
bool isHreg()
|
||||
{
|
||||
|
||||
bool isHreg() const {
|
||||
return type == HREG;
|
||||
}
|
||||
};
|
||||
|
||||
struct TCallback {
|
||||
enum CallbackType { ON_SET,
|
||||
ON_GET };
|
||||
enum CallbackType {
|
||||
ON_SET,
|
||||
ON_GET
|
||||
};
|
||||
CallbackType type;
|
||||
TAddress address;
|
||||
cbModbus cb;
|
||||
@@ -107,8 +112,8 @@ struct TCallback {
|
||||
struct TRegister {
|
||||
TAddress address;
|
||||
uint16_t value;
|
||||
bool operator==(const TRegister& obj) const
|
||||
{
|
||||
|
||||
bool operator==(const TRegister &obj) const {
|
||||
return address == obj.address;
|
||||
}
|
||||
};
|
||||
@@ -151,23 +156,41 @@ public:
|
||||
EX_CONNECTION_LOST = 0xE5, // Custom. Connection with device lost
|
||||
EX_CANCEL = 0xE6 // Custom. Transaction/request canceled
|
||||
};
|
||||
|
||||
~Modbus();
|
||||
|
||||
bool addHreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);
|
||||
|
||||
bool Hreg(uint16_t offset, uint16_t value);
|
||||
|
||||
uint16_t Hreg(uint16_t offset);
|
||||
|
||||
uint16_t removeHreg(uint16_t offset, uint16_t numregs = 1);
|
||||
|
||||
bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1);
|
||||
|
||||
bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1);
|
||||
|
||||
bool addIreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);
|
||||
|
||||
bool Coil(uint16_t offset, bool value);
|
||||
|
||||
bool Ists(uint16_t offset, bool value);
|
||||
|
||||
bool Ireg(uint16_t offset, uint16_t value);
|
||||
|
||||
bool Coil(uint16_t offset);
|
||||
|
||||
bool Ists(uint16_t offset);
|
||||
|
||||
uint16_t Ireg(uint16_t offset);
|
||||
|
||||
bool removeCoil(uint16_t offset, uint16_t numregs = 1);
|
||||
|
||||
bool removeIsts(uint16_t offset, uint16_t numregs = 1);
|
||||
|
||||
bool removeIreg(uint16_t offset, uint16_t numregs = 1);
|
||||
|
||||
/*
|
||||
bool Hreg(uint16_t offset, uint16_t* value);
|
||||
bool Coil(uint16_t offset, bool* value);
|
||||
@@ -175,38 +198,57 @@ public:
|
||||
bool Ireg(uint16_t offset, uint16_t* value);
|
||||
*/
|
||||
void cbEnable(bool state = true);
|
||||
|
||||
void cbDisable();
|
||||
|
||||
bool onGetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onSetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onGetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onSetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onGetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onSetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onGetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onSetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnGetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnSetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnGetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnSetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnGetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnSetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnGetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnSetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
private:
|
||||
void readBits(TAddress startreg, uint16_t numregs, FunctionCode fn);
|
||||
|
||||
void readWords(TAddress startreg, uint16_t numregs, FunctionCode fn);
|
||||
|
||||
void setMultipleBits(uint8_t* frame, TAddress startreg, uint16_t numoutputs);
|
||||
void setMultipleWords(uint16_t* frame, TAddress startreg, uint16_t numoutputs);
|
||||
void setMultipleBits(const uint8_t *frame, TAddress startreg, uint16_t numoutputs);
|
||||
|
||||
void getMultipleBits(uint8_t* frame, TAddress startreg, uint16_t numregs);
|
||||
void getMultipleWords(uint16_t* frame, TAddress startreg, uint16_t numregs);
|
||||
void setMultipleWords(const uint16_t *frame, TAddress startreg, uint16_t numoutputs);
|
||||
|
||||
void bitsToBool(bool* dst, uint8_t* src, uint16_t numregs);
|
||||
void boolToBits(uint8_t* dst, bool* src, uint16_t numregs);
|
||||
void getMultipleBits(uint8_t *frame, TAddress startreg, uint16_t numregs);
|
||||
|
||||
void getMultipleWords(uint16_t *frame, TAddress startreg, uint16_t numregs);
|
||||
|
||||
static void bitsToBool(bool *dst, const uint8_t *src, uint16_t numregs);
|
||||
|
||||
static void boolToBits(uint8_t *dst, const bool *src, uint16_t numregs);
|
||||
|
||||
protected:
|
||||
//Reply Types
|
||||
@@ -221,24 +263,31 @@ protected:
|
||||
std::vector<TRegister> _regs;
|
||||
std::vector<TCallback> _callbacks;
|
||||
#endif
|
||||
uint8_t* _frame = nullptr;
|
||||
uint8_t *_frame = nullptr;
|
||||
uint16_t _len = 0;
|
||||
uint8_t _reply = 0;
|
||||
bool cbEnabled = true;
|
||||
uint16_t mbCallback(TRegister* reg, uint16_t val, TCallback::CallbackType t);
|
||||
TRegister* searchRegister(TAddress addr);
|
||||
|
||||
uint16_t mbCallback(TRegister *reg, uint16_t val, TCallback::CallbackType t);
|
||||
|
||||
TRegister *searchRegister(TAddress addr);
|
||||
|
||||
void exceptionResponse(FunctionCode fn, ResultCode excode); // Fills _frame with response
|
||||
void successResponce(TAddress startreg, uint16_t numoutputs, FunctionCode fn); // Fills frame with response
|
||||
void slavePDU(uint8_t* frame); //For Slave
|
||||
void masterPDU(uint8_t* frame, uint8_t* sourceFrame, TAddress startreg, uint16_t* output = nullptr); //For Master
|
||||
void slavePDU(uint8_t *frame); //For Slave
|
||||
void
|
||||
masterPDU(uint8_t *frame, const uint8_t *sourceFrame, TAddress startreg, uint16_t *output = nullptr); //For Master
|
||||
// frame - data received form slave
|
||||
// sourceFrame - data have sent fo slave
|
||||
// startreg - local register to start put data to
|
||||
// output - if not null put data to the buffer insted local registers. output assumed to by array of uint16_t or boolean
|
||||
|
||||
bool readSlave(uint16_t address, uint16_t numregs, FunctionCode fn);
|
||||
bool writeSlaveBits(TAddress startreg, uint16_t to, uint16_t numregs, FunctionCode fn, bool* data = nullptr);
|
||||
bool writeSlaveWords(TAddress startreg, uint16_t to, uint16_t numregs, FunctionCode fn, uint16_t* data = nullptr);
|
||||
|
||||
bool writeSlaveBits(TAddress startreg, uint16_t to, uint16_t numregs, FunctionCode fn, bool *data = nullptr);
|
||||
|
||||
bool
|
||||
writeSlaveWords(TAddress startreg, uint16_t to, uint16_t numregs, FunctionCode fn, const uint16_t *data = nullptr);
|
||||
// startreg - local register to get data from
|
||||
// to - slave register to write data to
|
||||
// numregs - number of registers
|
||||
@@ -246,16 +295,23 @@ protected:
|
||||
// data - if null use local registers. Otherwise use data from array to erite to slave
|
||||
|
||||
bool addReg(TAddress address, uint16_t value = 0, uint16_t numregs = 1);
|
||||
|
||||
bool Reg(TAddress address, uint16_t value);
|
||||
|
||||
uint16_t Reg(TAddress address);
|
||||
|
||||
bool removeReg(TAddress address, uint16_t numregs = 1);
|
||||
|
||||
bool onGet(TAddress address, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool onSet(TAddress address, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnSet(TAddress address, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
bool removeOnGet(TAddress address, cbModbus cb = nullptr, uint16_t numregs = 1);
|
||||
|
||||
virtual uint32_t eventSource() { return 0; }
|
||||
};
|
||||
|
||||
typedef bool (*cbTransaction)(Modbus::ResultCode event, uint16_t transactionId, void* data); // Callback skeleton for requests
|
||||
typedef bool (*cbTransaction)(Modbus::ResultCode event, uint16_t transactionId,
|
||||
void *data); // Callback skeleton for requests
|
||||
|
||||
Reference in New Issue
Block a user