1#ifndef IV_SRC_COMMS_MODBUS_FUNCTIONS_HPP_
2#define IV_SRC_COMMS_MODBUS_FUNCTIONS_HPP_
18 uint16_t crc = 0xFFFF;
20 for (int32_t pos = 0; pos < length; pos++)
22 crc ^=
static_cast<uint16_t
>(buf[pos]);
24 for (int32_t i = 8; i != 0; i--)
26 if ((crc & 0x0001) != 0)
69 if (registerTypeValue == 0)
76 return fmt::format(
"{}{:05}", registerTypeValue, address);
79 return fmt::format(
"{}{:04}", registerTypeValue, address);
82inline std::vector<iv::types::comms::modbus::registerValue>
84 const std::vector<iv::types::comms::modbus::registerValue> &values)
86 std::vector<iv::types::comms::modbus::registerValue> processedValues;
91 processedValues = values;
99 for (
const auto value: values)
104 std::ranges::reverse(processedValues);
108 processedValues = values;
109 std::ranges::reverse(processedValues);
113 for (
const auto value: values)
120 processedValues = values;
129 for (
const auto value: values)
133 std::ranges::reverse(processedValues.begin(), processedValues.end());
137 processedValues = values;
138 std::ranges::reverse(processedValues.begin(), processedValues.end());
142 for (
const auto value: values)
149 processedValues = values;
154 return processedValues;
159 int64_t channelValue = 0;
163 if (values.size() == 5)
166 const auto mask = (
static_cast<uint16_t
>(1) << numOfLastRegBits) - 1;
168 for (uint32_t i = 0; i < values.size(); i++)
170 channelValue |= values[i] << (i * 16);
180 channelValue |= lastRegValue;
184 for (uint32_t i = 0; i < values.size(); i++)
186 channelValue |= values[i] << (i * 16);
196 uint64_t mask = std::numeric_limits<uint64_t>::max();
203 channelValue &=
static_cast<int64_t
>(mask);
209 const uint64_t channelValue)
211 std::optional<iv::types::channelValue> value;
216 value =
static_cast<bool>(channelValue);
221 std::memcpy(&valueInt16, &channelValue,
sizeof(int16_t));
226 uint16_t valueUint16;
227 std::memcpy(&valueUint16, &channelValue,
sizeof(uint16_t));
233 std::memcpy(&valueInt32, &channelValue,
sizeof(int32_t));
238 uint32_t valueUint32;
239 std::memcpy(&valueUint32, &channelValue,
sizeof(uint32_t));
245 std::memcpy(&valueFloat, &channelValue,
sizeof(
float));
251 std::memcpy(&valueInt64, &channelValue,
sizeof(int64_t));
256 uint64_t valueUint64;
257 std::memcpy(&valueUint64, &channelValue,
sizeof(uint64_t));
263 std::memcpy(&
valueDouble, &channelValue,
sizeof(
double));
constexpr uint16_t reverseBytes(const uint16_t x)
Definition bswapOps.hpp:16
Definition AbstractModbusCommunicationProtocol.cpp:8
std::string getCalculatedDirection(const iv::comms::modbus::eRegisterType registerType, iv::types::comms::modbus::modbusAddress address, bool isExtended)
Definition functions.hpp:64
uint16_t getRegisterTypeValue(const iv::comms::modbus::eRegisterType registerType)
Definition functions.hpp:41
int64_t applyMask(const iv::channels::ModbusRegisterInfo &modbusInfo, const std::vector< int64_t > &values)
Definition functions.hpp:157
std::optional< iv::types::channelValue > processValue(const iv::channels::ModbusRegisterInfo &modbusInfo, const uint64_t channelValue)
Definition functions.hpp:208
uint16_t calculateCrcToModbusRTU(const uint8_t *buf, const int32_t length)
Definition functions.hpp:16
std::vector< iv::types::comms::modbus::registerValue > applyEndianness(const iv::channels::ModbusRegisterInfo &modbusInfo, const std::vector< iv::types::comms::modbus::registerValue > &values)
Definition functions.hpp:83
eRegisterType
Definition enums.hpp:315
uint16_t modbusAddress
Definition defines.hpp:37
std::string formatNumber(T number, std::optional< uint8_t > precision=std::nullopt)
Check if a string is a number, only applies precision to floating point numbers.
Definition stringUtils.hpp:343
@ LittleEndianByteSwapped
Definition structs.hpp:509
iv::comms::modbus::eRegisterType type
Definition structs.hpp:510
uint8_t registerBitsLength
Definition structs.hpp:519
iv::eDataType dataType
Definition structs.hpp:514
uint8_t bitPosition
Definition structs.hpp:520
iv::eEndianType endianType
Definition structs.hpp:515
Definition sNmeaData.hpp:8