Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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 <zephyr/drivers/uart.h>
34#include <zephyr/sys/slist.h>
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
48#define MODBUS_EXC_NONE 0
50#define MODBUS_EXC_ILLEGAL_FC 1
52#define MODBUS_EXC_ILLEGAL_DATA_ADDR 2
54#define MODBUS_EXC_ILLEGAL_DATA_VAL 3
56#define MODBUS_EXC_SERVER_DEVICE_FAILURE 4
58#define MODBUS_EXC_ACK 5
60#define MODBUS_EXC_SERVER_DEVICE_BUSY 6
62#define MODBUS_EXC_MEM_PARITY_ERROR 8
64#define MODBUS_EXC_GW_PATH_UNAVAILABLE 10
66#define MODBUS_EXC_GW_TARGET_FAILED_TO_RESP 11
88
116int modbus_read_coils(const int iface,
117 const uint8_t unit_id,
118 const uint16_t start_addr,
119 uint8_t *const coil_tbl,
120 const uint16_t num_coils);
121
150int modbus_read_dinputs(const int iface,
151 const uint8_t unit_id,
152 const uint16_t start_addr,
153 uint8_t *const di_tbl,
154 const uint16_t num_di);
155
173int modbus_read_holding_regs(const int iface,
174 const uint8_t unit_id,
175 const uint16_t start_addr,
176 uint16_t *const reg_buf,
177 const uint16_t num_regs);
178
196int modbus_read_input_regs(const int iface,
197 const uint8_t unit_id,
198 const uint16_t start_addr,
199 uint16_t *const reg_buf,
200 const uint16_t num_regs);
201
214int modbus_write_coil(const int iface,
215 const uint8_t unit_id,
216 const uint16_t coil_addr,
217 const bool coil_state);
218
232int modbus_write_holding_reg(const int iface,
233 const uint8_t unit_id,
234 const uint16_t start_addr,
235 const uint16_t reg_val);
236
250int modbus_request_diagnostic(const int iface,
251 const uint8_t unit_id,
252 const uint16_t sfunc,
253 const uint16_t data,
254 uint16_t *const data_out);
255
283int modbus_write_coils(const int iface,
284 const uint8_t unit_id,
285 const uint16_t start_addr,
286 uint8_t *const coil_tbl,
287 const uint16_t num_coils);
288
306int modbus_write_holding_regs(const int iface,
307 const uint8_t unit_id,
308 const uint16_t start_addr,
309 uint16_t *const reg_buf,
310 const uint16_t num_regs);
311
329int modbus_read_holding_regs_fp(const int iface,
330 const uint8_t unit_id,
331 const uint16_t start_addr,
332 float *const reg_buf,
333 const uint16_t num_regs);
334
353 const uint8_t unit_id,
354 const uint16_t start_addr,
355 float *const reg_buf,
356 const uint16_t num_regs);
357
361 int (*coil_rd)(uint16_t addr, bool *state);
362
364 int (*coil_wr)(uint16_t addr, bool state);
365
367 int (*discrete_input_rd)(uint16_t addr, bool *state);
368
370 int (*input_reg_rd)(uint16_t addr, uint16_t *reg);
371
373 int (*input_reg_rd_fp)(uint16_t addr, float *reg);
374
376 int (*holding_reg_rd)(uint16_t addr, uint16_t *reg);
377
380
382 int (*holding_reg_rd_fp)(uint16_t addr, float *reg);
383
385 int (*holding_reg_wr_fp)(uint16_t addr, float reg);
386};
387
398int modbus_iface_get_by_name(const char *iface_name);
399
409typedef int (*modbus_raw_cb_t)(const int iface, const struct modbus_adu *adu,
410 void *user_data);
411
433typedef bool (*modbus_custom_cb_t)(const int iface,
434 const struct modbus_adu *const rx_adu,
435 struct modbus_adu *const tx_adu,
436 uint8_t *const excep_code,
437 void *const user_data);
438
443struct modbus_custom_fc {
444 sys_snode_t node;
446 void *user_data;
447 uint8_t fc;
448 uint8_t excep_code;
449};
455#define MODBUS_CUSTOM_FC_DEFINE(name, user_cb, user_fc, userdata) \
456 static struct modbus_custom_fc modbus_cfg_##name = { \
457 .cb = user_cb, \
458 .user_data = userdata, \
459 .fc = user_fc, \
460 .excep_code = MODBUS_EXC_NONE, \
461 }
462
474
495
505
510
518 union {
524 };
525 union {
527 struct modbus_serial_param serial;
529 struct modbus_raw_cb rawcb;
530 };
531};
532
541int modbus_init_server(const int iface, struct modbus_iface_param param);
542
551int modbus_init_client(const int iface, struct modbus_iface_param param);
552
562int modbus_disable(const uint8_t iface);
563
572int modbus_raw_submit_rx(const int iface, const struct modbus_adu *adu);
573
581void modbus_raw_put_header(const struct modbus_adu *adu, uint8_t *header);
582
589void modbus_raw_get_header(struct modbus_adu *adu, const uint8_t *header);
590
599
612int modbus_raw_backend_txn(const int iface, struct modbus_adu *adu);
613
630int modbus_register_user_fc(const int iface, struct modbus_custom_fc *custom_fc);
631
632#ifdef __cplusplus
633}
634#endif
635
640#endif /* ZEPHYR_INCLUDE_MODBUS_H_ */
Public APIs for UART drivers.
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)
bool(* modbus_custom_cb_t)(const int iface, const struct modbus_adu *const rx_adu, struct modbus_adu *const tx_adu, uint8_t *const excep_code, void *const user_data)
Custom function code handler function signature.
Definition modbus.h:433
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:466
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_raw_cb_t)(const int iface, const struct modbus_adu *adu, void *user_data)
ADU raw callback function signature.
Definition modbus.h:409
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_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)
int modbus_register_user_fc(const int iface, struct modbus_custom_fc *custom_fc)
Register a user-defined function code handler.
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
Modbus raw ADU mode.
Definition modbus.h:472
@ MODBUS_MODE_RTU
Modbus over serial line RTU mode.
Definition modbus.h:468
@ MODBUS_MODE_ASCII
Modbus over serial line ASCII mode.
Definition modbus.h:470
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
uart_config_parity
Parity modes.
Definition uart.h:79
uart_config_stop_bits
Number of stop bits.
Definition uart.h:88
state
Definition parser_state.h:29
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Frame struct used internally and for raw ADU support.
Definition modbus.h:72
uint8_t fc
Function Code.
Definition modbus.h:82
uint16_t trans_id
Transaction Identifier.
Definition modbus.h:74
uint16_t crc
RTU CRC.
Definition modbus.h:86
uint8_t data[CONFIG_MODBUS_BUFFER_SIZE - 4]
Transaction Data.
Definition modbus.h:84
uint16_t length
Length of the data only (not the length of unit ID + PDU)
Definition modbus.h:78
uint8_t unit_id
Unit Identifier.
Definition modbus.h:80
uint16_t proto_id
Protocol Identifier.
Definition modbus.h:76
User parameter structure to configure Modbus interface as client or server.
Definition modbus.h:515
uint32_t rx_timeout
Amount of time client will wait for a response from the server.
Definition modbus.h:523
struct modbus_server_param server
Definition modbus.h:519
enum modbus_mode mode
Mode of the interface.
Definition modbus.h:517
Definition modbus.h:506
modbus_raw_cb_t raw_tx_cb
Definition modbus.h:507
void * user_data
Definition modbus.h:508
Modbus serial line parameter.
Definition modbus.h:478
enum uart_config_parity parity
parity UART's parity setting: UART_CFG_PARITY_NONE, UART_CFG_PARITY_EVEN, UART_CFG_PARITY_ODD
Definition modbus.h:486
enum uart_config_stop_bits stop_bits_client
stop_bits_client UART's stop bits setting if in client mode: UART_CFG_STOP_BITS_0_5,...
Definition modbus.h:493
uint32_t baud
Baudrate of the serial line.
Definition modbus.h:480
Modbus server parameter.
Definition modbus.h:499
uint8_t unit_id
Modbus unit ID of the server.
Definition modbus.h:503
struct modbus_user_callbacks * user_cb
Pointer to the User Callback structure.
Definition modbus.h:501
Modbus Server User Callback structure.
Definition modbus.h:359
int(* coil_wr)(uint16_t addr, bool state)
Coil write callback.
Definition modbus.h:364
int(* holding_reg_wr_fp)(uint16_t addr, float reg)
Floating Point Holding Register write callback.
Definition modbus.h:385
int(* coil_rd)(uint16_t addr, bool *state)
Coil read callback.
Definition modbus.h:361
int(* holding_reg_wr)(uint16_t addr, uint16_t reg)
Holding Register write callback.
Definition modbus.h:379
int(* holding_reg_rd_fp)(uint16_t addr, float *reg)
Floating Point Holding Register read callback.
Definition modbus.h:382
int(* input_reg_rd)(uint16_t addr, uint16_t *reg)
Input Register read callback.
Definition modbus.h:370
int(* discrete_input_rd)(uint16_t addr, bool *state)
Discrete Input read callback.
Definition modbus.h:367
int(* holding_reg_rd)(uint16_t addr, uint16_t *reg)
Holding Register read callback.
Definition modbus.h:376
int(* input_reg_rd_fp)(uint16_t addr, float *reg)
Floating Point Input Register read callback.
Definition modbus.h:373
static fdata_t data[2]
Definition test_fifo_contexts.c:15
static const intptr_t user_data[5]
Definition main.c:588