72 lines
3.0 KiB
C++
72 lines
3.0 KiB
C++
#include "modbus_temperature_humidity.h"
|
|
|
|
// --- Generic Data Conversion Functions ---
|
|
int16_t bytesToSignedInt16(uint16_t val) {
|
|
return static_cast<int16_t>(val);
|
|
}
|
|
|
|
// --- Specific Sensor Data Parsing Functions (e.g., for JWST-20 sensor) ---
|
|
// Note: BAUDRATE_MAP is defined in the header and externs it.
|
|
// If it were static const and only used here, it would be defined here.
|
|
|
|
float parseTemperature(uint16_t raw_value) {
|
|
// Assuming temperature is signed 16-bit integer, scaled by 10
|
|
return static_cast<float>(bytesToSignedInt16(raw_value)) / 10.0f;
|
|
}
|
|
|
|
float parseHumidity(uint16_t raw_value) {
|
|
// Assuming humidity is unsigned 16-bit integer, scaled by 10
|
|
return static_cast<float>(raw_value) / 10.0f;
|
|
}
|
|
|
|
std::string parseAlarmStatus(uint16_t raw_value) {
|
|
uint8_t alarm_code = static_cast<uint8_t>(raw_value & 0xFF);
|
|
switch (alarm_code) {
|
|
case 0x00: return "No Alarm";
|
|
case 0x01: return "Upper Limit Alarm";
|
|
case 0x02: return "Lower Limit Alarm";
|
|
default: return "Unknown Alarm Status (Code: 0x" + std::to_string(static_cast<int>(alarm_code)) + ")";
|
|
}
|
|
}
|
|
|
|
uint32_t parseBaudrate(uint16_t raw_value) {
|
|
auto it = BAUDRATE_MAP.find(raw_value);
|
|
if (it != BAUDRATE_MAP.end()) {
|
|
return it->second;
|
|
}
|
|
return 0; // Return 0 for unknown baudrate code, or throw an exception
|
|
}
|
|
|
|
void printRegisters(const std::string& description, const std::vector<uint16_t>& registers) {
|
|
if (registers.empty()) {
|
|
std::cout << description << ": No data." << std::endl;
|
|
return;
|
|
}
|
|
|
|
std::cout << "--- " << description << " ---" << std::endl;
|
|
for (size_t i = 0; i < registers.size(); ++i) {
|
|
uint16_t raw_val = registers[i];
|
|
std::cout << " Register " << i << " (0x" << std::setw(4) << std::setfill('0') << std::hex << raw_val << std::dec << "): ";
|
|
|
|
if (description.find("Temperature") != std::string::npos && description.find("Alarm") == std::string::npos) {
|
|
std::cout << std::fixed << std::setprecision(1) << parseTemperature(raw_val) << " °C";
|
|
} else if (description.find("Humidity") != std::string::npos && description.find("Alarm") == std::string::npos) {
|
|
std::cout << std::fixed << std::setprecision(1) << parseHumidity(raw_val) << " %RH";
|
|
} else if (description.find("Alarm Status") != std::string::npos) {
|
|
std::cout << parseAlarmStatus(raw_val);
|
|
} else if (description.find("Modbus Address") != std::string::npos) {
|
|
std::cout << "Slave ID: " << raw_val;
|
|
} else if (description.find("Baudrate") != std::string::npos) {
|
|
uint32_t detected_baudrate = parseBaudrate(raw_val);
|
|
if (detected_baudrate != 0) {
|
|
std::cout << "Code: " << raw_val << " (" << detected_baudrate << " bps)";
|
|
} else {
|
|
std::cout << "Code: " << raw_val << " (Unknown baudrate code)";
|
|
}
|
|
} else {
|
|
std::cout << raw_val; // Default to raw value if no specific parser matches
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
}
|