minimal changes

This commit is contained in:
Eduard Iten 2025-06-13 08:26:49 +02:00
parent adc6275c55
commit 5ab33e3a7b
2 changed files with 20 additions and 24 deletions

View File

@ -4,8 +4,7 @@
#include <zephyr/modbus/modbus.h> #include <zephyr/modbus/modbus.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
// LOG_MODULE_REGISTER(mbc, CONFIG_LOG_DEFAULT_LEVEL); LOG_MODULE_REGISTER(mbc, CONFIG_LOG_DEFAULT_LEVEL);
LOG_MODULE_REGISTER(mbc, 4); // Set log level to 4 (Debug)
#define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial) #define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)

View File

@ -1,8 +1,9 @@
/* Testing MODBUS functionality /* Testing MODBUS functionality
This code initializes a Modbus client, sets minimum and maximum water levels, This code initializes a Modbus client, sets the zero point and the 2m point for water level,
reads the current water level, and logs the results. and reads the water level in both meters and millimeters.
You can set the zero point and the 2m point for water level in lines 32 and 38. It uses the Zephyr RTOS and its Modbus library to communicate with a Modbus server.
*/ */
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio.h>
@ -56,26 +57,22 @@ int main(void)
LOG_INF("Water zero point is set to: %dmm", water_level_min_mm); LOG_INF("Water zero point is set to: %dmm", water_level_min_mm);
LOG_INF("Water 2m point is set to: %dmm", water_level_max_mm); LOG_INF("Water 2m point is set to: %dmm", water_level_max_mm);
while (1) /* Read water level */
{ /* Read water level */ rc = mb_read_water_level(&water_level);
rc = mb_read_water_level(&water_level); if (rc < 0)
if (rc < 0) {
{ LOG_ERR("Failed to read water level: %d", rc);
LOG_ERR("Failed to read water level: %d", rc); return rc;
return rc;
}
rc = mb_read_water_level_mm(&water_level_mm);
if (rc < 0)
{
LOG_ERR("Failed to read water level: %d", rc);
return rc;
}
LOG_INF("Water level: %.3fm", water_level);
LOG_INF("Water level: %dmm", water_level_mm);
LOG_INF("Modbus test completed successfully");
return 0;
k_sleep(K_SECONDS(1));
} }
rc = mb_read_water_level_mm(&water_level_mm);
if (rc < 0)
{
LOG_ERR("Failed to read water level: %d", rc);
return rc;
}
LOG_INF("Water level: %.3fm", water_level);
LOG_INF("Water level: %dmm", water_level_mm);
LOG_INF("Modbus test completed successfully");
return 0; return 0;
} }