From 8d2f2a6486860f2ccfebf28f8686e7a2a69646a7 Mon Sep 17 00:00:00 2001 From: dooku Date: Mon, 16 Aug 2021 23:21:33 +0200 Subject: [PATCH] Added CO2 MODBUS readout --- fw/Core/Src/main.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/fw/Core/Src/main.c b/fw/Core/Src/main.c index 56f3423..8a1e1d7 100644 --- a/fw/Core/Src/main.c +++ b/fw/Core/Src/main.c @@ -50,6 +50,20 @@ const uint16_t tim21_prescaler = 60000-1; // 100Hz //const uint16_t tim21_period = 12000-1; // 60s const uint16_t tim21_period = 1200-1; // 6s //const uint16_t tim21_period = 200-1; // 1s +/* Variables to store the measured data */ +int CO2, T_SCD4x, RH_SCD4x; +int T_SHT4x, RH_SHT4x; +uint16_t sps30_measured_data[10]; +/* TODO maybe use union for measured value? + * this would make modbus register readout easier */ +union { + struct { + uint16_t CO2; + uint16_t T_SCD4x; + uint16_t RH_SCD4x; + }; + uint16_t array[3]; +} measured_values; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -66,6 +80,13 @@ static void MX_TIM21_Init(void); /* USER CODE BEGIN 0 */ int8_t modbus_slave_callback(modbus_transaction_t *transaction) { + if (transaction->function_code == MODBUS_READ_INPUT_REGISTERS) { + // TODO select reasonable register numbers and implement + // readout of all measured variables + if (transaction->register_number == 30010) { // register number 30010 (randomly selected) + transaction->input_registers[0] = CO2; + } + } return MODBUS_ERROR_FUNCTION_NOT_IMPLEMENTED; /* nothing implemented yet! TODO */ } @@ -147,11 +168,6 @@ int main(void) sps30_is_connected = 1; } - /* Variables to store the measured data */ - int CO2, T_SCD4x, RH_SCD4x; - int T_SHT4x, RH_SHT4x; - uint16_t sps30_measured_data[10]; - /* Wait 1000ms for sensors initialization */ /* SHT4x Init Time: max 1 ms (datasheet pg. 8) */ /* SCD4x Init Time: max 1000 ms (datasheet pg. 6) */