1 /*
2  * Copyright (c) 2020 PHYTEC Messtechnik GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/util.h>
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/modbus/modbus.h>
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(mbc_sample, LOG_LEVEL_INF);
14 
15 static int client_iface;
16 
17 const static struct modbus_iface_param client_param = {
18 	.mode = MODBUS_MODE_RTU,
19 	.rx_timeout = 50000,
20 	.serial = {
21 		.baud = 19200,
22 		.parity = UART_CFG_PARITY_NONE,
23 		.stop_bits_client = UART_CFG_STOP_BITS_2,
24 	},
25 };
26 
27 #define MODBUS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_modbus_serial)
28 
init_modbus_client(void)29 static int init_modbus_client(void)
30 {
31 	const char iface_name[] = {DEVICE_DT_NAME(MODBUS_NODE)};
32 
33 	client_iface = modbus_iface_get_by_name(iface_name);
34 
35 	return modbus_init_client(client_iface, client_param);
36 }
37 
main(void)38 int main(void)
39 {
40 	uint16_t holding_reg[8] = {'H', 'e', 'l', 'l', 'o'};
41 	const uint8_t coil_qty = 3;
42 	uint8_t coil[1] = {0};
43 	const int32_t sleep = 250;
44 	static uint8_t node = 1;
45 	int err;
46 
47 
48 	if (init_modbus_client()) {
49 		LOG_ERR("Modbus RTU client initialization failed");
50 		return 0;
51 	}
52 
53 	err = modbus_write_holding_regs(client_iface, node, 0, holding_reg,
54 					ARRAY_SIZE(holding_reg));
55 	if (err != 0) {
56 		LOG_ERR("FC16 failed with %d", err);
57 		return 0;
58 	}
59 
60 	err = modbus_read_holding_regs(client_iface, node, 0, holding_reg,
61 				       ARRAY_SIZE(holding_reg));
62 	if (err != 0) {
63 		LOG_ERR("FC03 failed with %d", err);
64 		return 0;
65 	}
66 
67 	LOG_HEXDUMP_INF(holding_reg, sizeof(holding_reg),
68 			"WR|RD holding register:");
69 
70 	while (true) {
71 		uint16_t addr = 0;
72 
73 		err = modbus_read_coils(client_iface, node, 0, coil, coil_qty);
74 		if (err != 0) {
75 			LOG_ERR("FC01 failed with %d", err);
76 			return 0;
77 		}
78 
79 		LOG_INF("Coils state 0x%02x", coil[0]);
80 
81 		err = modbus_write_coil(client_iface, node, addr++, true);
82 		if (err != 0) {
83 			LOG_ERR("FC05 failed with %d", err);
84 			return 0;
85 		}
86 
87 		k_msleep(sleep);
88 		err = modbus_write_coil(client_iface, node, addr++, true);
89 		if (err != 0) {
90 			LOG_ERR("FC05 failed with %d", err);
91 			return 0;
92 		}
93 
94 		k_msleep(sleep);
95 		err = modbus_write_coil(client_iface, node, addr++, true);
96 		if (err != 0) {
97 			LOG_ERR("FC05 failed with %d", err);
98 			return 0;
99 		}
100 
101 		k_msleep(sleep);
102 		err = modbus_read_coils(client_iface, node, 0, coil, coil_qty);
103 		if (err != 0) {
104 			LOG_ERR("FC01 failed with %d", err);
105 			return 0;
106 		}
107 
108 		LOG_INF("Coils state 0x%02x", coil[0]);
109 
110 		coil[0] = 0;
111 		err = modbus_write_coils(client_iface, node, 0, coil, coil_qty);
112 		if (err != 0) {
113 			LOG_ERR("FC15 failed with %d", err);
114 			return 0;
115 		}
116 
117 		k_msleep(sleep);
118 	}
119 }
120