Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
modbus.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 PHYTEC Messtechnik GmbH
3 * Copyright (c) 2021 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8/*
9 * Client API in this file is based on mbm_core.c from uC/Modbus Stack.
10 *
11 * uC/Modbus
12 * The Embedded Modbus Stack
13 *
14 * Copyright 2003-2020 Silicon Laboratories Inc. www.silabs.com
15 *
16 * SPDX-License-Identifier: APACHE-2.0
17 *
18 * This software is subject to an open source license and is distributed by
19 * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
20 * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
21 */
22
30#ifndef ZEPHYR_INCLUDE_MODBUS_H_
31#define ZEPHYR_INCLUDE_MODBUS_H_
32
33#include <drivers/uart.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
40#define MODBUS_MBAP_LENGTH 7
42#define MODBUS_MBAP_AND_FC_LENGTH (MODBUS_MBAP_LENGTH + 1)
43
47struct modbus_adu {
59 uint8_t data[CONFIG_MODBUS_BUFFER_SIZE - 4];
62};
63
91int modbus_read_coils(const int iface,
92 const uint8_t unit_id,
93 const uint16_t start_addr,
94 uint8_t *const coil_tbl,
95 const uint16_t num_coils);
96
125int modbus_read_dinputs(const int iface,
126 const uint8_t unit_id,
127 const uint16_t start_addr,
128 uint8_t *const di_tbl,
129 const uint16_t num_di);
130
148int modbus_read_holding_regs(const int iface,
149 const uint8_t unit_id,
150 const uint16_t start_addr,
151 uint16_t *const reg_buf,
152 const uint16_t num_regs);
153
171int modbus_read_input_regs(const int iface,
172 const uint8_t unit_id,
173 const uint16_t start_addr,
174 uint16_t *const reg_buf,
175 const uint16_t num_regs);
176
189int modbus_write_coil(const int iface,
190 const uint8_t unit_id,
191 const uint16_t coil_addr,
192 const bool coil_state);
193
207int modbus_write_holding_reg(const int iface,
208 const uint8_t unit_id,
209 const uint16_t start_addr,
210 const uint16_t reg_val);
211
225int modbus_request_diagnostic(const int iface,
226 const uint8_t unit_id,
227 const uint16_t sfunc,
228 const uint16_t data,
229 uint16_t *const data_out);
230
258int modbus_write_coils(const int iface,
259 const uint8_t unit_id,
260 const uint16_t start_addr,
261 uint8_t *const coil_tbl,
262 const uint16_t num_coils);
263
281int modbus_write_holding_regs(const int iface,
282 const uint8_t unit_id,
283 const uint16_t start_addr,
284 uint16_t *const reg_buf,
285 const uint16_t num_regs);
286
304int modbus_read_holding_regs_fp(const int iface,
305 const uint8_t unit_id,
306 const uint16_t start_addr,
307 float *const reg_buf,
308 const uint16_t num_regs);
309
328 const uint8_t unit_id,
329 const uint16_t start_addr,
330 float *const reg_buf,
331 const uint16_t num_regs);
332
336 int (*coil_rd)(uint16_t addr, bool *state);
337
339 int (*coil_wr)(uint16_t addr, bool state);
340
342 int (*discrete_input_rd)(uint16_t addr, bool *state);
343
345 int (*input_reg_rd)(uint16_t addr, uint16_t *reg);
346
348 int (*input_reg_rd_fp)(uint16_t addr, float *reg);
349
351 int (*holding_reg_rd)(uint16_t addr, uint16_t *reg);
352
355
357 int (*holding_reg_rd_fp)(uint16_t addr, float *reg);
358
360 int (*holding_reg_wr_fp)(uint16_t addr, float reg);
361};
362
373int modbus_iface_get_by_name(const char *iface_name);
374
383typedef int (*modbus_raw_cb_t)(const int iface, const struct modbus_adu *adu);
384
395};
396
409};
410
419};
420
428 union {
434 };
435 union {
440 };
441};
442
451int modbus_init_server(const int iface, struct modbus_iface_param param);
452
461int modbus_init_client(const int iface, struct modbus_iface_param param);
462
472int modbus_disable(const uint8_t iface);
473
482int modbus_raw_submit_rx(const int iface, const struct modbus_adu *adu);
483
493void modbus_raw_put_header(const struct modbus_adu *adu, uint8_t *header);
494
503void modbus_raw_get_header(struct modbus_adu *adu, const uint8_t *header);
504
513
526int modbus_raw_backend_txn(const int iface, struct modbus_adu *adu);
527
528#ifdef __cplusplus
529}
530#endif
531
536#endif /* ZEPHYR_INCLUDE_MODBUS_H_ */
int modbus_read_coils(const int iface, const uint8_t unit_id, const uint16_t start_addr, uint8_t *const coil_tbl, const uint16_t num_coils)
Coil read (FC01)
int modbus_disable(const uint8_t iface)
Disable Modbus Interface.
void modbus_raw_get_header(struct modbus_adu *adu, const uint8_t *header)
Get MBAP header from a buffer.
modbus_mode
Modbus interface mode.
Definition: modbus.h:388
int modbus_read_input_regs(const int iface, const uint8_t unit_id, const uint16_t start_addr, uint16_t *const reg_buf, const uint16_t num_regs)
Read input registers (FC04)
int modbus_raw_submit_rx(const int iface, const struct modbus_adu *adu)
Submit raw ADU.
int modbus_write_holding_regs_fp(const int iface, const uint8_t unit_id, const uint16_t start_addr, float *const reg_buf, const uint16_t num_regs)
Write floating-point holding registers (FC16)
int modbus_raw_backend_txn(const int iface, struct modbus_adu *adu)
Use interface as backend to send and receive ADU.
int modbus_read_holding_regs(const int iface, const uint8_t unit_id, const uint16_t start_addr, uint16_t *const reg_buf, const uint16_t num_regs)
Read holding registers (FC03)
void modbus_raw_put_header(const struct modbus_adu *adu, uint8_t *header)
Put MBAP header into a buffer.
int modbus_read_dinputs(const int iface, const uint8_t unit_id, const uint16_t start_addr, uint8_t *const di_tbl, const uint16_t num_di)
Read discrete inputs (FC02)
int modbus_init_client(const int iface, struct modbus_iface_param param)
Configure Modbus Interface as raw ADU client.
int(* modbus_raw_cb_t)(const int iface, const struct modbus_adu *adu)
ADU raw callback function signature.
Definition: modbus.h:383
int modbus_read_holding_regs_fp(const int iface, const uint8_t unit_id, const uint16_t start_addr, float *const reg_buf, const uint16_t num_regs)
Read floating-point holding registers (FC03)
int modbus_iface_get_by_name(const char *iface_name)
Get Modbus interface index according to interface name.
int modbus_write_coils(const int iface, const uint8_t unit_id, const uint16_t start_addr, uint8_t *const coil_tbl, const uint16_t num_coils)
Write coils (FC15)
int modbus_request_diagnostic(const int iface, const uint8_t unit_id, const uint16_t sfunc, const uint16_t data, uint16_t *const data_out)
Read diagnostic (FC08)
int modbus_write_coil(const int iface, const uint8_t unit_id, const uint16_t coil_addr, const bool coil_state)
Write single coil (FC05)
void modbus_raw_set_server_failure(struct modbus_adu *adu)
Set Server Device Failure exception.
int modbus_write_holding_regs(const int iface, const uint8_t unit_id, const uint16_t start_addr, uint16_t *const reg_buf, const uint16_t num_regs)
Write holding registers (FC16)
int modbus_init_server(const int iface, struct modbus_iface_param param)
Configure Modbus Interface as raw ADU server.
int modbus_write_holding_reg(const int iface, const uint8_t unit_id, const uint16_t start_addr, const uint16_t reg_val)
Write single holding register (FC06)
@ MODBUS_MODE_RAW
Definition: modbus.h:394
@ MODBUS_MODE_RTU
Definition: modbus.h:390
@ MODBUS_MODE_ASCII
Definition: modbus.h:392
uart_config_parity
Parity modes.
Definition: uart.h:270
state
Definition: http_parser_state.h:30
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Frame struct used internally and for raw ADU support.
Definition: modbus.h:47
uint8_t fc
Definition: modbus.h:57
uint16_t trans_id
Definition: modbus.h:49
uint16_t crc
Definition: modbus.h:61
uint8_t data[CONFIG_MODBUS_BUFFER_SIZE - 4]
Definition: modbus.h:59
uint16_t length
Definition: modbus.h:53
uint8_t unit_id
Definition: modbus.h:55
uint16_t proto_id
Definition: modbus.h:51
User parameter structure to configure Modbus interfase as client or server.
Definition: modbus.h:425
uint32_t rx_timeout
Definition: modbus.h:433
struct modbus_server_param server
Definition: modbus.h:429
struct modbus_serial_param serial
Definition: modbus.h:437
modbus_raw_cb_t raw_tx_cb
Definition: modbus.h:439
enum modbus_mode mode
Definition: modbus.h:427
Modbus serial line parameter.
Definition: modbus.h:400
enum uart_config_parity parity
Definition: modbus.h:408
uint32_t baud
Definition: modbus.h:402
Modbus server parameter.
Definition: modbus.h:414
uint8_t unit_id
Definition: modbus.h:418
struct modbus_user_callbacks * user_cb
Definition: modbus.h:416
Definition: modbus.h:334
int(* coil_wr)(uint16_t addr, bool state)
Definition: modbus.h:339
int(* holding_reg_wr_fp)(uint16_t addr, float reg)
Definition: modbus.h:360
int(* coil_rd)(uint16_t addr, bool *state)
Definition: modbus.h:336
int(* holding_reg_wr)(uint16_t addr, uint16_t reg)
Definition: modbus.h:354
int(* holding_reg_rd_fp)(uint16_t addr, float *reg)
Definition: modbus.h:357
int(* input_reg_rd)(uint16_t addr, uint16_t *reg)
Definition: modbus.h:345
int(* discrete_input_rd)(uint16_t addr, bool *state)
Definition: modbus.h:342
int(* holding_reg_rd)(uint16_t addr, uint16_t *reg)
Definition: modbus.h:351
int(* input_reg_rd_fp)(uint16_t addr, float *reg)
Definition: modbus.h:348
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Public APIs for UART drivers.