FrnBarCodeInfo.cs
20.6 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ACTETHERLib;
using System.Configuration;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace HaierExe03
{
public partial class FrnBarCodeInfo : Form
{
#region 扫描相关数据的定义
//-------------------------------------------------------------------------------------------------------
//编码规则:由4位数据组成 1位:表示系列型号 2位:国内或国外
//10开头的表示HPM系列的国内洗衣机 最后的那个数表示洗衣机的KG数
public const short HPM_5KG_D = 1005; //domestic and foreign 国内和国外
public const short HPM_6KG_D = 1006;
public const short HPM_7KG_D = 1007;
public const short HPM_8KG_D = 1008;
//11开头的表示HPM系列的国外洗衣机
public const short HPM_5KG_F = 1105;
public const short HPM_6KG_F = 1106;
public const short HPM_7KG_F = 1107;
public const short HPM_8KG_F = 1108;
//20开头的表示FPA系列的国内洗衣机
public const short FPA_6KG_D = 2006;
public const short FPA_7KG_D = 2007;
public const short FPA_8KG_D = 2008;
//21开头的表示FPA系列的国外洗衣机
public const short FPA_6KG_F = 2106;
public const short FPA_7KG_F = 2107;
public const short FPA_8KG_F = 2108;
// 写入数据标志
public const short Write_Flag_OK = 1001;//表示上位机写入PLC成功
public const short Write_Flag_NG = 1002;//表示PLC处理数据成功
//-----------------------------------------------------------------------
//条码设备的IP地址和对应的存储地址 定义 这里考虑最多添加10个扫描设备
public short[] shortProductTypeValue=new short[10]{0,0,0,0,0,0,0,0,0,0};
public string[] strProductTypeAddress=new string[10]{"D2000", "D2002", "D2004", "D2006", "D2008", "D2010", "D2012", "D2014", "D2016", "D2018"};
public string[] strProductTypeFlag = new string[10] {"D2001", "D2003", "D2005", "D2007", "D2009", "D2011", "D2013", "D2015", "D2017", "D2019"};
//-------------------------------------------------------------------------------------------------------
#endregion
#region 定义要用到的类
private IPEndPoint ServerInfo;//存放服务器的IP和端口信息
private Socket ServerSocket;//服务端运行的SOCKET
private Thread ServerThread;//服务端运行的线程
private Socket[] ClientSocket;//为客户端建立的SOCKET连接
private int ClientNumb;//存放客户端数量
private byte[] MsgBuffer;//存放消息数据
private string LocalIP;
private int LocalPort=51237;//定义端口号
private string remoteIP="";//定义远程计算机的IP地址
string tb_Name = "T_BarCode";
LinkSqlDB SqlDBItem = new LinkSqlDB();
#endregion
private string GetLocalIPAddress()
{
//string hostName = Dns.GetHostName();//本机名
//System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
//System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6
IPAddress[] addresslist = Dns.GetHostByName(Dns.GetHostName()).AddressList;
//IPAddress[] addresslist=Dns.GetHostAddresses(Dns.GetHostName());
for (int i = 0; i < addresslist.Length; i++)
{
if (!addresslist[i].IsIPv6LinkLocal)
{
return addresslist[i].ToString();
}
}
return addresslist[0].ToString();
}
/// <summary>
/// //开启服务器 侦听客户端的连接请求
/// </summary>
private void SocketListen()
{
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//提供一个 IP 地址,指示服务器应侦听所有网络接口上的客户端活动
//IPAddress ip = IPAddress.Any;
IPAddress ip = IPAddress.Parse(GetLocalIPAddress());
ServerInfo = new IPEndPoint(ip, LocalPort);
ServerSocket.Bind(ServerInfo);//将SOCKET接口和IP端口绑定
ServerSocket.Listen(25);//开始监听,并且挂起数为25
ClientSocket = new Socket[65535];//为客服端提供连接个数
MsgBuffer = new byte[65535];//消息数据的大小
ClientNumb = 0;//客服端数据从0开始计数
ServerThread = new Thread(new ThreadStart(RecieveAccept));//将接受客户端连接的方法委托给线程
ServerThread.Start();//线程开始运行
CheckForIllegalCrossThreadCalls = false;//不捕获对错误线程的调用
this.ClientList.Items.Add("服务器"+ip.ToString()+"于"+DateTime.Now.ToString()+"开始运行\n");
}
/// <summary>
/// //接受客户端连接的方法
/// </summary>
private void RecieveAccept()
{
while (true)
{
//Accept 以同步方式从侦听套接字的连接请求队列中提取第一个挂起的连接请求,然后创建并返回新的 Socket。
//在阻止模式中,Accept 将一直处于阻止状态,直到传入的连接尝试排入队列。连接被接受后,原来的 Socket 继续将传入的连接请求排入队列,直到您关闭它。
ClientSocket[ClientNumb] = ServerSocket.Accept();
ClientSocket[ClientNumb].BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), ClientSocket[ClientNumb]);
//string strmsg = Encoding.ASCII.GetString(MsgBuffer);
lock (this.ClientList)
{
this.ClientList.Items.Add(ClientSocket[ClientNumb].RemoteEndPoint.ToString() + "成功连接服务器");
}
ClientNumb++;
}
}
/// <summary>
/// //回发数据给客户端
/// </summary>
/// <param name="AR"></param>
private void RecieveCallBack(IAsyncResult AR)
{
try
{
Socket RSocket = (Socket)AR.AsyncState;
int REnd = RSocket.EndReceive(AR);
remoteIP=RSocket.RemoteEndPoint.ToString().Split(':')[0];
RSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack), RSocket);
}
catch { }
}
public FrnBarCodeInfo()
{
InitializeComponent();
dgv = new DgvDisplayUtil(dataGridView1);
dgv.IsCheckbox = false;
dgv.AddDgvTitle("C_ID", "编号", 25);
dgv.AddDgvTitle("Station", "工位", 120);
dgv.AddDgvTitle("StationCode", "工位代号", 140);
dgv.AddDgvTitle("IP", "设备IP", 130);
dgv.AddDgvTitle("barcode", "条码", 120);
dgv.AddDgvTitle("typeInfo", "解析信息", 120);
dgv.AddDateTimeDgvTitle("InputTime", "录入时间", 150);
RefreshData();
}
/// <summary>
/// 刷新
/// </summary>
public void RefreshData()
{
try
{
dgv.RefreshDgvData(SqlDBItem.GetALL(tb_Name));
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
}
/// <summary>
/// 刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton1_Click(object sender, EventArgs e)
{
RefreshData();
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
toolStripLabel1.Text = string.Format("共{0}条记录,筛选出{1}条记录", dgv.dtSource == null ? 0 : dgv.dtSource.Rows.Count, dataGridView1.Rows.Count);
}
/// <summary>
/// 新建
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton2_Click(object sender, EventArgs e)
{
//FrmUserNew item = new FrmUserNew();
item.ShowDialog();
RefreshData();
}
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton4_Click(object sender, EventArgs e)
{
List<DataRow> drs = dgv.GetCheckedRows();
if (drs == null || drs.Count == 0)
{
MessageUtil.ShowWarning("请选择要编辑的记录行!!!");
return;
}
frmBarcodeModify frm = new frmBarcodeModify(drs[0]["C_ID"].ToString());
frm.ShowDialog();
RefreshData();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要删除此项目", this.item.Text, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
List<DataRow> drs = dgv.GetCheckedRows();
if (drs == null || drs.Count == 0)
{
MessageUtil.ShowWarning("请选择要删除的记录行!!!");
return;
}
int Count = drs.Count;
int ErrQty = 0;
foreach (DataRow dr in drs)
{
string C_ID = dr["C_ID"].ToString();
string strCondition = "C_ID=" + C_ID;
if (!SqlDBItem.Delete(tb_Name, strCondition))
{
ErrQty++;
}
}
if (ErrQty > 0)
{
MessageUtil.ShowError(string.Format("删除用户组记录,共操作{0}条记录,成功{1}条,失败{2}条。", Count, Count - ErrQty, ErrQty));
}
else
{
MessageUtil.ShowTips(string.Format("成功删除{0}条用户组记录。", Count));
}
RefreshData();
}
}
#region Testing
//private void settingToolStripMenuItem_Click(object sender, EventArgs e)
//{
// FrmServerMain frmserver = new FrmServerMain();
// frmserver.ShowDialog();
//}
//private void socketTCP客户端ToolStripMenuItem_Click(object sender, EventArgs e)
//{
// FrmClientMain frmclient = new FrmClientMain();
// frmclient.ShowDialog();
//}
#endregion
private void FrnBarCodeInfo_Load(object sender, EventArgs e)
{
SocketListen();
masterPLC3.ActHostAddress = ConfigurationSettings.AppSettings["MasterPLC3"];
}
private void FrnBarCodeInfo_FormClosed(object sender, FormClosedEventArgs e)
{
ServerThread.Abort();//线程终止
ServerSocket.Close();//关闭SOCKET
}
/// <summary>
/// 将型号数据写到PLC,并记录到数据库中
/// </summary>
/// <param name="strRemoteIP"></param>
/// <param name="strMsg"></param>
AxACTETHERLib.AxActQNUDECPUTCP masterPLC3 = new AxACTETHERLib.AxActQNUDECPUTCP();
short[] PLCData = new short[2];
private bool WriteProductMasterPLC(string strRemoteIP, string strMsg)
{
//分解出那个扫描设备传来的数据 根据最后的IP地址判断
string str1 = strRemoteIP.Split('.')[3];
//解析型号值
PLCData[0] = 1005;
//将数据记录到数据库中
//SqlDBItem.InsertT();
if (masterPLC3.Open() == 0)
{
switch (int.Parse(str1))
{
case 41:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2001", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2000\nD2001", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 42:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2003", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2002\nD2003", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 43:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2005", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2004\nD2005", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 44:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2007", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2006\nD2007", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 45:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2009", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2008\nD2009", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 46:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2011", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2010\nD2011", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 47:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2013", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2012\nD2013", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 48:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2015", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2014\nD2015", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 49:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2017", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2016\nD2017", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
case 50:
{
//读取PLC 的写入型号标志D2001 如果不为1001 表示可以写入数据
if (masterPLC3.ReadDeviceRandom2("D2019", 1, out PLCData[1]) == 0)
{
if (PLCData[1] != 1001)
{
PLCData[1] = 1001;
if (masterPLC3.WriteDeviceRandom2("D2018\nD2019", 2, ref PLCData[0]) == 0)
{
return true;
}
}
}
break;
}
default:
{
break;
}
}
MessageBox.Show("PLC Write failuer!");
//关闭PLC连接
masterPLC3.Close();
return false;
}
else
{
MessageBox.Show("PLC Open failure!");
return false;
}
}
}
}