Skip to content

Commit bdd9426

Browse files
Maksim Salaunashif
authored andcommitted
modbus: serial: Add non-compliant mode with custom stop-bit settings
The mode is activated by the CONFIG_MODBUS_NONCOMPLIANT_SERIAL_MODE option and allows any stop-bit setting for the serial port. Signed-off-by: Maksim Salau <[email protected]>
1 parent 6e0d0f5 commit bdd9426

File tree

7 files changed

+14
-13
lines changed

7 files changed

+14
-13
lines changed

drivers/sensor/pzem004t/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ config PZEM004T
1010
depends on MODBUS
1111
depends on MODBUS_SERIAL
1212
depends on MODBUS_RAW_ADU
13+
select MODBUS_NONCOMPLIANT_SERIAL_MODE
1314
help
1415
Enable the driver for the Peacefair PZEM004T Multifunction AC Meter.
1516

drivers/sensor/pzem004t/pzem004t.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static const struct pzem004t_config pzem004t_config_##inst = {
325325
{ \
326326
.baud = 9600, \
327327
.parity = UART_CFG_PARITY_NONE, \
328-
.stop_bits_client = UART_CFG_STOP_BITS_1, \
328+
.stop_bits = UART_CFG_STOP_BITS_1, \
329329
}, \
330330
}, \
331331
}; \

include/zephyr/modbus/modbus.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,13 @@ struct modbus_serial_param {
484484
* UART_CFG_PARITY_ODD
485485
*/
486486
enum uart_config_parity parity;
487-
/** stop_bits_client UART's stop bits setting if in client mode:
487+
/** stop_bits UART's stop bits setting in non-compliant mode:
488488
* UART_CFG_STOP_BITS_0_5,
489489
* UART_CFG_STOP_BITS_1,
490490
* UART_CFG_STOP_BITS_1_5,
491491
* UART_CFG_STOP_BITS_2,
492492
*/
493-
enum uart_config_stop_bits stop_bits_client;
493+
enum uart_config_stop_bits stop_bits;
494494
};
495495

496496
/**

samples/subsys/modbus/rtu_client/src/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const static struct modbus_iface_param client_param = {
1919
.serial = {
2020
.baud = 19200,
2121
.parity = UART_CFG_PARITY_NONE,
22-
.stop_bits_client = UART_CFG_STOP_BITS_2,
2322
},
2423
};
2524

subsys/modbus/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ config MODBUS_FC08_DIAGNOSTIC
7979
help
8080
Enable function code 08 Diagnostic support
8181

82+
config MODBUS_NONCOMPLIANT_SERIAL_MODE
83+
bool "Non-compliant serial mode"
84+
depends on MODBUS_SERIAL
85+
help
86+
Allow non-compliant stop and parity bit settings.
87+
8288
module = MODBUS
8389
module-str = Modbus Support
8490
module-help = Sets log level for Modbus support

subsys/modbus/modbus_serial.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,14 @@ static inline int configure_uart(struct modbus_context *ctx,
477477
return -EINVAL;
478478
}
479479

480-
if (ctx->client) {
481-
/* Allow custom stop bit settings only in client mode */
482-
switch (param->serial.stop_bits_client) {
480+
if (IS_ENABLED(CONFIG_MODBUS_NONCOMPLIANT_SERIAL_MODE)) {
481+
/* Allow custom stop bit settings only in non-compliant mode */
482+
switch (param->serial.stop_bits) {
483483
case UART_CFG_STOP_BITS_0_5:
484484
case UART_CFG_STOP_BITS_1:
485485
case UART_CFG_STOP_BITS_1_5:
486486
case UART_CFG_STOP_BITS_2:
487-
uart_cfg.stop_bits = param->serial.stop_bits_client;
487+
uart_cfg.stop_bits = param->serial.stop_bits;
488488
break;
489489
default:
490490
return -EINVAL;

tests/subsys/modbus/src/test_modbus_client.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ static struct modbus_iface_param client_param = {
216216
.serial = {
217217
.baud = MB_TEST_BAUDRATE_LOW,
218218
.parity = UART_CFG_PARITY_ODD,
219-
.stop_bits_client = UART_CFG_STOP_BITS_1,
220219
},
221220
};
222221

@@ -241,7 +240,6 @@ void test_client_setup_low_none(void)
241240
client_param.mode = MODBUS_MODE_RTU;
242241
client_param.serial.baud = MB_TEST_BAUDRATE_LOW;
243242
client_param.serial.parity = UART_CFG_PARITY_NONE;
244-
client_param.serial.stop_bits_client = UART_CFG_STOP_BITS_2;
245243

246244
err = modbus_init_client(client_iface, client_param);
247245
zassert_equal(err, 0, "Failed to configure RTU client");
@@ -255,7 +253,6 @@ void test_client_setup_low_odd(void)
255253
client_param.mode = MODBUS_MODE_RTU;
256254
client_param.serial.baud = MB_TEST_BAUDRATE_LOW;
257255
client_param.serial.parity = UART_CFG_PARITY_ODD;
258-
client_param.serial.stop_bits_client = UART_CFG_STOP_BITS_1;
259256

260257
err = modbus_init_client(client_iface, client_param);
261258
zassert_equal(err, 0, "Failed to configure RTU client");
@@ -269,7 +266,6 @@ void test_client_setup_high_even(void)
269266
client_param.mode = MODBUS_MODE_RTU;
270267
client_param.serial.baud = MB_TEST_BAUDRATE_HIGH;
271268
client_param.serial.parity = UART_CFG_PARITY_EVEN;
272-
client_param.serial.stop_bits_client = UART_CFG_STOP_BITS_1;
273269

274270
err = modbus_init_client(client_iface, client_param);
275271
zassert_equal(err, 0, "Failed to configure RTU client");
@@ -283,7 +279,6 @@ void test_client_setup_ascii(void)
283279
client_param.mode = MODBUS_MODE_ASCII;
284280
client_param.serial.baud = MB_TEST_BAUDRATE_HIGH;
285281
client_param.serial.parity = UART_CFG_PARITY_EVEN;
286-
client_param.serial.stop_bits_client = UART_CFG_STOP_BITS_1;
287282

288283
err = modbus_init_client(client_iface, client_param);
289284

0 commit comments

Comments
 (0)