ACCMDManager.cs
11.0 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineStore.DeviceLibrary
{
public class ACCMDManager
{
/// <summary>
/// 0132h 原点接近传感器输入 (HOME) 状态 ReadOnly 0: 输入 OFF 1: 输入 ON
/// </summary>
public static string Home_Single = "0132";
// 0022h 负方向驱动禁止输入(NOT) ReadOnly 0: 输入 OFF、 1: 输入 ON
// 0023h 正方向驱动禁止输入(POT) ReadOnly 0: 输入 OFF、 1: 输入 ON
/// <summary>
///速度 4601
/// </summary>
public static string Speed_Addr = "4601";
/// <summary>
/// 0060h 伺服 On 输入 (SRV-ON) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string ServerOn_Addr = "0060";
/// <summary>
/// 0120h 选通输入 (STB) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string STB_Addr = "0120";
/// <summary>
/// 0124h 减速停止输入 (S-STOP) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string Stop_Addr = "0124";
/// <summary>
/// 0123h 紧急停止输入 (H-STOP) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string SDStop_Addr = "0123";
/// <summary>
/// 0061h 报警清除输入 (A-CLR) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string Clear_Alarm_Addr = "0061";
/// <summary>
/// 1020h Save all parameters - 0 - 4294967295 2 R/W 参数写入 EEPROM 写入“6173h”后,实行 EEPROM 写入
/// </summary>
public static string EEPROM_Param_Addr = "1020";
/// <summary>
/// 目标位置=600B
/// </summary>
public static string TargetPostion = "600B";
/// <summary>
/// 实际位置=600F
/// </summary>
public static string ActualPosition = "600F";
/// <summary>
/// BUSY状态=0140,0140h 动作执行状态 (BUSY) ReadOnly 0:未执行 1:执行中
public static string BUSYStatus = "0140";
/// <summary>
/// HOME-CMP=0141,0141h 回原点完成状态 (HOME-CMP) ReadOnly 0:未完成 1:完成
/// </summary>
public static string HOME_CMP_Status = "0141";
/// <summary>
/// 私服Ready状态,00A0h 伺服 Ready 状态 (S-RDY) ReadOnly 0:Ready OFF 1:Ready ON
/// </summary>
public static string ServerOn_Status = "00A0";
/// <summary>
/// 报警状态=00A1,00A1h 报警状态 (ALM) ReadOnly 0:报警未发生 1:报警发生
/// </summary>
public static string Alarm_Status = "00A1";
/// <summary>
/// 指定BlockNo=4414
/// </summary>
public static string BlockNo = "4414";
/// <summary>
/// 读线圈01
/// </summary>
public static byte CMD_ReadCoil= 0x01;
/// <summary>
/// 写线圈 05
/// </summary>
public static byte CMD_WriteCoil = 0x05;
/// <summary>
/// 读寄存器03
/// </summary>
public static byte CMD_ReadRegisters = 0x03;
/// <summary>
/// 写寄存器06
/// </summary>
public static byte CMD_WriteRegisters = 0x06;
/// <summary>
/// 写多个线圈 0f
/// </summary>
public static byte CMD_WriteMCoil = 0x0F;
/// <summary>
/// 写多个寄存器10
/// </summary>
public static byte CMD_WriteMRegisters = 0x10;
/// <summary>
/// block0=原点返回,正方向
/// </summary>
public static string Block_HomeMove0 = "0";
/// <summary>
/// block1=原点返回,反方向
/// </summary>
public static string Block_HomeMove1 = "1";
/// <summary>
/// block2=绝对位置运动
/// </summary>
public static string Block_AbsMove = "2";
/// <summary>
/// block3=相对位置运动
/// </summary>
public static string Block_RelMove = "3";
/// <summary>
/// block4=正方向匀速运动
/// </summary>
public static string Block_VolMove0 = "4";
/// <summary>
/// block5=反方向匀速运动
/// </summary>
public static string Block_VolMove1 = "5";
/// <summary>
/// block6=减速停止
/// </summary>
public static string Block_DelSpeedStop = "6";
/// <summary>
/// block7=紧急停止
/// </summary>
public static string Block_SuddenStop = "7";
public static byte[] buildCheckData(byte[] sendData, int length)
{
ushort pChecksum = 0;
SerialBean.CalculateCRC(sendData, length, out pChecksum);
string checkStr = Convert.ToString(pChecksum, 16);
byte[] checkByte = SerialBean.StringToByte(checkStr);
if (checkByte.Length == 1)
{
sendData[length] = checkByte[0];
sendData[length + 1] = 0x00;
}
else
{
sendData[length + 1] = checkByte[0];
sendData[length] = checkByte[1];
}
return sendData;
}
public static byte[] GetWriteData(int slvAddr, byte cmd, string addr, string dataValue, int length)
{
// ( 2) 读取寄存器( 03h)
if (cmd.Equals(CMD_ReadRegisters))
{
return Get03WriteData(slvAddr, addr,length);
}
else if (cmd.Equals(CMD_WriteRegisters))
{
// ( 4) 写入寄存器( 06h)
return Get06WriteData(slvAddr, addr, dataValue);
}
else if (cmd.Equals(CMD_WriteCoil))
{
return Get05WriteData(slvAddr, addr, dataValue);
}
else if (cmd.Equals(CMD_ReadCoil))
{
return Get01WriteData(slvAddr, addr, length);
}
return null;
}
private static byte[] Get05WriteData(int slvAddr, string addr, string dataValue)
{
byte[] sendData = new byte[8];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_WriteCoil;
byte[] addrByte = SerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = SerialBean.StringToByte(dataValue);
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[5] = dataByte[1];
sendData[4] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length - 2);
return sendData;
}
private static byte[] Get01WriteData(int slvAddr, string addr, int length)
{
byte[] sendData = new byte[8];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_ReadCoil;
byte[] addrByte = SerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = SerialBean.StringToByte(length.ToString());
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[4] = dataByte[1];
sendData[5] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length - 2);
return sendData;
}
private static byte[] Get03WriteData(int slvAddr, string addr, int length)
{
// ( 2) 读取寄存器( 03h)
//发送
//从站地址
//03h
//寄存器起始地址 高位
//低位
//寄存器数(N) 高位
//低位
//CRC 低位
//高位
byte[] sendData = new byte[8 ];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_ReadRegisters;
byte[] addrByte = SerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = SerialBean.StringToByte(length.ToString());
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[4] = dataByte[1];
sendData[5] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length-2);
return sendData;
}
private static byte[] Get06WriteData(int slvAddr, string addr, string dataValue)
{
// ( 4) 写入寄存器( 06h)
//从站地址
//06h
//地址 高位
//低位
//変更数据 高位
//低位
//CRC 低位
//高位
byte[] sendData = new byte[8];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_WriteRegisters;
byte[] addrByte = SerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = SerialBean.StringToByte(dataValue);
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[4] = dataByte[1];
sendData[5] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length - 2);
return sendData;
}
}
}