grblHAL core  20241107
modbus.h
Go to the documentation of this file.
1 /*
2 
3  modbus.h - a lightweight ModBus implementation
4 
5  Part of grblHAL
6 
7  Copyright (c) 2023 Terje Io
8 
9  Grbl is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  Grbl is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifndef _MODBUS_H_
25 #define _MODBUS_H_
26 
27 #ifndef MODBUS_MAX_ADU_SIZE
28 #define MODBUS_MAX_ADU_SIZE 12
29 #endif
30 #ifndef MODBUS_QUEUE_LENGTH
31 #define MODBUS_QUEUE_LENGTH 8
32 #endif
33 
34 #include <stdint.h>
35 #include <stdbool.h>
36 
37 #define MODBUS_SET_MSB16(v) ((v) >> 8)
38 #define MODBUS_SET_LSB16(v) ((v) & 0xFF)
39 
40 typedef enum {
45 
46 typedef enum {
58 
59 typedef struct {
60  void *context;
61  bool crc_check;
62  uint8_t tx_length;
63  uint8_t rx_length;
66 
67 typedef struct {
68  void (*on_rx_packet)(modbus_message_t *msg);
69  void (*on_rx_exception)(uint8_t code, void *context);
71 
72 typedef union {
73  uint16_t timeout[6];
74  struct {
75  uint16_t b2400;
76  uint16_t b4800;
77  uint16_t b9600;
78  uint16_t b19200;
79  uint16_t b38400;
80  uint16_t b115200;
81  };
83 
84 typedef bool (*modbus_is_up_ptr)(void);
85 typedef void (*modbus_flush_queue_ptr)(void);
86 typedef void (*modbus_set_silence_ptr)(const modbus_silence_timeout_t *timeout);
87 typedef bool (*modbus_send_ptr)(modbus_message_t *msg, const modbus_callbacks_t *callbacks, bool block);
88 
89 typedef struct {
95 } modbus_api_t;
96 
97 bool modbus_isup (void);
98 bool modbus_enabled (void);
99 void modbus_flush_queue (void);
100 void modbus_set_silence (const modbus_silence_timeout_t *timeout);
101 bool modbus_send (modbus_message_t *msg, const modbus_callbacks_t *callbacks, bool block);
102 uint16_t modbus_read_u16 (uint8_t *p);
103 void modbus_write_u16 (uint8_t *p, uint16_t value);
104 bool modbus_register_api (const modbus_api_t *api);
105 
106 #endif
modbus_function_t
Definition: modbus.h:46
@ ModBus_WriteCoil
Definition: modbus.h:51
@ ModBus_ReadHoldingRegisters
Definition: modbus.h:49
@ ModBus_WriteRegisters
Definition: modbus.h:56
@ ModBus_Diagnostics
Definition: modbus.h:54
@ ModBus_ReadCoils
Definition: modbus.h:47
@ ModBus_WriteCoils
Definition: modbus.h:55
@ ModBus_ReadInputRegisters
Definition: modbus.h:50
@ ModBus_WriteRegister
Definition: modbus.h:52
@ ModBus_ReadExceptionStatus
Definition: modbus.h:53
@ ModBus_ReadDiscreteInputs
Definition: modbus.h:48
void(* modbus_flush_queue_ptr)(void)
Definition: modbus.h:85
#define MODBUS_MAX_ADU_SIZE
Definition: modbus.h:28
void(* modbus_set_silence_ptr)(const modbus_silence_timeout_t *timeout)
Definition: modbus.h:86
void modbus_write_u16(uint8_t *p, uint16_t value)
Definition: modbus.c:80
bool(* modbus_send_ptr)(modbus_message_t *msg, const modbus_callbacks_t *callbacks, bool block)
Definition: modbus.h:87
modbus_if_t
Definition: modbus.h:40
@ Modbus_InterfaceASCII
Definition: modbus.h:42
@ Modbus_InterfaceTCP
Definition: modbus.h:43
@ Modbus_InterfaceRTU
Definition: modbus.h:41
bool modbus_isup(void)
Definition: modbus.c:33
bool modbus_register_api(const modbus_api_t *api)
Definition: modbus.c:86
bool modbus_send(modbus_message_t *msg, const modbus_callbacks_t *callbacks, bool block)
Definition: modbus.c:65
void modbus_set_silence(const modbus_silence_timeout_t *timeout)
Definition: modbus.c:59
uint16_t modbus_read_u16(uint8_t *p)
Definition: modbus.c:75
bool modbus_enabled(void)
Definition: modbus.c:45
void modbus_flush_queue(void)
Definition: modbus.c:50
bool(* modbus_is_up_ptr)(void)
Definition: modbus.h:84
Definition: modbus.h:89
modbus_set_silence_ptr set_silence
Definition: modbus.h:93
modbus_flush_queue_ptr flush_queue
Definition: modbus.h:92
modbus_send_ptr send
Definition: modbus.h:94
modbus_if_t interface
Definition: modbus.h:90
modbus_is_up_ptr is_up
Definition: modbus.h:91
Definition: modbus.h:67
Definition: modbus.h:59
uint8_t rx_length
Definition: modbus.h:63
uint8_t tx_length
Definition: modbus.h:62
bool crc_check
Definition: modbus.h:61
void * context
Definition: modbus.h:60
Definition: modbus.h:72
uint16_t b9600
Definition: modbus.h:77
uint16_t b38400
Definition: modbus.h:79
uint16_t b19200
Definition: modbus.h:78
uint16_t b115200
Definition: modbus.h:80
uint16_t b2400
Definition: modbus.h:75
uint16_t b4800
Definition: modbus.h:76