|
| 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 | +void 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 | + addUInt32(value.val_u32, reg_offset); |
| 26 | +} |
| 27 | + |
| 28 | +void ModbusDTUMessage::addFloat32AsDecimalFixedPoint(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 | + addUInt32(conv.fixed_point_u32, reg_offset); |
| 42 | +} |
| 43 | + |
| 44 | +void ModbusDTUMessage::addString(const char * const str, const size_t length, const size_t reg_offset) { |
| 45 | + // Check if the position is within the bounds of the string |
| 46 | + size_t offset = reg_offset * sizeof(uint16_t); |
| 47 | + if (offset + sizeof(uint16_t) <= length) { |
| 48 | + // Reinterpret the memory at position 'offset' as uint16_t |
| 49 | + std::memcpy(&value.val_u16, str + offset, sizeof(uint16_t)); |
| 50 | + } else { |
| 51 | + value.val_u16 = 0; |
| 52 | + } |
| 53 | + |
| 54 | + add(value.val_u16); |
| 55 | +} |
| 56 | + |
| 57 | +void ModbusDTUMessage::addString(const String &str, const size_t reg_offset) { |
| 58 | + addString(str.c_str(), str.length(), reg_offset); |
| 59 | +} |
| 60 | + |
| 61 | +void ModbusDTUMessage::addUInt32(const uint32_t val, const size_t reg_offset) { |
| 62 | + if (reg_offset <= 1) { |
| 63 | + add((uint16_t)(val >> 16 * (1 - reg_offset))); |
| 64 | + } else { |
| 65 | + add((uint16_t)0); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void ModbusDTUMessage::addUInt64(const uint64_t val, const size_t reg_offset) { |
| 70 | + if (reg_offset <= 3) { |
| 71 | + add((uint16_t)(val >> 16 * (3 - reg_offset))); |
| 72 | + } else { |
| 73 | + add((uint16_t)0); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void ModbusDTUMessage::addUInt64AsHexString(const uint64_t val, const size_t reg_offset) { |
| 78 | + // Check if value is already converted to hex string |
| 79 | + if (val != value.val_u64) { |
| 80 | + snprintf(&conv.u64_hex_str[0], sizeof(conv.u64_hex_str), "%0x%08x", |
| 81 | + ((uint32_t)((val >> 32) & 0xFFFFFFFF)), |
| 82 | + ((uint32_t)(val & 0xFFFFFFFF))); |
| 83 | + // mark conversion |
| 84 | + value.val_u64 = val; |
| 85 | + } |
| 86 | + |
| 87 | + addString(&conv.u64_hex_str[0], sizeof(conv.u64_hex_str), reg_offset); |
| 88 | +} |
| 89 | + |
| 90 | +void ModbusDTUMessage::addUInt64AsDecimalDigits(const uint64_t val, const size_t reg_offset) { |
| 91 | + if (val != value.val_u64) { |
| 92 | + value.val_u64 = val; |
| 93 | + // Extract digits from the number |
| 94 | + for (int i = 6 - 1; i >= 0; i--) { |
| 95 | + conv.u64_dec_digits[i] = value.val_u64 % 10; // Extract the least significant digit |
| 96 | + value.val_u64 /= 10; // Remove the least significant digit |
| 97 | + conv.u64_dec_digits[i] += (value.val_u64 % 10) << 8; // Extract the least significant digit |
| 98 | + value.val_u64 /= 10; // Remove the least significant digit |
| 99 | + } |
| 100 | + // mark conversion |
| 101 | + value.val_u64 = val; |
| 102 | + } |
| 103 | + |
| 104 | + if (reg_offset < 6) { |
| 105 | + add(conv.u64_dec_digits[reg_offset]); |
| 106 | + } else { |
| 107 | + add((uint16_t)0); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +void ModbusDTUMessage::addIPAddressAsString(const IPAddress val, const size_t reg_offset) { |
| 112 | + // Check if value is already converted to hex string |
| 113 | + if (val != value.val_ip) { |
| 114 | + String str(val.toString()); |
| 115 | + std::memcpy(&conv.ip_str, str.c_str(), std::min(sizeof(conv.ip_str), str.length())); |
| 116 | + // mark conversion |
| 117 | + value.val_ip = val; |
| 118 | + } |
| 119 | + |
| 120 | + addString(&conv.ip_str[0], sizeof(conv.ip_str), reg_offset); |
| 121 | +} |
| 122 | + |
| 123 | +// Create server(s) |
| 124 | +ModbusServerTCPasync ModbusTCPServer; |
0 commit comments