Form2.cs
15.2 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
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System;
using System.Drawing;
using System.Threading;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace TinLabel
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
crc.OpenResourceLog = true;
crc.GetLanguageEvent += Crc_GetLanguageEvent;
crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
crc.CurrLanguage = Crc_GetLanguageEvent();
}
private string Crc_GetLanguageEvent()
{
return Setting.Device_Default_Language;
}
private void Crc_LanguageChangeEvent(object sender, EventArgs e)
{
LanguageProcess();
}
public static PuYueRFID_FR540SP_C2S RFID = new PuYueRFID_FR540SP_C2S();
public static PrinterHelper printerHelper = new PrinterHelper();
Task RFIDTask;
bool rfidwork = false;
private void Form1_Load(object sender, EventArgs e)
{
FormClosing += Form1_FormClosing;
Label loadinfolabel = new Label();
loadinfolabel.AutoSize = false;
//loadinfolabel.Size = new Size(Width, Height);
loadinfolabel.Dock = DockStyle.Fill;
loadinfolabel.TextAlign = ContentAlignment.MiddleCenter;
loadinfolabel.Text = crc.GetString("Res0001.03426328", "设备初始化中...");
loadinfolabel.Font = new Font("Arial", 21.75F, FontStyle.Regular, GraphicsUnit.Point);
this.Controls.Add(loadinfolabel);
loadinfolabel.BringToFront();
Task.Run(() =>
{
if (init(out string errmsg))
{
this.Controls.Remove(loadinfolabel);
}
else
{
this.Invoke(new Action(() =>
{
loadinfolabel.ForeColor = Color.Red;
loadinfolabel.Text = errmsg;
}));
}
});
textBox_qrcode.KeyUp += TextBox_qrcode_KeyUp;
RFIDTask = Task.Run(() =>
{
RFIDProcess();
});
btn_printlabel.Enabled = false;
btn_cancel.Visible = false;
btn_rewriterfid.Visible = false;
btn_readrfid.Enabled = false;
label_codeinfo.Text = crc.GetString("Res0002.a336d735", "请扫码");
label_process.Text = string.Empty;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
LogUtil.info("正常退出");
}
private void TextBox_qrcode_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab || e.KeyCode == Keys.Enter)
{
process();
}
}
ReelParam reelParam = new ReelParam();
void process()
{
textBox_qrcode.SelectAll();
reelParam = new ReelParam();
var rightcode = textBox_qrcode.Text.Trim();
if (!string.IsNullOrEmpty(rightcode))
{
var cs = rightcode.Split('|');
if (cs.Length >= 7)
{
reelParam.PN = cs[0].Substring(1);
reelParam.LOT = cs[3].Substring(1);
reelParam.EXP = cs[4].Substring(1);
reelParam.SN = cs[5].Substring(1);
reelParam.WareCode = rightcode;
reelParam.ReeID = reelParam.SN;
reelParam.RFID = "S" + reelParam.SN;
label_codeinfo.Text = $"SN: {reelParam.SN}\r\nPN: {reelParam.PN}\r\nRFID: {reelParam.RFID}";
label_codeinfo.ForeColor = Color.DarkGreen;
if (cb_autoprint.Checked)
{
PrintLabel();
}
btn_printlabel.Enabled = true;
btn_readrfid.Enabled = true;
}
else
{
reelParam = null;
btn_printlabel.Enabled = false;
label_codeinfo.Text = crc.GetString("Res0003.8cac7dc7", "条码不正确,请重新扫码.");
label_codeinfo.ForeColor = Color.Red;
textBox_qrcode.Text = "";
}
}
}
void PrintLabel()
{
if (reelParam == null)
{
MessageBox.Show(crc.GetString("Res0004.92e1e70c", "未读到有效的条码,无法打印"));
return;
}
btn_printlabel.Enabled = false;
var PrintTask = printerHelper.PrintLabel(reelParam, Setting.Printer_Labelname);
PrintTask.Wait(5000);
if (PrintTask.IsCompleted)
{
var (result, msg) = PrintTask.Result;
if (result)
{
rfidwork = true;
btn_printlabel.Enabled = false;
btn_readrfid.Enabled = false;
}
else
{
MessageBox.Show(crc.GetString("Res0005.05c6d4d0", "打印失败:") + msg);
btn_printlabel.Enabled = true;
return;
}
}
else
{
MessageBox.Show(crc.GetString("Res0006.15521f9b", "打印机响应超时."));
return;
}
}
void RFIDProcess()
{
while (!this.IsDisposed)
{
Task.Delay(500);
if (reelParam == null)
{
LogUtil.info("未读到有效的条码,无法写入rfid");
continue;
}
if (!rfidwork)
continue;
this.Invoke((EventHandler)delegate
{
btn_cancel.Visible = true;
if (label_process.ForeColor == Color.Black)
{
label_process.Text = crc.GetString("Res0007.78622f56", "请将标签放置到RFID读卡器上,并稍作等待.");
label_process.ForeColor = Color.YellowGreen;
}
else
{
label_process.Text = crc.GetString("Res0007.78622f56", "请将标签放置到RFID读卡器上,并稍作等待.");
label_process.ForeColor = Color.Black;
}
Application.DoEvents();
});
var epc = reelParam.RFID.PadRight(16);
//var r = RFID.ReOpen();
RFID.WriteByte(550, new byte[] { 0x40, 0x00 });
Thread.Sleep(300);
RFID.WriteEPC(Encoding.ASCII.GetBytes(epc));
Thread.Sleep(500);
if (RFID.ReadEPC(16, out byte[] data))
{
if (data[0] != 0xFF)
{
var ds = Encoding.ASCII.GetString(data).Trim();
if (ds == reelParam.RFID)
{
LogUtil.info("RFID 写入成功:" + ds);
rfidwork = false;
this.Invoke((EventHandler)delegate
{
label_process.Text = crc.GetString("Res0008.0c5aab19", "RFID写入成功! ") + " RFID:" + ds;
label_process.ForeColor = Color.DarkGreen;
btn_cancel.Visible = false;
btn_rewriterfid.Visible = false;
btn_printlabel.Enabled = true;
btn_readrfid.Enabled = true;
//label_codeinfo.Text = "请扫码";
});
continue;
}
else
{
LogUtil.info("失败的 RFID:" + ds);
}
}
}
}
}
public bool init(out string errmsg)
{
bool IsLoadOk = true;
errmsg = "";
if (!printerHelper.Connection(Setting.Printer_IP))
{
errmsg += crc.GetString("Res0009.f11b67ff", "标签打印机打开失败") + "\n";
IsLoadOk = false;
}
else
LogUtil.info("标签打印机打开成功");
RFID.Open(Setting.Device_RFID_IP);
if (!RFID.IsConn)
{
IsLoadOk = false;
errmsg += crc.GetString("Res0010.279f5658", "RFID连接失败,IP:") + $"{Setting.Device_RFID_IP}\n";
}
else
LogUtil.info("RFID连接成功,IP:" + Setting.Device_RFID_IP);
if (!IsLoadOk)
{
//label_process.Text = msg;
}
return IsLoadOk;
}
private void btn_printlabel_Click(object sender, EventArgs e)
{
LogUtil.info("手动点击标签打印");
PrintLabel();
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 aboutBox1 = new AboutBox1();
aboutBox1.ShowDialog();
}
private void btn_cancel_Click(object sender, EventArgs e)
{
btn_cancel.Visible = false;
rfidwork = false;
label_process.Text = crc.GetString("Res0011.cbadd79b", "RFID写入已取消");
btn_rewriterfid.Visible = true;
btn_printlabel.Enabled = true;
btn_readrfid.Enabled = true;
}
private void btn_rewriterfid_Click(object sender, EventArgs e)
{
btn_rewriterfid.Visible = false;
rfidwork = true;
btn_cancel.Visible = true;
}
private void 标签设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
var f = new Frm_LabelEdit();
f.printerHelper = printerHelper;
crc.LanguageProcess(f);
f.ShowDialog();
}
private void btn_readrfid_click(object sender, EventArgs e)
{
if (reelParam == null)
{
MessageBox.Show(crc.GetString("Res0004.92e1e70c", "未读到有效的条码,无法打印"));
return;
}
//label_process.Text = "正在读取RFID";
//label_process.ForeColor = Color.YellowGreen;
LogUtil.info("手动读卡:" + reelParam.ToDetailStr());
Application.DoEvents();
RFID.ReOpen();
Task.Run(() =>
{
for (int i = 0; i < 10; i++)
{
this.Invoke((EventHandler)delegate
{
if (label_process.ForeColor == Color.Black)
{
label_process.Text = crc.GetString("Res0007.78622f56", "请将标签放置到RFID读卡器上,并稍作等待.");
label_process.ForeColor = Color.YellowGreen;
}
else
{
label_process.Text = crc.GetString("Res0007.78622f56", "请将标签放置到RFID读卡器上,并稍作等待.");
label_process.ForeColor = Color.Black;
}
});
Task.Delay(500).Wait();
if (RFID.ReadEPC(16, out byte[] data))
{
if (data[0] != 0xFF)
{
var ds = Encoding.ASCII.GetString(data).Trim();
if (ds == reelParam.RFID)
{
LogUtil.info("RFID与条码相符! " + " RFID:" + ds);
this.Invoke((EventHandler)delegate
{
label_process.Text = crc.GetString("Res0012.cb82b33c", "RFID与条码相符! ") + " RFID:" + ds;
label_process.ForeColor = Color.DarkGreen;
//label_codeinfo.Text = "请扫码";
});
}
else
{
LogUtil.info("RFID与条码不相符! " + " RFID:" + ds);
this.Invoke((EventHandler)delegate
{
label_process.Text = crc.GetString("Res0013.2f81e963", "RFID与条码不相符! ") + " RFID:" + ds;
label_process.ForeColor = Color.Red;
//label_codeinfo.Text = "请扫码";
});
}
this.Invoke((EventHandler)delegate
{
btn_cancel.Visible = false;
//btn_rewriterfid.Visible = false;
btn_printlabel.Enabled = true;
btn_readrfid.Enabled = true;
});
return;
}
else
{
LogUtil.info("读卡数据错误:" + BitConverter.ToString(data));
}
}
else
{
LogUtil.info("没有读到卡");
}
}
this.Invoke((EventHandler)delegate
{
label_process.Text = crc.GetString("Res0014.5a9787db", "RFID读取失败");
label_process.ForeColor = Color.Red;
btn_cancel.Visible = false;
//btn_rewriterfid.Visible = false;
btn_printlabel.Enabled = true;
btn_readrfid.Enabled = true;
});
});
}
private void btn_barcodeenter_Click(object sender, EventArgs e)
{
process();
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox_qrcode.Focus();
}
private void 简体中文ToolStripMenuItem_Click(object sender, EventArgs e)
{
Setting.Device_Default_Language = "zh-CN";
crc.LanguageChange();
}
private void englishToolStripMenuItem_Click(object sender, EventArgs e)
{
Setting.Device_Default_Language = "en-US";
crc.LanguageChange();
}
private void Form2_Shown(object sender, EventArgs e)
{
LanguageProcess();
}
void LanguageProcess()
{
crc.LanguageProcess(this, this.GetType().Name);
crc.ProcessListItem(menuStrip1.Items, "menuStrip1");
}
}
}