-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_data_channel.c
281 lines (220 loc) · 8.27 KB
/
service_data_channel.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "service_data_channel.h"
#include "datagram.h"
#include <string.h>
#include "format.h"
// 1.通道配置:站地址、通道起始物理地址,通道长度,控制字,通道号
// 站地址寻址
bool service_data_channel_config(uint16 station_address, uint16 phy_addr,
uint16 len, uint8 control, uint8 ch_No)
{
// 通道号不正确,越出范围
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
bool flag; // 检验成功与否的标志位
nc_slave_fpwr_uint16(station_address, SVR_DATA_CH_ADDR_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, phy_addr, &flag);
if (!flag)
return false;
nc_slave_fpwr_uint16(station_address, SVR_DATA_CH_LEN_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, len, &flag);
if (!flag)
return false;
nc_slave_fpwr_uint8(station_address, SVR_DATA_CH_CTRL_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, control, &flag);
if (!flag)
return false;
return true;
}
// 1.通道配置:站地址、通道起始物理地址,通道长度,控制字,通道号
// 顺序寻址
bool service_data_channel_config_apwr(uint16 ring_position, uint16 phy_addr,
uint16 len, uint8 control, uint8 ch_No)
{
// 通道号不正确,越出范围
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
bool flag;
nc_slave_apwr_uint16(ring_position, SVR_DATA_CH_ADDR_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, phy_addr, &flag);
if (!flag)
return false;
nc_slave_apwr_uint16(ring_position, SVR_DATA_CH_LEN_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, len, &flag);
if (!flag)
return false;
nc_slave_apwr_uint8(ring_position, SVR_DATA_CH_CTRL_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, control, &flag);
if (!flag)
return false;
return true;
}
// 2.通道开关
// 站地址寻址
bool service_data_channel_on(uint16 station_address, uint8 ch_No)
{
// 通道号不正确,越出范围
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
bool flag;
// 写入 0x80 启动过程数据通道,参见寄存器手册 reg 0x0125
nc_slave_fpwr_uint8(station_address, SVR_DATA_CH_SWITCH_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, 0x80, &flag);
if (!flag)
return false;
return true;
}
bool serivce_data_channel_off(uint16 station_address, uint8 ch_No)
{
// 通道号不正确,越出范围
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
bool flag;
// 写入 0x80 启动过程数据通道,参见寄存器手册 reg 0x0125
nc_slave_fpwr_uint8(station_address, SVR_DATA_CH_SWITCH_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, 0x00, &flag);
if (!flag)
return false;
return true;
}
// 2.通道开关
// 顺序寻址
bool service_data_channel_on_apwr(uint16 ring_position, uint8 ch_No)
{
// 通道号不正确,越出范围
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
bool flag;
// 写入 0x80 启动过程数据通道,参见寄存器手册 reg 0x0125
nc_slave_apwr_uint8(ring_position, SVR_DATA_CH_SWITCH_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, 0x80, &flag);
if (!flag)
return false;
return true;
}
bool service_data_channel_off_apwr(uint16 ring_position, uint8 ch_No)
{
// 通道号不正确,越出范围
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
bool flag;
// 写入 0x80 启动过程数据通道,参见寄存器手册 reg 0x0125
nc_slave_apwr_uint8(ring_position, SVR_DATA_CH_SWITCH_REG + (ch_No - MINI_SER_DATA_CHANNEL_NO) * SER_DATA_CHANNEL_CONFIG_SIZE, 0x00, &flag);
if (!flag)
return false;
return true;
}
// 3.通道读写函数
// 站地址寻址
bool service_data_channel_fchrd(uint16 station_address, uint8 *data, uint16 len, uint8 ch_No)
{
nc_frame_t frame;
uint16 irq = 0x00;
int16 result;
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
// 清空读取的缓存区
memset(data, 0, len);
initframe(&frame);
// 站内偏移地址设为0x00,irq设为0x00
result = chAccessdgrmAdd(&frame, FCHRD, station_address, 0x00, len, ch_No, data, 0x00);
// 报文添加报错,由于需要读取的字节超出 单报文/每帧 所允许的数据区的最大长度
if (result < 0)
return false;
FrameTransmit(&frame);
if (FrameReceive(&frame) < 0)
return false;
irq = nctohs(*(uint16 *)(frame.dgrambuff + 7 + len));
if ((irq & 0x03) != 0x01)
return false;
memcpy(data, frame.dgrambuff + 7, len);
return true;
}
bool service_data_channel_fchwr(uint16 station_address, uint8 *data, uint16 len, uint8 ch_No)
{
nc_frame_t frame;
uint16 irq = 0x00;
int16 result;
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
initframe(&frame);
// 站内偏移地址设为0x00,irq设为0x00
result = chAccessdgrmAdd(&frame, FCHWR, station_address, 0x00, len, ch_No, data, 0x00);
// 报文添加报错,由于需要读取的字节超出 单报文/每帧 所允许的数据区的最大长度
if (result < 0)
return false;
FrameTransmit(&frame);
if (FrameReceive(&frame) < 0)
return false;
irq = nctohs(*(uint16 *)(frame.dgrambuff + 7 + len));
if ((irq & 0x03) != 0x01)
return false;
return true;
}
// 3.通道读写函数
// 顺序寻址
bool service_data_channel_achrd(uint16 ring_position, uint8 *data, uint16 len, uint8 ch_No)
{
nc_frame_t frame;
uint16 irq = 0x00;
int16 result;
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
// 清空读取的缓存区
memset(data, 0, len);
initframe(&frame);
// 站内偏移地址设为0x00,irq设为0x00
result = chAccessdgrmAdd(&frame, ACHRD, ring_position, 0x00, len, ch_No, data, 0x00);
// 报文添加报错,由于需要读取的字节超出 单报文/每帧 所允许的数据区的最大长度
if (result < 0)
return false;
FrameTransmit(&frame);
if (FrameReceive(&frame) < 0)
return false;
irq = nctohs(*(uint16 *)(frame.dgrambuff + 7 + len));
if ((irq & 0x03) != 0x01)
return false;
memcpy(data, frame.dgrambuff + 7, len);
return true;
}
bool service_data_channel_achwr(uint16 ring_position, uint8 *data, uint16 len, uint8 ch_No)
{
nc_frame_t frame;
uint16 irq = 0x00;
int16 result;
if (ch_No > MAX_SER_DATA_CHANNEL_NO || ch_No < MINI_SER_DATA_CHANNEL_NO)
{
printf("out of the range of service data channel.\n");
return false;
}
initframe(&frame);
// 站内偏移地址设为0x00,irq设为0x00
result = chAccessdgrmAdd(&frame, ACHWR, ring_position, 0x00, len, ch_No, data, 0x00);
// 报文添加报错,由于需要读取的字节超出 单报文/每帧 所允许的数据区的最大长度
if (result < 0)
return false;
FrameTransmit(&frame);
if (FrameReceive(&frame) < 0)
return false;
irq = nctohs(*(uint16 *)(frame.dgrambuff + 7 + len));
if ((irq & 0x03) != 0x01)
return false;
return true;
}