mbed trace

This commit is contained in:
Beslan
2022-01-25 06:48:12 +03:00
parent 4aba0072c3
commit bb39f6b11c
3 changed files with 92 additions and 92 deletions

View File

@@ -188,13 +188,14 @@ void Modbus::slavePDU(uint8_t *frame) {
default:
exceptionResponse(fcode, EX_ILLEGAL_FUNCTION);
break;
}
}
void Modbus::successResponce(TAddress startreg, uint16_t numoutputs, FunctionCode fn) {
free(_frame);
_len = 5;
_frame = (uint8_t *) malloc(_len);
_len = 5U;
_frame = static_cast<uint8_t *> (malloc(_len));
_frame[0] = fn;
_frame[1] = startreg.address >> 8;
_frame[2] = startreg.address & 0x00FF;
@@ -204,7 +205,7 @@ void Modbus::successResponce(TAddress startreg, uint16_t numoutputs, FunctionCod
void Modbus::exceptionResponse(FunctionCode fn, ResultCode excode) {
free(_frame);
_len = 2;
_len = 2U;
_frame = (uint8_t *) malloc(_len);
_frame[0] = fn + 0x80;
_frame[1] = excode;
@@ -212,17 +213,15 @@ void Modbus::exceptionResponse(FunctionCode fn, ResultCode excode) {
}
void Modbus::getMultipleBits(uint8_t *frame, TAddress startreg, uint16_t numregs) {
uint8_t bitn = 0;
uint16_t i = 0;
uint8_t bitn = 0U;
uint16_t i = 0U;
while (numregs--) {
if (BIT_BOOL(Reg(startreg)))
bitSet(frame[i], bitn);
else
bitClear(frame[i], bitn);
if (BIT_BOOL(Reg(startreg))) { bitSet(frame[i], bitn); }
else { bitClear(frame[i], bitn); }
bitn++; //increment the bit index
if (bitn == 8) {
if (bitn == 8U) {
i++;
bitn = 0;
bitn = 0U;
}
startreg++; //increment the register
}
@@ -251,7 +250,7 @@ void Modbus::readBits(TAddress startreg, uint16_t numregs, FunctionCode fn) {
free(_frame);
//Determine the message length = function type, byte count and
//for each group of 8 registers the message length increases by 1
_len = 2 + numregs / 8;
_len = 2U + numregs / 8U;
if (numregs % 8)
_len++; //Add 1 to the message length for the partial byte.
_frame = (uint8_t *) malloc(_len);
@@ -260,8 +259,8 @@ void Modbus::readBits(TAddress startreg, uint16_t numregs, FunctionCode fn) {
return;
}
_frame[0] = fn;
_frame[1] = _len - 2; //byte count (_len - function code and byte count)
_frame[_len - 1] = 0; //Clean last probably partial byte
_frame[1] = _len - 2U; //byte count (_len - function code and byte count)
_frame[_len - 1U] = 0U; //Clean last probably partial byte
getMultipleBits(_frame + 2, startreg, numregs);
_reply = REPLY_NORMAL;
}
@@ -290,22 +289,22 @@ void Modbus::readWords(TAddress startreg, uint16_t numregs, FunctionCode fn) {
}
void Modbus::setMultipleBits(const uint8_t *frame, TAddress startreg, uint16_t numoutputs) {
uint8_t bitn = 0;
uint16_t i = 0;
uint8_t bitn = 0U;
uint16_t i = 0U;
while (numoutputs--) {
Reg(startreg, BIT_VAL(bitRead(frame[i], bitn)));
(void) Reg(startreg, BIT_VAL(bitRead(frame[i], bitn)));
bitn++; //increment the bit index
if (bitn == 8) {
if (bitn == 8U) {
i++;
bitn = 0;
bitn = 0U;
}
startreg++; //increment the register
}
}
void Modbus::setMultipleWords(const uint16_t *frame, TAddress startreg, uint16_t numregs) {
for (uint16_t i = 0; i < numregs; i++) {
Reg(startreg + i, __bswap_16(frame[i]));
for (uint16_t i = 0U; i < numregs; i++) {
(void) Reg(startreg + i, __bswap_16(frame[i]));
}
}
@@ -333,7 +332,7 @@ bool Modbus::onSet(TAddress address, cbModbus cb, uint16_t numregs) {
if (!cb) {
return removeOnGet(address);
}
while (numregs > 0) {
while (numregs > 0U) {
reg = searchRegister(address);
if (reg) {
_callbacks.push_back({TCallback::ON_SET, address, cb});
@@ -347,7 +346,7 @@ bool Modbus::onSet(TAddress address, cbModbus cb, uint16_t numregs) {
bool Modbus::removeOnSet(TAddress address, cbModbus cb, uint16_t numregs) {
while (numregs--) {
_callbacks.erase(remove_if(_callbacks.begin(), _callbacks.end(), [address, cb](TCallback entry) {
(void) _callbacks.erase(remove_if(_callbacks.begin(), _callbacks.end(), [address, cb](TCallback entry) {
return entry.type == TCallback::ON_SET && entry.address == address && (!cb || entry.cb == cb);
}), _callbacks.end());
address++;
@@ -357,7 +356,7 @@ bool Modbus::removeOnSet(TAddress address, cbModbus cb, uint16_t numregs) {
bool Modbus::removeOnGet(TAddress address, cbModbus cb, uint16_t numregs) {
while (numregs--) {
_callbacks.erase(remove_if(_callbacks.begin(), _callbacks.end(), [address, cb](TCallback entry) {
(void) _callbacks.erase(remove_if(_callbacks.begin(), _callbacks.end(), [address, cb](TCallback entry) {
return entry.type == TCallback::ON_GET && entry.address == address && (!cb || entry.cb == cb);
}), _callbacks.end());
address++;
@@ -367,7 +366,7 @@ bool Modbus::removeOnGet(TAddress address, cbModbus cb, uint16_t numregs) {
bool Modbus::readSlave(uint16_t address, uint16_t numregs, FunctionCode fn) {
free(_frame);
_len = 5;
_len = 5U;
_frame = (uint8_t *) malloc(_len);
_frame[0] = fn;
_frame[1] = address >> 8;
@@ -379,18 +378,18 @@ bool Modbus::readSlave(uint16_t address, uint16_t numregs, FunctionCode fn) {
bool Modbus::writeSlaveBits(TAddress startreg, uint16_t to, uint16_t numregs, FunctionCode fn, bool *data) {
free(_frame);
_len = 6 + numregs / 8;
if (numregs % 8)
_len = 6U + numregs / 8U;
if (numregs % 8U)
_len++; //Add 1 to the message length for the partial byte.
_frame = (uint8_t *) malloc(_len);
if (_frame) {
_frame[0] = fn;
_frame[1] = to >> 8;
_frame[2] = to & 0x00FF;
_frame[3] = numregs >> 8;
_frame[4] = numregs & 0x00FF;
_frame[5] = _len - 6;
_frame[_len - 1] = 0; //Clean last probably partial byte
_frame[1] = to >> 8U;
_frame[2] = to & 0x00FFU;
_frame[3] = numregs >> 8U;
_frame[4] = numregs & 0x00FFU;
_frame[5] = _len - 6U;
_frame[_len - 1] = 0U; //Clean last probably partial byte
if (data) {
boolToBits(_frame + 6, data, numregs);
} else {
@@ -429,33 +428,33 @@ bool Modbus::writeSlaveWords(TAddress startreg, uint16_t to, uint16_t numregs, F
}
void Modbus::boolToBits(uint8_t *dst, const bool *src, uint16_t numregs) {
uint8_t bitn = 0;
uint16_t i = 0;
uint16_t j = 0;
uint8_t bitn = 0U;
uint16_t i = 0U;
uint16_t j = 0U;
while (numregs--) {
if (src[j])
bitSet(dst[i], bitn);
else
bitClear(dst[i], bitn);
bitn++; //increment the bit index
if (bitn == 8) {
if (bitn == 8U) {
i++;
bitn = 0;
bitn = 0U;
}
j++; //increment the register
}
}
void Modbus::bitsToBool(bool *dst, const uint8_t *src, uint16_t numregs) {
uint8_t bitn = 0;
uint16_t i = 0;
uint16_t j = 0;
uint8_t bitn = 0U;
uint16_t i = 0U;
uint16_t j = 0U;
while (numregs--) {
dst[j] = bitRead(src[i], bitn);
bitn++; //increment the bit index
if (bitn == 8) {
if (bitn == 8U) {
i++;
bitn = 0;
bitn = 0U;
}
j++; //increment the register
}
@@ -493,8 +492,8 @@ void Modbus::masterPDU(uint8_t *frame, const uint8_t *sourceFrame, TAddress star
case FC_READ_COILS:
case FC_READ_INPUT_STAT:
//field2 = numregs, frame[1] = data length, header len = 2
bytecount_calc = field2 / 8;
if (field2 % 8)
bytecount_calc = field2 / 8U;
if (field2 % 8U)
bytecount_calc++;
if (frame[1] != bytecount_calc) { // check if data size matches
_reply = EX_DATA_MISMACH;
@@ -513,6 +512,7 @@ void Modbus::masterPDU(uint8_t *frame, const uint8_t *sourceFrame, TAddress star
break;
default:
_reply = EX_GENERAL_FAILURE;
break;
}
}

View File

@@ -122,56 +122,56 @@ class Modbus {
public:
//Function Codes
enum FunctionCode {
FC_READ_COILS = 0x01, // Read Coils (Output) Status
FC_READ_INPUT_STAT = 0x02, // Read Input Status (Discrete Inputs)
FC_READ_REGS = 0x03, // Read Holding Registers
FC_READ_INPUT_REGS = 0x04, // Read Input Registers
FC_WRITE_COIL = 0x05, // Write Single Coil (Output)
FC_WRITE_REG = 0x06, // Preset Single Register
FC_DIAGNOSTICS = 0x08, // Not implemented. Diagnostics (Serial Line only)
FC_WRITE_COILS = 0x0F, // Write Multiple Coils (Outputs)
FC_WRITE_REGS = 0x10, // Write block of contiguous registers
FC_READ_FILE_REC = 0x14, // Not implemented. Read File Record
FC_WRITE_FILE_REC = 0x15, // Not implemented. Write File Record
FC_MASKWRITE_REG = 0x16, // Not implemented. Mask Write Register
FC_READWRITE_REGS = 0x17 // Not implemented. Read/Write Multiple registers
FC_READ_COILS = 0x01U, // Read Coils (Output) Status
FC_READ_INPUT_STAT = 0x02U, // Read Input Status (Discrete Inputs)
FC_READ_REGS = 0x03U, // Read Holding Registers
FC_READ_INPUT_REGS = 0x04U, // Read Input Registers
FC_WRITE_COIL = 0x05U, // Write Single Coil (Output)
FC_WRITE_REG = 0x06U, // Preset Single Register
FC_DIAGNOSTICS = 0x08U, // Not implemented. Diagnostics (Serial Line only)
FC_WRITE_COILS = 0x0FU, // Write Multiple Coils (Outputs)
FC_WRITE_REGS = 0x10U, // Write block of contiguous registers
FC_READ_FILE_REC = 0x14U, // Not implemented. Read File Record
FC_WRITE_FILE_REC = 0x15U, // Not implemented. Write File Record
FC_MASKWRITE_REG = 0x16U, // Not implemented. Mask Write Register
FC_READWRITE_REGS = 0x17U // Not implemented. Read/Write Multiple registers
};
//Exception Codes
//Custom result codes used internally and for callbacks but never used for Modbus responce
enum ResultCode {
EX_SUCCESS = 0x00, // Custom. No error
EX_ILLEGAL_FUNCTION = 0x01, // Function Code not Supported
EX_ILLEGAL_ADDRESS = 0x02, // Output Address not exists
EX_ILLEGAL_VALUE = 0x03, // Output Value not in Range
EX_SLAVE_FAILURE = 0x04, // Slave or Master Device Fails to process request
EX_ACKNOWLEDGE = 0x05, // Not used
EX_SLAVE_DEVICE_BUSY = 0x06, // Not used
EX_MEMORY_PARITY_ERROR = 0x08, // Not used
EX_PATH_UNAVAILABLE = 0x0A, // Not used
EX_DEVICE_FAILED_TO_RESPOND = 0x0B, // Not used
EX_GENERAL_FAILURE = 0xE1, // Custom. Unexpected master error
EX_DATA_MISMACH = 0xE2, // Custom. Inpud data size mismach
EX_UNEXPECTED_RESPONSE = 0xE3, // Custom. Returned result doesn't mach transaction
EX_TIMEOUT = 0xE4, // Custom. Operation not finished within reasonable time
EX_CONNECTION_LOST = 0xE5, // Custom. Connection with device lost
EX_CANCEL = 0xE6 // Custom. Transaction/request canceled
EX_SUCCESS = 0x00U, // Custom. No error
EX_ILLEGAL_FUNCTION = 0x01U, // Function Code not Supported
EX_ILLEGAL_ADDRESS = 0x02U, // Output Address not exists
EX_ILLEGAL_VALUE = 0x03U, // Output Value not in Range
EX_SLAVE_FAILURE = 0x04U, // Slave or Master Device Fails to process request
EX_ACKNOWLEDGE = 0x05U, // Not used
EX_SLAVE_DEVICE_BUSY = 0x06U, // Not used
EX_MEMORY_PARITY_ERROR = 0x08U, // Not used
EX_PATH_UNAVAILABLE = 0x0AU, // Not used
EX_DEVICE_FAILED_TO_RESPOND = 0x0BU, // Not used
EX_GENERAL_FAILURE = 0xE1U, // Custom. Unexpected master error
EX_DATA_MISMACH = 0xE2U, // Custom. Inpud data size mismach
EX_UNEXPECTED_RESPONSE = 0xE3U, // Custom. Returned result doesn't mach transaction
EX_TIMEOUT = 0xE4U, // Custom. Operation not finished within reasonable time
EX_CONNECTION_LOST = 0xE5U, // Custom. Connection with device lost
EX_CANCEL = 0xE6U // Custom. Transaction/request canceled
};
~Modbus();
bool addHreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);
bool addHreg(uint16_t offset, uint16_t value = 0U, uint16_t numregs = 1U);
bool Hreg(uint16_t offset, uint16_t value);
uint16_t Hreg(uint16_t offset);
uint16_t removeHreg(uint16_t offset, uint16_t numregs = 1);
uint16_t removeHreg(uint16_t offset, uint16_t numregs = 1U);
bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1);
bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1U);
bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1);
bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1U);
bool addIreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);
bool addIreg(uint16_t offset, uint16_t value = 0U, uint16_t numregs = 1U);
bool Coil(uint16_t offset, bool value);
@@ -185,11 +185,11 @@ public:
uint16_t Ireg(uint16_t offset);
bool removeCoil(uint16_t offset, uint16_t numregs = 1);
bool removeCoil(uint16_t offset, uint16_t numregs = 1U);
bool removeIsts(uint16_t offset, uint16_t numregs = 1);
bool removeIsts(uint16_t offset, uint16_t numregs = 1U);
bool removeIreg(uint16_t offset, uint16_t numregs = 1);
bool removeIreg(uint16_t offset, uint16_t numregs = 1U);
/*
bool Hreg(uint16_t offset, uint16_t* value);
@@ -253,19 +253,19 @@ private:
protected:
//Reply Types
enum ReplyCode {
REPLY_OFF = 0x01,
REPLY_ECHO = 0x02,
REPLY_NORMAL = 0x03,
REPLY_ERROR = 0x04,
REPLY_UNEXPECTED = 0x05
REPLY_OFF = 0x01U,
REPLY_ECHO = 0x02U,
REPLY_NORMAL = 0x03U,
REPLY_ERROR = 0x04U,
REPLY_UNEXPECTED = 0x05U
};
#ifndef MB_GLOBAL_REGS
std::vector<TRegister> _regs;
std::vector<TCallback> _callbacks;
#endif
uint8_t *_frame = nullptr;
uint16_t _len = 0;
uint8_t _reply = 0;
uint16_t _len = 0U;
uint8_t _reply = 0U;
bool cbEnabled = true;
uint16_t mbCallback(TRegister *reg, uint16_t val, TCallback::CallbackType t);

View File

@@ -23,8 +23,8 @@
//#define MB_STATIC_FRAME 1
class ModbusRTU : public Modbus {
protected:
CircularBuffer<char, 200> RX_RingBuff;
private:
CircularBuffer<char, 200U> RX_RingBuff;
UnbufferedSerial *_port;
DigitalOut *_txPin;
@@ -47,7 +47,7 @@ protected:
void *_data = nullptr;
uint8_t *_sentFrame = nullptr;
TAddress _sentReg = COIL(0);
uint16_t maxRegs = 0x007D;
uint16_t maxRegs = 0x007DU;
#ifdef ESP32
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
#endif