feat(shell): Add commands to configure Modbus
- Implement a new 'modbus' command in the shell. - Add sub-commands 'set_baud', 'set_id', and 'show'. - Add validation for baud rate and slave ID inputs. - The new parameters are applied to the Modbus server at runtime, allowing for live reconfiguration of the communication settings. - The shell backend is set to RTT.
This commit is contained in:
@@ -12,6 +12,8 @@
|
||||
#include <zephyr/usb/usb_device.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
#include "modbus_bridge.h"
|
||||
|
||||
LOG_MODULE_REGISTER(mbs_sample, LOG_LEVEL_INF);
|
||||
|
||||
#define APP_VERSION_MAJOR 1
|
||||
@@ -32,6 +34,20 @@ enum {
|
||||
|
||||
|
||||
static uint16_t watchdog_timeout_s;
|
||||
static int modbus_iface;
|
||||
|
||||
static struct modbus_iface_param server_param = {
|
||||
.mode = MODBUS_MODE_RTU,
|
||||
.server = {
|
||||
.user_cb = NULL, // Will be set later
|
||||
.unit_id = 1,
|
||||
},
|
||||
.serial = {
|
||||
.baud = 19200,
|
||||
.parity = UART_CFG_PARITY_NONE,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
static int coil_rd(uint16_t addr, bool *state)
|
||||
{
|
||||
@@ -112,33 +128,56 @@ static struct modbus_user_callbacks mbs_cbs = {
|
||||
.input_reg_rd = input_reg_rd,
|
||||
};
|
||||
|
||||
const static struct modbus_iface_param server_param = {
|
||||
.mode = MODBUS_MODE_RTU,
|
||||
.server = {
|
||||
.user_cb = &mbs_cbs,
|
||||
.unit_id = 1,
|
||||
},
|
||||
.serial = {
|
||||
.baud = 19200,
|
||||
.parity = UART_CFG_PARITY_NONE,
|
||||
},
|
||||
};
|
||||
|
||||
#define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)
|
||||
|
||||
int modbus_reconfigure(uint32_t baudrate, uint8_t unit_id)
|
||||
{
|
||||
int err;
|
||||
|
||||
LOG_INF("Reconfiguring Modbus: baudrate=%u, id=%u", baudrate, unit_id);
|
||||
|
||||
err = modbus_disable(modbus_iface);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to disable Modbus: %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
server_param.serial.baud = baudrate;
|
||||
server_param.server.unit_id = unit_id;
|
||||
|
||||
err = modbus_init_server(modbus_iface, server_param);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to re-init Modbus server: %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t modbus_get_baudrate(void)
|
||||
{
|
||||
return server_param.serial.baud;
|
||||
}
|
||||
|
||||
uint8_t modbus_get_unit_id(void)
|
||||
{
|
||||
return server_param.server.unit_id;
|
||||
}
|
||||
|
||||
|
||||
static int init_modbus_server(void)
|
||||
{
|
||||
const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};
|
||||
int iface;
|
||||
|
||||
iface = modbus_iface_get_by_name(iface_name);
|
||||
|
||||
if (iface < 0) {
|
||||
modbus_iface = modbus_iface_get_by_name(iface_name);
|
||||
if (modbus_iface < 0) {
|
||||
LOG_ERR("Failed to get iface index for %s", iface_name);
|
||||
return iface;
|
||||
return modbus_iface;
|
||||
}
|
||||
|
||||
return modbus_init_server(iface, server_param);
|
||||
server_param.server.user_cb = &mbs_cbs;
|
||||
|
||||
return modbus_init_server(modbus_iface, server_param);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
@@ -154,4 +193,4 @@ int main(void)
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user