Added modbus lib and test
This commit is contained in:
1
software/modbus_test/.gitignore
vendored
Normal file
1
software/modbus_test/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
build
|
||||
16
software/modbus_test/.vscode/c_cpp_properties.json
vendored
Normal file
16
software/modbus_test/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Win32",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"defines": [
|
||||
"_DEBUG",
|
||||
"UNICODE",
|
||||
"_UNICODE"
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
14
software/modbus_test/CMakeLists.txt
Normal file
14
software/modbus_test/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
|
||||
list(APPEND BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
||||
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(hello_world)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
target_sources(app PRIVATE ../lib/modbus.c)
|
||||
target_include_directories(app PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../lib
|
||||
)
|
||||
9
software/modbus_test/boards/nucleo_f103rb.overlay
Normal file
9
software/modbus_test/boards/nucleo_f103rb.overlay
Normal file
@@ -0,0 +1,9 @@
|
||||
&usart1 {
|
||||
status = "okay";
|
||||
current-speed = <9600>;
|
||||
modbus0 {
|
||||
compatible = "zephyr,modbus-serial";
|
||||
status = "okay";
|
||||
// de-gpios = <&arduino_header 15 GPIO_ACTIVE_LOW>; /* D9 */
|
||||
};
|
||||
};
|
||||
13
software/modbus_test/prj.conf
Normal file
13
software/modbus_test/prj.conf
Normal file
@@ -0,0 +1,13 @@
|
||||
CONFIG_LOG=y
|
||||
CONFIG_LOG_DEFAULT_LEVEL=3
|
||||
CONFIG_CBPRINTF_FP_SUPPORT=y
|
||||
|
||||
CONFIG_UART_CONSOLE=y # Console on USART1
|
||||
#CONFIG_RTT_CONSOLE=y
|
||||
#CONFIG_USE_SEGGER_RTT=y
|
||||
|
||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||
CONFIG_UART_LINE_CTRL=n
|
||||
|
||||
CONFIG_MODBUS=y
|
||||
CONFIG_MODBUS_ROLE_CLIENT=y
|
||||
81
software/modbus_test/src/main.c
Normal file
81
software/modbus_test/src/main.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/* Testing MODBUS functionality
|
||||
This code initializes a Modbus client, sets minimum and maximum water levels,
|
||||
reads the current water level, and logs the results.
|
||||
You can set the zero point and the 2m point for water level in lines 32 and 38.
|
||||
*/
|
||||
#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);
|
||||
|
||||
while (1)
|
||||
{ /* 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;
|
||||
k_sleep(K_SECONDS(1));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user