-
Notifications
You must be signed in to change notification settings - Fork 9
/
mb-packet.c
195 lines (174 loc) · 5.08 KB
/
mb-packet.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
/*
MODBUS Packet Maker
By Liyanboy74
https://github.com/liyanboy74/modbus
*/
#include "mb-packet.h"
#if (MB_MODE==MB_MODE_MASTER) || defined MB_DEBUG
mb_packet_s mb_packet_request_read_coil(uint8_t DeviceAddress,uint16_t StartAd,uint16_t Quantity)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.func= MB_FUNC_Read_Coils;
Packet.type=MB_PACKET_TYPE_Master_Request_Fix;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
return Packet;
}
mb_packet_s mb_packet_request_read_discrete_inputs(uint8_t DeviceAddress,uint16_t StartAd,uint16_t Quantity)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.type=MB_PACKET_TYPE_Master_Request_Fix;
Packet.func= MB_FUNC_Read_Discrete_Inputs;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
return Packet;
}
mb_packet_s mb_packet_request_read_holding_registers(uint8_t DeviceAddress,uint16_t StartAd,uint16_t Quantity)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.type=MB_PACKET_TYPE_Master_Request_Fix;
Packet.func= MB_FUNC_Read_Holding_Registers;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
return Packet;
}
mb_packet_s mb_packet_request_read_input_registers(uint8_t DeviceAddress,uint16_t StartAd,uint16_t Quantity)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.type=MB_PACKET_TYPE_Master_Request_Fix;
Packet.func= MB_FUNC_Read_Input_Registers;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
return Packet;
}
mb_packet_s mb_packet_request_write_single_coil(uint8_t DeviceAddress,uint16_t CAddress,uint16_t Data)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.type=MB_PACKET_TYPE_Master_Request_Fix;
Packet.func= MB_FUNC_Write_Single_Coil;
Packet.u16_1=CAddress;
Packet.u16_2=Data;
return Packet;
}
mb_packet_s mb_packet_request_write_single_register(uint8_t DeviceAddress,uint16_t RAddress,uint16_t Data)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.type=MB_PACKET_TYPE_Master_Request_Fix;
Packet.func= MB_FUNC_Write_Single_Register;
Packet.u16_1=RAddress;
Packet.u16_2=Data;
return Packet;
}
mb_packet_s mb_packet_request_write_multiple_coils(uint8_t DeviceAddress,uint16_t StartAd,uint16_t Quantity,uint8_t len,uint8_t *Data)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.type=MB_PACKET_TYPE_Master_Request_Var;
Packet.func= MB_FUNC_Write_Multiple_Coils;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
Packet.len= len;
Packet.Data=Data;
return Packet;
}
mb_packet_s mb_packet_request_write_multiple_registers(uint8_t DeviceAddress,uint16_t StartAd,uint16_t Quantity,uint8_t len,uint8_t *Data)
{
mb_packet_s Packet;
Packet.device_address=DeviceAddress;
Packet.type=MB_PACKET_TYPE_Master_Request_Var;
Packet.func= MB_FUNC_Write_Multiple_Registers;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
Packet.len=len;
Packet.Data=Data;
return Packet;
}
#endif
#if (MB_MODE==MB_MODE_SLAVE) || defined MB_DEBUG
mb_packet_s mb_packet_response_read_coil(uint8_t len,uint8_t *Data)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Var;
Packet.func= MB_FUNC_Read_Coils;
Packet.len= len;
Packet.Data=Data;
return Packet;
}
mb_packet_s mb_packet_response_read_discrete_inputs(uint8_t len,uint8_t *Data)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Var;
Packet.func= MB_FUNC_Read_Discrete_Inputs;
Packet.len= len;
Packet.Data=Data;
return Packet;
}
mb_packet_s mb_packet_response_read_holding_registers(uint8_t len,uint8_t *Data)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Var;
Packet.func= MB_FUNC_Read_Holding_Registers;
Packet.len = len;
Packet.Data=Data;
return Packet;
}
mb_packet_s mb_packet_response_read_input_registers(uint8_t len,uint8_t *Data)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Var;
Packet.func= MB_FUNC_Read_Input_Registers;
Packet.len= len;
Packet.Data=Data;
return Packet;
}
mb_packet_s mb_packet_response_write_single_coil(uint16_t CAddress,uint16_t Data)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Fix;
Packet.func= MB_FUNC_Write_Single_Coil;
Packet.u16_1=CAddress;
Packet.u16_2=Data;
return Packet;
}
mb_packet_s mb_packet_response_write_single_register(uint16_t RAddress,uint16_t Data)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Fix;
Packet.func= MB_FUNC_Write_Single_Register;
Packet.u16_1=RAddress;
Packet.u16_2=Data;
return Packet;
}
mb_packet_s mb_packet_response_write_multiple_coils(uint16_t StartAd,uint16_t Quantity)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Fix;
Packet.func= MB_FUNC_Write_Multiple_Coils;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
return Packet;
}
mb_packet_s mb_packet_response_write_multiple_registers(uint16_t StartAd,uint16_t Quantity)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_Slave_Responce_Fix;
Packet.func= MB_FUNC_Write_Multiple_Registers;
Packet.u16_1=StartAd;
Packet.u16_2=Quantity;
return Packet;
}
mb_packet_s mb_packet_error(mb_functions_e Func,mb_error_e Exeption_Code)
{
mb_packet_s Packet;
Packet.type=MB_PACKET_TYPE_ERROR;
Packet.func= 0x80|Func;
Packet.err= Exeption_Code;
return Packet;
}
#endif