renamed modbus test to fit other tests I will add
This commit is contained in:
78
software/test_modbus/src/main.c
Normal file
78
software/test_modbus/src/main.c
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Testing MODBUS functionality
|
||||
This code initializes a Modbus client, sets the zero point and the 2m point for water level,
|
||||
and reads the water level in both meters and millimeters.
|
||||
It uses the Zephyr RTOS and its Modbus library to communicate with a Modbus server.
|
||||
*/
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
#include "modbus.h"
|
||||
|
||||
LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
|
||||
|
||||
/* 1000 msec = 1 sec */
|
||||
#define SLEEP_TIME_MS 1000
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = mb_init_client();
|
||||
if (rc != 0)
|
||||
{
|
||||
LOG_ERR("Failed to initialize Modbus client: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
LOG_INF("Modbus client initialized successfully");
|
||||
|
||||
double water_level = 0.0;
|
||||
int water_level_mm, water_level_min_mm, water_level_max_mm = 0;
|
||||
|
||||
rc = mb_write_minimum_mm(42); // Set the zero point for water level
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to write minimum water level: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
rc = mb_write_maximum_mm(2000); // Set the 2m point for water level
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to write maximum water level: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
rc = mb_read_minimum_mm(&water_level_min_mm);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read minimum water level: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
rc = mb_read_maximum_mm(&water_level_max_mm);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read maximum water level: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
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);
|
||||
|
||||
/* Read water level */
|
||||
rc = mb_read_water_level(&water_level);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read water level: %d", 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;
|
||||
}
|
||||
Reference in New Issue
Block a user