|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (C) 2024 Bobby Noelte |
| 4 | + */ |
| 5 | +#include <array> |
| 6 | +#include <cstring> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +// OpenDTU |
| 10 | +#include "ModbusDtu.h" |
| 11 | + |
| 12 | + |
| 13 | +ModbusDTUMessage::ModbusDTUMessage(uint16_t dataLen = 0) : ModbusMessage(dataLen) { |
| 14 | + value.val_float = NAN; |
| 15 | +} |
| 16 | + |
| 17 | +ModbusDTUMessage::ModbusDTUMessage(std::vector<uint8_t> s) : ModbusMessage(s) { |
| 18 | + value.val_float = NAN; |
| 19 | +} |
| 20 | + |
| 21 | +uint16_t ModbusDTUMessage::addFloat32(const float_t &val, const size_t reg_offset) { |
| 22 | + // Use union to convert from float to uint32 |
| 23 | + value.val_float = val; |
| 24 | + |
| 25 | + return addUInt32(value.val_u32, reg_offset); |
| 26 | +} |
| 27 | + |
| 28 | +uint16_t ModbusDTUMessage::addFloatAsDecimalFixedPoint32(const float_t &val, const float &precision, const size_t reg_offset) { |
| 29 | + // Check if value is already converted to fixed point |
| 30 | + if (value.val_float != val) { |
| 31 | + // Multiply by 10^precision to shift the decimal point |
| 32 | + // Round the scaled value to the nearest integer |
| 33 | + // Use union to convert from fixed point to uint32 |
| 34 | + value.val_i32 = round(val * std::pow(10, precision)); |
| 35 | + // remember converted value |
| 36 | + conv.fixed_point_u32 = value.val_u32; |
| 37 | + // mark conversion |
| 38 | + value.val_float = val; |
| 39 | + } |
| 40 | + |
| 41 | + return addUInt32(conv.fixed_point_u32, reg_offset); |
| 42 | +} |
| 43 | + |
| 44 | +uint16_t ModbusDTUMessage::addFloatAsDecimalFixedPoint16(const float_t &val, const float &precision) { |
| 45 | + // Multiply by 10^precision to shift the decimal point |
| 46 | + // Round the scaled value to the nearest integer |
| 47 | + // Use union to convert from fixed point to uint16 |
| 48 | + value.val_i16 = round(val * std::pow(10, precision)); |
| 49 | + |
| 50 | + add(value.val_u16); |
| 51 | + return value.val_u16; |
| 52 | +} |
| 53 | + |
| 54 | +uint16_t ModbusDTUMessage::addString(const char * const str, const size_t length, const size_t reg_offset) { |
| 55 | + // Check if the position is within the bounds of the string |
| 56 | + size_t offset = reg_offset * sizeof(uint16_t); |
| 57 | + if (offset + sizeof(uint16_t) <= length) { |
| 58 | + // Reinterpret the memory at position 'offset' as uint16_t |
| 59 | + std::memcpy(&value.val_u16, str + offset, sizeof(uint16_t)); |
| 60 | + } else { |
| 61 | + value.val_u16 = 0; |
| 62 | + } |
| 63 | + |
| 64 | + add(value.val_u16); |
| 65 | + return value.val_u16; |
| 66 | +} |
| 67 | + |
| 68 | +uint16_t ModbusDTUMessage::addString(const String &str, const size_t reg_offset) { |
| 69 | + return addString(str.c_str(), str.length(), reg_offset); |
| 70 | +} |
| 71 | + |
| 72 | +uint16_t ModbusDTUMessage::addUInt16(const uint16_t val) { |
| 73 | + add(val); |
| 74 | + return val; |
| 75 | +} |
| 76 | + |
| 77 | +uint16_t ModbusDTUMessage::addUInt32(const uint32_t val, const size_t reg_offset) { |
| 78 | + if (reg_offset <= 1) { |
| 79 | + value.val_u16 = val >> (16 * (1 - reg_offset)); |
| 80 | + } else { |
| 81 | + value.val_u16 = 0; |
| 82 | + } |
| 83 | + add(value.val_u16); |
| 84 | + return value.val_u16; |
| 85 | +} |
| 86 | + |
| 87 | +uint16_t ModbusDTUMessage::addUInt64(const uint64_t val, const size_t reg_offset) { |
| 88 | + if (reg_offset <= 3) { |
| 89 | + value.val_u16 = val >> (16 * (3 - reg_offset)); |
| 90 | + } else { |
| 91 | + value.val_u16 = 0; |
| 92 | + } |
| 93 | + add(value.val_u16); |
| 94 | + return value.val_u16; |
| 95 | +} |
| 96 | + |
| 97 | +uint16_t ModbusDTUMessage::addUInt64AsHexString(const uint64_t val, const size_t reg_offset) { |
| 98 | + // Check if value is already converted to hex string |
| 99 | + if (val != value.val_u64) { |
| 100 | + snprintf(&conv.u64_hex_str[0], sizeof(conv.u64_hex_str), "%0x%08x", |
| 101 | + ((uint32_t)((val >> 32) & 0xFFFFFFFFUL)), |
| 102 | + ((uint32_t)(val & 0xFFFFFFFFUL))); |
| 103 | + // mark conversion |
| 104 | + value.val_u64 = val; |
| 105 | + } |
| 106 | + |
| 107 | + return addString(&conv.u64_hex_str[0], sizeof(conv.u64_hex_str), reg_offset); |
| 108 | +} |
| 109 | + |
| 110 | +uint16_t ModbusDTUMessage::addIPAddressAsString(const IPAddress val, const size_t reg_offset) { |
| 111 | + // Check if value is already converted to hex string |
| 112 | + if (val != value.val_ip) { |
| 113 | + String str(val.toString()); |
| 114 | + std::memcpy(&conv.ip_str, str.c_str(), std::min(sizeof(conv.ip_str), str.length())); |
| 115 | + // mark conversion |
| 116 | + value.val_ip = val; |
| 117 | + } |
| 118 | + |
| 119 | + return addString(&conv.ip_str[0], sizeof(conv.ip_str), reg_offset); |
| 120 | +} |
| 121 | + |
| 122 | +// Create server(s) |
| 123 | +ModbusServerTCPasync ModbusTCPServer; |
0 commit comments