-
Notifications
You must be signed in to change notification settings - Fork 9
/
mb.c
199 lines (162 loc) · 5.68 KB
/
mb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
MODBUS
By Liyanboy74
https://github.com/liyanboy74/modbus
*/
#include <stdio.h>
#include "mb.h"
#include "mb-check.h"
#include "mb-table.h"
#include "mb-process.h"
#include "mb-link.h"
#include "mb-packet.h"
mb_config_s MB_Config={MB_DEFAULT_SLAVE_ADDRESS};
#if(MB_MODE==MB_MODE_SLAVE)
void mb_slave_address_set(uint8_t Address)
{
MB_Config.address=Address;
}
uint8_t mb_slave_address_get(void)
{
return MB_Config.address;
}
#elif(MB_MODE==MB_MODE_MASTER)
void mb_set_master_process_handler(void (*f)(mb_packet_s))
{
MB_Config.master_process_handler=f;
}
#endif
void mb_set_tx_handler(void (*f)(uint8_t *,uint8_t))
{
MB_Config.tx_handler=f;
}
#if(MB_MODE==MB_MODE_SLAVE)
void mb_error_handler(mb_packet_s* Packet,mb_error_e err)
{
#ifdef MB_SLAVE_LISTEN_BROADCAST
if(Packet->device_address != MB_BROADCAST_ADDRESS)
#endif
mb_tx_packet_handler(mb_packet_error(Packet->func,err));
}
#endif
void mb_rx_packet_handler(mb_packet_s Packet)
{
#if(MB_MODE==MB_MODE_SLAVE)
mb_error_e err;
err=mb_check_func(Packet.func);
if(err){mb_error_handler(&Packet,err);return;}
#if MB_ENABLE_FUNC_Read_Coils
if(Packet.func==MB_FUNC_Read_Coils)
{
err=mb_check_quantity(Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_table_coils_address(Packet.u16_1,Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_read_coils(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
}
else
#endif
#if MB_ENABLE_FUNC_Read_Discrete_Inputs
if(Packet.func==MB_FUNC_Read_Discrete_Inputs)
{
err=mb_check_quantity(Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_table_discretes_input_address(Packet.u16_1,Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_read_discrere_inputs(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
}
else
#endif
#if MB_ENABLE_FUNC_Read_Holding_Registers
if(Packet.func==MB_FUNC_Read_Holding_Registers)
{
err=mb_check_quantity(Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_table_holding_registers_address(Packet.u16_1,Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_read_holding_registers(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
}
else
#endif
#if MB_ENABLE_FUNC_Read_Input_Registers
if(Packet.func==MB_FUNC_Read_Input_Registers)
{
err=mb_check_quantity(Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_table_input_registers_address(Packet.u16_1,Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_read_input_registers(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
}
else
#endif
#if MB_ENABLE_FUNC_Write_Single_Coil
if(Packet.func==MB_FUNC_Write_Single_Coil)
{
err=mb_check_on_off(Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_table_coils_address(Packet.u16_1,0);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_write_single_coil(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
}
else
#endif
#if MB_ENABLE_FUNC_Write_Single_Register
if(Packet.func==MB_FUNC_Write_Single_Register)
{
err=mb_check_table_holding_registers_address(Packet.u16_1,0);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_write_single_register(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
}
else
#endif
#if MB_ENABLE_FUNC_Write_Multiple_Coils
if(Packet.func==MB_FUNC_Write_Multiple_Coils)
{
err=mb_check_quantity(Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_quantity_bit_n_byte(Packet.u16_2,Packet.len);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_table_coils_address(Packet.u16_1,Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_write_multiple_coils(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
}
else
#endif
#if MB_ENABLE_FUNC_Write_Multiple_Registers
if(Packet.func==MB_FUNC_Write_Multiple_Registers)
{
err=mb_check_quantity(Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_quantity_reg_n_byte(Packet.u16_2,Packet.len);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_check_table_holding_registers_address(Packet.u16_1,Packet.u16_2);
if(err){mb_error_handler(&Packet,err);return;}
err=mb_slave_process_write_multiple_register(&Packet);
if(err){mb_error_handler(&Packet,err);return;}
} else
#endif
return;
#elif(MB_MODE==MB_MODE_MASTER)
if(MB_Config.master_process_handler!=NULL)
MB_Config.master_process_handler(Packet);
#endif
}
void mb_tx_packet_handler(mb_packet_s Packet)
{
mb_link_prepare_tx_data(Packet);
}
void mb_rx_new_data(uint8_t Byte)
{
mb_link_check_new_data(Byte);
}
void mb_rx_timeout_handler(void)
{
mb_link_reset_rx_buffer();
}