提交 | 用户 | 时间
|
e46d3b
|
1 |
using System; |
懒 |
2 |
using System.IO; |
|
3 |
using YX.BLL; |
|
4 |
using YX.Entity; |
|
5 |
using System.Net; |
|
6 |
using System.Text; |
|
7 |
using YX.WebService; |
|
8 |
using Newtonsoft.Json; |
|
9 |
using System.Net.Sockets; |
|
10 |
using Newtonsoft.Json.Linq; |
|
11 |
using System.Security.Cryptography; |
|
12 |
using System.Drawing; |
|
13 |
using System.Windows.Forms; |
|
14 |
using System.Threading; |
|
15 |
using System.Configuration; |
7820f5
|
16 |
using EasyModbus; // 引入EasyModbusTCP库 ***** |
e46d3b
|
17 |
using System.IO.Ports; |
懒 |
18 |
using System.Net.NetworkInformation; |
|
19 |
|
4dff04
|
20 |
namespace YX |
e46d3b
|
21 |
{ |
懒 |
22 |
public partial class Frmwt : WindowParent |
|
23 |
{ |
|
24 |
Thread receiveDataThread; |
|
25 |
//声明一个委托类型,该委托类型无输入参数和输出参数 |
|
26 |
public delegate void ProcessDelegate(); |
|
27 |
|
|
28 |
private string ipAddress = ConfigurationManager.AppSettings["IPAddress"]; // 从 App.config 获取 IP 地址配置 |
|
29 |
private int port1 = 502; |
|
30 |
ModbusClient tcpClient; |
|
31 |
private bool isTryingToCon = false; |
|
32 |
private string encryptionKey = "a7x3qsz12edc45tgfrbi67yhnmju89i9"; |
|
33 |
|
|
34 |
public string PSN = ""; |
|
35 |
public string PSN2 = ""; |
|
36 |
public string PSN3 = ""; |
742223
|
37 |
public bool t = true; |
e46d3b
|
38 |
public Frmwt(string ParentId) |
懒 |
39 |
{ |
|
40 |
InitializeComponent(); |
|
41 |
|
|
42 |
receiveDataThread = new Thread(new ThreadStart(ReceiveSerialData)); |
|
43 |
receiveDataThread.IsBackground = true; |
|
44 |
try |
|
45 |
{ |
|
46 |
receiveDataThread.Start(); // 启动线程 |
|
47 |
} |
|
48 |
catch (Exception ex) |
|
49 |
{ |
|
50 |
Console.WriteLine($"发生错误:{ex}"); |
|
51 |
} |
fd5f7c
|
52 |
dataGridView1.DataSource = System_Bll.GetSystemLog(FrmLogin.LocalIP, "称重"); |
e46d3b
|
53 |
macBox.Text = GetMacByNetworkInterface(); |
懒 |
54 |
|
|
55 |
} |
|
56 |
|
|
57 |
private void ReceiveSerialData() |
|
58 |
{ |
|
59 |
while (true) |
|
60 |
{ |
|
61 |
ProcessDelegate showProcess = new ProcessDelegate(LabelShow); |
|
62 |
//string str = "1\n2\n3\n"; |
|
63 |
//str = str.Replace("\n", string.Empty); // 将 \n 替换为空字符串 |
|
64 |
string str = ModbusTCPRead(5, 1).Trim("\n".ToCharArray()); |
|
65 |
if (ModbusTCPRead(5, 1).Trim("\n".ToCharArray()) == "1") |
|
66 |
{ |
|
67 |
|
|
68 |
try |
|
69 |
{ |
|
70 |
//PSN = ModbusTCPRead(5, 20).Replace("\n", string.Empty); |
|
71 |
PSN = ModbusTCPReadString(25, 30).Replace("\0", string.Empty); |
|
72 |
PSN2 = ModbusTCPReadString(45, 20).Replace("\0", string.Empty); |
|
73 |
PSN3 = ModbusTCPReadString(65, 20).Replace("\0", string.Empty); |
|
74 |
label1.Invoke(showProcess); |
|
75 |
ModbusTCPWrite(5, 10); |
|
76 |
} |
|
77 |
catch (Exception e) |
|
78 |
{ |
|
79 |
ModbusTCPWrite(5, 11); |
|
80 |
} |
|
81 |
} |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
private string ModbusTCPReadString(int startingAddress, int quantity) |
|
86 |
{ |
|
87 |
ConnectServer(ipAddress, port1); |
|
88 |
int[] data = tcpClient.ReadHoldingRegisters(startingAddress, quantity); // 读取寄存器数据 |
|
89 |
string strData = ""; // 存储字符串数据 |
|
90 |
//string str = ""; |
|
91 |
//string str1 = ""; |
|
92 |
//string str2 = ""; |
|
93 |
|
|
94 |
for (int i = 0; i < quantity; i++) |
|
95 |
{ |
|
96 |
//str= data[i].ToString("X"); |
|
97 |
//if (str.Length == 4) |
|
98 |
//{ |
|
99 |
// str1 = str.Substring(2, 2); |
|
100 |
// str2 = str.Substring(0, 2); |
|
101 |
// strData += str1 + str2; |
|
102 |
//} |
|
103 |
//if (str.Length == 2) |
|
104 |
//{ |
|
105 |
// strData += str; |
|
106 |
//} |
|
107 |
strData += data[i].ToString("X"); |
|
108 |
} |
|
109 |
string str66 = hexStrToStr(strData); |
|
110 |
return hexStrToStr(strData); ; |
|
111 |
} |
|
112 |
|
|
113 |
private static string hexStrToStr(string str) |
|
114 |
{ |
|
115 |
//去除字符串中的空格 |
|
116 |
string[] strT = str.Split(' '); |
|
117 |
string strA = ""; |
|
118 |
foreach (string strB in strT) |
|
119 |
{ |
|
120 |
strA += strB; |
|
121 |
} |
|
122 |
|
|
123 |
char[] chars = strA.ToCharArray(); |
|
124 |
string returnstr = ""; |
|
125 |
string[] str1 = new string[chars.Length / 2]; |
|
126 |
|
|
127 |
for (int i = 0; i < chars.Length / 2; i++) |
|
128 |
{ |
|
129 |
string str111 = chars[2 * i].ToString() + chars[2 * i + 1].ToString(); |
|
130 |
uint num = uint.Parse(str111, System.Globalization.NumberStyles.AllowHexSpecifier); |
|
131 |
char charr = (char)num; |
|
132 |
returnstr = returnstr + charr; |
|
133 |
} |
|
134 |
return returnstr; |
|
135 |
} |
|
136 |
|
|
137 |
public void ConnectServer(string ip, int port) |
|
138 |
{ |
|
139 |
//防止多个事例去重复连接 |
|
140 |
if (isTryingToCon == true) |
|
141 |
{ |
|
142 |
return; |
|
143 |
} |
|
144 |
try |
|
145 |
{ |
|
146 |
if (tcpClient != null) |
|
147 |
{ |
|
148 |
tcpClient.Disconnect(); |
|
149 |
} |
|
150 |
tcpClient = new ModbusClient(ip, port); |
|
151 |
|
|
152 |
tcpClient.UnitIdentifier = 2; //地址 |
|
153 |
tcpClient.Baudrate = 9600; //波特率 |
|
154 |
tcpClient.Parity = Parity.None; //校验位 |
|
155 |
tcpClient.StopBits = StopBits.One; //停止位 |
|
156 |
tcpClient.ConnectionTimeout = 500; |
|
157 |
tcpClient.Connect(); |
|
158 |
|
|
159 |
ProcessDelegate LimeColor = new ProcessDelegate(ButtonLimeColor); |
|
160 |
radioButton1.Invoke(LimeColor); |
|
161 |
|
|
162 |
isTryingToCon = true; |
|
163 |
} |
|
164 |
catch (Exception e) |
|
165 |
{ |
|
166 |
ProcessDelegate RedColor = new ProcessDelegate(ButtonRedColor); |
|
167 |
if (this.IsHandleCreated) |
|
168 |
{ |
|
169 |
radioButton1.Invoke(RedColor); |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
|
|
174 |
private void ButtonRedColor() |
|
175 |
{ |
|
176 |
radioButton1.BackColor = Color.Red; |
|
177 |
} |
|
178 |
|
|
179 |
private void ButtonLimeColor() |
|
180 |
{ |
|
181 |
radioButton1.BackColor = Color.Lime; |
|
182 |
} |
|
183 |
|
|
184 |
/// <param name="startingAddress">寄存器初始地址号</param> |
|
185 |
/// <param name="quantity">读取位数</param> |
|
186 |
private string ModbusTCPRead(int startingAddress, int quantity) |
|
187 |
{ |
|
188 |
ConnectServer(ipAddress, port1); |
|
189 |
string str = ""; |
|
190 |
try |
|
191 |
{ |
|
192 |
int[] result = tcpClient.ReadHoldingRegisters(startingAddress, quantity); //寄存器初始地址号 |
|
193 |
StringBuilder builder = new StringBuilder(); |
|
194 |
for (int i = 0; i < result.Length; i++) |
|
195 |
{ |
|
196 |
builder.Append(Convert.ToString(result[i]) + "\n"); |
|
197 |
} |
|
198 |
str = builder.ToString(); |
|
199 |
} |
|
200 |
catch (Exception ex) |
|
201 |
{ |
|
202 |
isTryingToCon = false; |
|
203 |
tcpClient.Disconnect(); |
|
204 |
} |
|
205 |
return str; |
|
206 |
} |
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
/// <param name="address">位置</param> |
|
211 |
/// <param name="value">写入值</param> |
|
212 |
private void ModbusTCPWrite(int address, int value) |
|
213 |
{ |
|
214 |
ConnectServer(ipAddress, port1); |
|
215 |
try |
|
216 |
{ |
|
217 |
tcpClient.WriteMultipleRegisters(address, new int[] { value }); |
|
218 |
} |
|
219 |
catch (Exception ex) |
|
220 |
{ |
|
221 |
isTryingToCon = false; |
|
222 |
tcpClient.Disconnect(); |
|
223 |
} |
|
224 |
} |
|
225 |
|
|
226 |
private void LabelShow() |
|
227 |
{ |
742223
|
228 |
string[] b_str = PSN.Split(','); |
YY |
229 |
PSNBox.Text = b_str[1]; |
e46d3b
|
230 |
|
懒 |
231 |
JObject jo = WI6027(); |
|
232 |
if (jo == null) |
|
233 |
{ |
|
234 |
System_Bll.WriteLogToDB(new Entity.Base_Log |
|
235 |
{ |
|
236 |
CreateUserID = FrmLogin.LoginUserID, |
|
237 |
CreateUserName = FrmLogin.loginUserName, |
|
238 |
LocalIP = FrmLogin.LocalIP, |
|
239 |
LogMessage = "WI6027调用失败!", |
|
240 |
Type = "称重", |
|
241 |
ClassName = typeof(FrmLogin).ToString() |
|
242 |
}); |
|
243 |
return; |
|
244 |
} |
|
245 |
var contents = jo["content"]; |
|
246 |
foreach (var content in contents) |
|
247 |
{ |
|
248 |
LineNameBox.Text = (string)content["Line"]; |
|
249 |
StationBox.Text = (string)content["Station"]; |
|
250 |
StationIDBox.Text = (string)content["StationID"]; |
|
251 |
} |
|
252 |
if (WI601K(PSNBox.Text)) |
|
253 |
{ |
|
254 |
JObject jo1032 = WI1032(PSNBox.Text, SNMBox.Text, KPNMBox.Text, KPValueBox.Text); |
|
255 |
if (jo1032 != null) |
|
256 |
{ |
742223
|
257 |
if (WI6041(PSNBox.Text, SNMBox.Text, KPNMBox.Text, b_str[0])) |
e46d3b
|
258 |
{ |
懒 |
259 |
System_Bll.WriteLogToDB(new Entity.Base_Log |
|
260 |
{ |
|
261 |
CreateUserID = FrmLogin.LoginUserID, |
|
262 |
CreateUserName = FrmLogin.loginUserName, |
|
263 |
LocalIP = FrmLogin.LocalIP, |
|
264 |
LogMessage = "重量:" + PSNBox.Text + ",称重条码:" + SNMBox.Text + ",工单号:" + KPNMBox.Text + ",MES条码:" + KPNMBox.Text, |
|
265 |
Type = "称重", |
|
266 |
ClassName = typeof(FrmLogin).ToString() |
|
267 |
}); |
|
268 |
} |
|
269 |
} |
|
270 |
} |
fd5f7c
|
271 |
dataGridView1.DataSource = System_Bll.GetSystemLog(FrmLogin.LocalIP, "称重"); |
e46d3b
|
272 |
Refresh(); |
懒 |
273 |
} |
|
274 |
|
|
275 |
/// <param name="encryptStr">明文</param> |
|
276 |
/// <param name="key">密钥</param> |
|
277 |
public static string AES256_Encrypt(string encryptStr, string key) |
|
278 |
{ |
|
279 |
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); |
|
280 |
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr); |
|
281 |
RijndaelManaged rDel = new RijndaelManaged(); |
|
282 |
rDel.Key = keyArray; |
|
283 |
rDel.Mode = CipherMode.ECB; |
|
284 |
rDel.Padding = PaddingMode.PKCS7; |
|
285 |
ICryptoTransform cTransform = rDel.CreateEncryptor(); |
|
286 |
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); |
|
287 |
return Convert.ToBase64String(resultArray, 0, resultArray.Length); |
|
288 |
} |
|
289 |
|
|
290 |
[Obsolete] |
|
291 |
private void FrmExpress_FormClosed(object sender, FormClosedEventArgs e) |
|
292 |
{ |
|
293 |
try |
|
294 |
{ |
|
295 |
receiveDataThread.Suspend(); |
|
296 |
receiveDataThread.Abort(); |
|
297 |
} |
|
298 |
catch (Exception ex) |
|
299 |
{ |
|
300 |
} |
|
301 |
} |
|
302 |
public JObject MES_Interface(string textToEncrypt, string encryptionKey) |
|
303 |
{ |
|
304 |
string par = AES256_Encrypt(textToEncrypt, encryptionKey); |
|
305 |
WMISoapClient wMISoapClient = new WMISoapClient(); |
|
306 |
string text5 = wMISoapClient.WMI01(par); |
|
307 |
return (JObject)JsonConvert.DeserializeObject(text5); |
|
308 |
} |
|
309 |
|
|
310 |
public JObject WI6027() |
|
311 |
{ |
742223
|
312 |
string a = "a0:36:9F:6c:0e:95"; |
e46d3b
|
313 |
string WI6027 = "{"; |
懒 |
314 |
WI6027 += "\"user\":\"sys\","; |
|
315 |
WI6027 += "\"password\":\"sysuser\","; |
|
316 |
WI6027 += "\"method\":\"MES_BA_WI6027\","; |
|
317 |
WI6027 += "\"servername\":\"London\","; |
|
318 |
WI6027 += "\"methodparameter\":{"; |
|
319 |
WI6027 += "\"mac\":\"" + a + "\","; |
742223
|
320 |
WI6027 += "\"hostName\":\"DESKTOP-UV8I1I0\""; |
e46d3b
|
321 |
WI6027 += "}}"; |
懒 |
322 |
|
742223
|
323 |
return MES_Interface(WI6027, encryptionKey); |
e46d3b
|
324 |
} |
懒 |
325 |
|
|
326 |
public bool WI601K(string BarCode) |
|
327 |
{ |
|
328 |
//string WI601K = "{\"user\":\"sys\", \"password\":\"sysuser\",\"method\":\"MES_BA_WI601K\",\"servername\":\"TEST_WTMES_Customer\", \"methodparameter\":{\"BarCode\":\"123456789\", \"HostName\":\"liuxingliang-PC\", \"Mac\":\"6A:15:63:C3:CC:EA\" }}\n"; |
742223
|
329 |
string a = "a0:36:9F:6c:0e:95"; |
e46d3b
|
330 |
string WI601K = "{"; |
懒 |
331 |
WI601K += "\"user\":\"sys\","; |
|
332 |
WI601K += "\"password\":\"sysuser\","; |
|
333 |
WI601K += "\"method\":\"MES_BA_WI601K\","; |
|
334 |
WI601K += "\"servername\":\"London\","; |
|
335 |
WI601K += "\"methodparameter\":{"; |
|
336 |
WI601K += "\"BarCode\":\"" + BarCode + "\","; |
742223
|
337 |
WI601K += "\"HostName\":\"DESKTOP-UV8I1I0\","; |
YY |
338 |
WI601K += "\"Mac\":\"" + a + "\""; |
e46d3b
|
339 |
WI601K += "}"; |
懒 |
340 |
WI601K += "}"; |
|
341 |
|
|
342 |
JObject jo = MES_Interface(WI601K, encryptionKey); |
|
343 |
if (jo["state"].ToString() == "false") |
|
344 |
{ |
742223
|
345 |
t = false; |
e46d3b
|
346 |
System_Bll.WriteLogToDB(new Entity.Base_Log |
742223
|
347 |
|
e46d3b
|
348 |
{ |
懒 |
349 |
CreateUserID = FrmLogin.LoginUserID, |
|
350 |
CreateUserName = FrmLogin.loginUserName, |
|
351 |
LocalIP = FrmLogin.LocalIP, |
742223
|
352 |
LogMessage = "WI601K接口调用失败:" + jo["content"].ToString(), |
YY |
353 |
Type = "称重", |
e46d3b
|
354 |
ClassName = typeof(FrmLogin).ToString() |
懒 |
355 |
}); |
|
356 |
return false; |
|
357 |
} |
|
358 |
return true; |
|
359 |
} |
|
360 |
public JObject WI1032(string PSN, string S_NM, string KP_NM, string KP_Value) |
|
361 |
{ |
|
362 |
string WI1032 = "{ "; |
|
363 |
WI1032 += "\"user\":\"sys\","; |
|
364 |
WI1032 += "\"password\":\"sysuser\","; |
|
365 |
WI1032 += "\"method\":\"MES_AS_WI1032\","; |
|
366 |
WI1032 += "\"servername\":\"London\","; |
|
367 |
WI1032 += "\"methodparameter\":{ "; |
|
368 |
WI1032 += "\"PSN\":\"" + PSN + "\","; //PLC读到的 |
|
369 |
WI1032 += "\"S_NM\":\"" + S_NM + "\","; |
|
370 |
WI1032 += "\"KP_NM\":\"" + KP_NM + "\","; |
|
371 |
WI1032 += "\"KP_Value\":\"" + KP_NM + "\""; |
|
372 |
WI1032 += "}}"; |
|
373 |
|
|
374 |
JObject jo = MES_Interface(WI1032, encryptionKey); |
|
375 |
if (jo["state"].ToString() == "false") |
|
376 |
{ |
742223
|
377 |
t = false; |
e46d3b
|
378 |
System_Bll.WriteLogToDB(new Entity.Base_Log |
懒 |
379 |
{ |
|
380 |
CreateUserID = FrmLogin.LoginUserID, |
|
381 |
CreateUserName = FrmLogin.loginUserName, |
|
382 |
LocalIP = FrmLogin.LocalIP, |
742223
|
383 |
LogMessage = "WI1032接口调用失败:" + jo["content"].ToString(), |
YY |
384 |
Type = "称重", |
e46d3b
|
385 |
ClassName = typeof(FrmLogin).ToString() |
懒 |
386 |
}); |
|
387 |
return null; |
|
388 |
} |
|
389 |
return jo; |
|
390 |
} |
|
391 |
|
742223
|
392 |
public bool WI6041(string Line, string Station, string StationID, string PSN) |
e46d3b
|
393 |
{ |
742223
|
394 |
string WI6041 = "{ "; |
YY |
395 |
WI6041 += "\n\"user\":\"sys\", "; |
|
396 |
WI6041 += "\n\"password\":\"sysuser\", "; |
|
397 |
WI6041 += "\n\"method\":\"MES_BA_WI6041\", "; |
|
398 |
WI6041 += "\n\"servername\":\"London\", "; |
|
399 |
WI6041 += "\n\"methodparameter\":{ "; |
|
400 |
WI6041 += "\n\"content\":{ "; |
|
401 |
WI6041 += "\n\"snresult\":{ "; |
|
402 |
WI6041 += "\n}, "; |
|
403 |
WI6041 += "\n\"testresult\":{ "; |
|
404 |
WI6041 += "\n\"psn\":\"" + PSN + "\", "; |
|
405 |
WI6041 += "\n\"workordernumber\":\"100000236815\", "; |
|
406 |
WI6041 += "\n\"testStationId\":\"" + StationID + "\", "; |
|
407 |
WI6041 += "\n\"testStationName\":\"" + Station + "\", "; |
|
408 |
WI6041 += "\n\"linename\":\"" + Line + "\", "; |
|
409 |
WI6041 += "\n\"hostname\":\"admin-PC-0930\", "; |
|
410 |
WI6041 += "\n\"testTime\":\"" + string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now) + "\", "; |
|
411 |
WI6041 += "\n\"testModel\":\"S87567GB002_BLK1\", "; |
|
412 |
WI6041 += "\n\"testWorkID\":\"\", "; |
|
413 |
WI6041 += "\n\"Fixture\":\"\", "; |
|
414 |
WI6041 += "\n\"FixtureSN\":\"\", "; |
|
415 |
WI6041 += "\n\"Powersupply\":\"\", "; |
|
416 |
WI6041 += "\n\"PowersupplySN\":\"\", "; |
|
417 |
WI6041 += "\n\"Instrument\":\"\", "; |
|
418 |
WI6041 += "\n\"InstrumentSN\":\"\", "; |
|
419 |
WI6041 += "\n\"Operator\":\"\", "; |
|
420 |
WI6041 += "\n\"testVersion\":\"V1.0.1\" "; |
|
421 |
WI6041 += "\n}, "; |
|
422 |
WI6041 += "\n\"itemresult\":[ "; |
|
423 |
WI6041 += "\n],"; |
|
424 |
WI6041 += "\n\"specialitem\":[ "; |
|
425 |
WI6041 += "\n] "; |
|
426 |
WI6041 += "\n}, "; |
|
427 |
WI6041 += "\n\"state\":\"pass\", "; |
|
428 |
WI6041 += "\n\"errcode\":\"\", "; |
|
429 |
WI6041 += "\n\"errmessage\":\"\"} "; |
|
430 |
WI6041 += "\n}"; |
e46d3b
|
431 |
JObject jo = MES_Interface(WI6041, encryptionKey); |
懒 |
432 |
if (jo["state"].ToString() == "false") |
|
433 |
{ |
742223
|
434 |
t = false; |
e46d3b
|
435 |
System_Bll.WriteLogToDB(new Entity.Base_Log |
懒 |
436 |
{ |
|
437 |
CreateUserID = FrmLogin.LoginUserID, |
|
438 |
CreateUserName = FrmLogin.loginUserName, |
|
439 |
LocalIP = FrmLogin.LocalIP, |
742223
|
440 |
LogMessage = "WI6041接口调用失败:" + jo["content"].ToString(), |
YY |
441 |
Type = "称重", |
e46d3b
|
442 |
ClassName = typeof(FrmLogin).ToString() |
懒 |
443 |
}); |
|
444 |
return false; |
|
445 |
} |
|
446 |
return true; |
|
447 |
} |
|
448 |
|
|
449 |
/// <summary> |
|
450 |
/// 通过NetworkInterface获取MAC地址 |
|
451 |
/// </summary> |
|
452 |
/// <returns></returns> |
|
453 |
public static string GetMacByNetworkInterface() |
|
454 |
{ |
|
455 |
try |
|
456 |
{ |
|
457 |
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); |
|
458 |
foreach (NetworkInterface ni in interfaces) |
|
459 |
{ |
|
460 |
return BitConverter.ToString(ni.GetPhysicalAddress().GetAddressBytes()); |
|
461 |
} |
|
462 |
} |
|
463 |
catch (Exception) |
|
464 |
{ |
|
465 |
} |
|
466 |
return "00-00-00-00-00-00"; |
|
467 |
} |
|
468 |
|
|
469 |
} |
|
470 |
} |