ACCMDManager.cs
8.1 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ACServoDriveTest
{
public class ACCMDManager
{
public static string ServerOn_Addr = "0060";
public static string STB_Addr = "0120";
public static string Stop_Addr = "0124";
public static string SDStop_Addr = "0123";
public static string Clear_Alarm_Addr = "0061";
/// <summary>
/// 目标位置=600B
/// </summary>
public static string TargetPostion = "600B";
/// <summary>
/// 实际位置=600F
/// </summary>
public static string ActualPosition = "600F";
/// <summary>
/// BUSY状态=0140
public static string BUSYStatus = "0140";
/// <summary>
/// HOME-CMP=0141
/// </summary>
public static string HOME_CMP_Status = "0141";
/// <summary>
/// 报警状态=00A1
/// </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;
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;
}
}
}