-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
1565 lines (1358 loc) · 55.1 KB
/
MainForm.cs
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using Common;
namespace comTest
{
public delegate void DelegatLog(string msg);
public delegate void ViewStatusCallback(string msg);
public partial class MainForm : Form
{
private string m_GZHComPort = null;
private string m_CJBComPort = null;
private int m_GZHBaudRate = 0;
private int m_CJBBaudRate = 0;
private SerialPort m_GZHSerialPort = null;
private SerialPort m_CJBSerialPort = null;
private Boolean m_GZHIsConnect = false;
private Boolean m_CJBIsConnect = false;
List<byte> m_GZHReceivedBytes = new List<byte>();
List<byte> m_CJBReceivedBytes = new List<byte>();
Thread m_GZHReadThread;
Boolean m_IsGZHContinueRead = false;
Thread m_CJBReadThread;
Boolean m_IsCJBContinueRead = false;
FileStream m_FileStream = null;
StreamWriter m_StreamWriter = null;
Export m_export = null;
Export1 m_export1 = null;
private float[][] m_OriginValues;
private float[][][] m_Measurements;
private float[][][] m_Errors;
private float[][][] m_PhaseAngles;
private string[] m_BodyStr = new string[] { "RL", "RA", "TT", "LL", "LA" };
private string[] m_BodyAngleStr = new string[] { "RL", "RA", "TT", "LL", "LA", "RALL", "LARL"};
private string[] m_RateStr = new string[] { "5k", "50k", "250k", "500k", "1k", "1000k" };
private float m_ErrPerM1 = 1;
private float m_ErrPerM2 = 3;
private float[] m_TestWeightLevels;
private float[][] m_TestWeightResults;
private int m_SelectedTestWeight = -1;
private string[] m_WeightPositionLevels;
private int m_SelectedWeightPosition = 0;
private string[] m_MachineTypeList;
private string m_MachineType;
private string m_SerialNumber;
private string m_ProductSerialNumber;
//记录阻抗数量
private int m_Counter = 1;
private int m_GZHCommandStep = 0;
private int m_CJBCommandStep = 0;
private int m_ControlStep = 0;
private bool m_ForDevelopment = true;
private byte m_HeaderByte = (byte)0x02;
private byte m_FooterByte = (byte)0x03;
private byte m_SecondByte = (byte)0x53;
private Command m_CJBCommand;
private Boolean m_IsAutoTest = true;
//自动
private Boolean m_chkIsOnlyTest = false;
private Boolean m_chkIsOriginalR = false;
private int m_cboSelectedTestItem = 0;
//手动
private int m_cbxSelectGZHPosition = 0;
private Boolean m_IsOnlyTestSD = false;
private int m_cboSelectedTestItemSD = 0;
private Boolean m_IsOrgImpSD = false;
private Boolean m_IsBodyTest = false;
public MainForm()
{
InitializeComponent();
m_CJBCommand = new Command(m_HeaderByte, m_SecondByte, m_FooterByte, 1);
Loger.LogRecived += new DelegateLog(OnLog);
Loger.LogSend += new DelegateLog(OnSendLog);
try
{
m_FileStream = new FileStream("log.txt", FileMode.OpenOrCreate);
m_StreamWriter = new StreamWriter(m_FileStream, Encoding.Unicode);
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
}
private void MainForm_Load(object sender, EventArgs e)
{
Initionalization();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
CloseCJBSerialPort();
Thread.Sleep(100);
Params2Ini();
m_StreamWriter.Close();
m_FileStream.Close();
}
catch
{ }
}
private void cboxBaud2_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboxBaud2.Text != null && cboxBaud2.Text != "")
{
m_CJBBaudRate = Int32.Parse(cboxBaud2.Text);
}
}
private void btnCJBOpen_Click(object sender, EventArgs e)
{
OpenCJBSerialPort();
CJBOpenStatusButtons();
}
private void btnCJBClose_Click(object sender, EventArgs e)
{
CloseCJBSerialPort();
InitButtons();
}
private void btnClearSendBox_Click(object sender, EventArgs e)
{
richboxSend.Clear();
}
private void btnClearReceiveBox_Click(object sender, EventArgs e)
{
richboxReceive.Clear();
}
private void btnInitStatus_Click(object sender, EventArgs e)
{
Initionalization();
}
private void btnExport_Click(object sender, EventArgs e)
{
m_export.m_MachineType = m_MachineType;
m_export.m_SerialNumber = m_SerialNumber;
m_export.m_ProductSerialNumber = m_ProductSerialNumber;
m_export.m_Measurements = m_Measurements;
m_export.m_Errors = m_Errors;
m_export.m_TestWeightResults = m_TestWeightResults;
m_export.m_PhaseAngles = m_PhaseAngles;
m_export.export();
}
private void btnExport1_Click(object sender, EventArgs e)
{
m_export1.m_MachineType = m_MachineType;
m_export1.m_SerialNumber = m_SerialNumber;
m_export1.m_ProductSerialNumber = m_ProductSerialNumber;
m_export1.m_Measurements = m_Measurements;
m_export1.m_Errors = m_Errors;
m_export1.m_TestWeightResults = m_TestWeightResults;
m_export1.m_PhaseAngles = m_PhaseAngles;
m_export1.export();
}
private void btnCalibration100_Click(object sender, EventArgs e)
{
if (CheckCJBCom())
{
btnCalibration100.BackColor = Color.Red;
CalibrateBtnsEnable(false);
SendCommand2CJB(100);
}
}
private void btnCalibration75_Click(object sender, EventArgs e)
{
if (CheckCJBCom())
{
btnCalibration75.BackColor = Color.Red;
CalibrateBtnsEnable(false);
SendCommand2CJB(101);
}
}
private void btnCalibration50_Click(object sender, EventArgs e)
{
if (CheckCJBCom())
{
btnCalibration50.BackColor = Color.Red;
CalibrateBtnsEnable(false);
SendCommand2CJB(102);
}
}
private void btnCalibration25_Click(object sender, EventArgs e)
{
if (CheckCJBCom())
{
btnCalibration25.BackColor = Color.Red;
CalibrateBtnsEnable(false);
SendCommand2CJB(103);
}
}
private void btnCalibration0_Click(object sender, EventArgs e)
{
if (CheckCJBCom())
{
btnCalibration0.BackColor = Color.Red;
CalibrateBtnsEnable(false);
SendCommand2CJB(104);
}
}
private void btnGetWeight_Click(object sender, EventArgs e)
{
if (CheckCJBCom())
{
btnGetWeight.BackColor = Color.Red;
txtWeight.Text = "";
CalibrateBtnsEnable(false);
SendCommand2CJB(105);
}
}
private void cbxCom2_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxCom2.Text != null && cbxCom2.Text != "")
{
m_CJBComPort = cbxCom2.Text;
}
}
private void cbxSelectGZHPosition_SelectedIndexChanged(object sender, EventArgs e)
{
m_cbxSelectGZHPosition = cbxSelectGZHPosition.SelectedIndex;
}
private void chkIsOrgImpSD_CheckedChanged(object sender, EventArgs e)
{
m_IsOrgImpSD = chkIsOrgImpSD.Checked;
}
private void btnSDTest_Click(object sender, EventArgs e)
{
m_IsAutoTest = false;
btnSDTest.BackColor = Color.Red;
CalibrateBtnsEnable(false);
StartSDTest();
}
private void cbxTestWeight_SelectedIndexChanged(object sender, EventArgs e)
{
m_SelectedTestWeight = cbxTestWeight.SelectedIndex;
cbxWeightPosition.SelectedIndex = 0;
cbxWeightPosition.Enabled = (m_SelectedTestWeight == 1);
}
private void cbxWeightPosition_SelectedIndexChanged(object sender, EventArgs e)
{
m_SelectedWeightPosition = cbxWeightPosition.SelectedIndex;
}
private void cbxMachineType_SelectedIndexChanged(object sender, EventArgs e)
{
m_MachineType = cbxMachineType.Text;
AutoCreateSerialNumber();
}
private void dupSerialNumber_SelectedItemChanged(object sender, EventArgs e)
{
m_ProductSerialNumber = dupSerialNumber.Text;
m_SerialNumber = m_ProductSerialNumber.Substring(4, m_ProductSerialNumber.Length - 4);
}
private void btnBodyTest_Click(object sender, EventArgs e)
{
m_IsAutoTest = false;
btnBodyTest.BackColor = Color.Red;
CalibrateBtnsEnable(false);
StartBodyTest();
}
private void SendLog(string msg)
{
richboxSend.AppendText(msg + "\r\n");
richboxSend.SelectionStart = richboxSend.Text.Length;
richboxSend.ScrollToCaret();
if (richboxSend.TextLength > 15000)
{
richboxSend.Clear();
}
}
private void OnSendLog(string msg)
{
try
{
this.Invoke(new DelegatLog(SendLog), new object[] { msg });
}
catch
{
}
}
private void Log(string msg)
{
String timeStr = DateTime.Now.ToString();
richboxReceive.AppendText(msg + "\r\n");
LogFile(timeStr + msg + "\r\n");
richboxReceive.SelectionStart = richboxReceive.Text.Length;
richboxReceive.ScrollToCaret();
if (richboxReceive.TextLength > 15000)
{
richboxReceive.Clear();
}
}
private void OnLog(string msg)
{
try
{
this.Invoke(new DelegatLog(Log), new object[] { msg });
}
catch
{
}
}
private void LogFile(string msg)
{
m_StreamWriter.WriteLine(msg);
m_StreamWriter.Flush();
}
private void OpenCJBSerialPort()
{
try
{
if (m_CJBSerialPort != null && m_CJBIsConnect && m_CJBSerialPort.IsOpen)
{
return;
}
if (m_CJBComPort == null || m_CJBBaudRate == 0)
{
MessageBox.Show("请选择采集板COM口和波特率!");
m_CJBIsConnect = false;
return;
}
if (m_CJBComPort == m_GZHComPort && m_GZHIsConnect)
{
MessageBox.Show("这串口已用了.请选择别的串口!");
m_CJBIsConnect = false;
return;
}
m_CJBSerialPort = new SerialPort(m_CJBComPort, m_CJBBaudRate, Parity.None, 8, StopBits.One);
//m_CJBSerialPort.WriteTimeout = 500;
//m_CJBSerialPort.ReadTimeout = 500;
m_CJBSerialPort.Open();
if (m_CJBSerialPort.IsOpen)
{
Loger.send("打开采集板串口成功");
m_CJBIsConnect = true;
m_IsCJBContinueRead = true;
m_CJBReadThread = new Thread(CJBSerialPortRead);
CJBOpenStatusButtons();
m_CJBReadThread.Start();
}
else
{
Loger.send("打开采集板串口失败");
m_CJBIsConnect = false;
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
m_CJBIsConnect = false;
MessageBox.Show("打开采集板串口失败");
}
}
private void CloseCJBSerialPort()
{
if (m_CJBIsConnect && m_CJBSerialPort != null && m_CJBSerialPort.IsOpen)
{
m_IsCJBContinueRead = false;
try
{
m_CJBSerialPort.DiscardInBuffer();
m_CJBSerialPort.DiscardOutBuffer();
m_CJBSerialPort.Close();
m_CJBSerialPort = null;
m_CJBIsConnect = false;
Loger.send("采集板串口成功关闭");
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
}
}
private Boolean CheckCJBCom()
{
if (m_CJBIsConnect)
{
return true;
}
else
{
MessageBox.Show("请连接采集板COM口");
return false;
}
}
private void CJBSerialPortRead()
{
while (m_IsCJBContinueRead)
{
Thread.Sleep(1);
try
{
int nReviceBytesNum = m_CJBSerialPort.BytesToRead; ///收到的字节数。
byte[] ReadBuf = new byte[nReviceBytesNum]; ///定义接收字节数组
m_CJBSerialPort.Read(ReadBuf, 0, nReviceBytesNum); ///接收数据
Boolean hasHeader = false;
foreach (byte temp in ReadBuf)
{
if (temp == m_HeaderByte)
{
m_CJBReceivedBytes.Clear();
hasHeader = true;
}
if (hasHeader || m_CJBReceivedBytes.Count > 0)
{
m_CJBReceivedBytes.Add(temp);
}
if (temp == m_FooterByte)
{
try
{
if (m_CJBReceivedBytes.Count > 3)
{
DealWithCJBData(m_CJBReceivedBytes);
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
m_CJBReceivedBytes.Clear();
hasHeader = false;
}
}
//m_CJBSerialPort.DiscardInBuffer(); //清空接收缓冲区
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.Message);
m_CJBReceivedBytes.Clear();
}
}
}
private void Initionalization()
{
SetPortNames();
// 0~8:档, 0~5:5,50,250,500,1K,1M 0~4:RA,LA,TT,RL,LL,
m_Measurements = Util.CreateJaggedArray<float[][][]>(-1, 9, 5, 6);
m_PhaseAngles = Util.CreateJaggedArray<float[][][]>(-1, 9, 7, 3);
m_Errors = Util.CreateJaggedArray<float[][][]>(-1, 9, 5, 6);
IniValue2Params();
InitButtons();
richboxReceive.Clear();
richboxSend.Clear();
CloseCJBSerialPort();
CJBOpenStatusButtons();
m_SelectedTestWeight = -1;
m_export = new Export();
m_export.m_BodyStr = m_BodyStr;
m_export.m_RateStr = m_RateStr;
m_export.m_TestWeightLevels = m_TestWeightLevels;
m_export.m_WeightPositionLevels = m_WeightPositionLevels;
m_export.m_OriginValues = m_OriginValues;
m_export.m_BodyAngleStr = m_BodyAngleStr;
m_export1 = new Export1();
m_export1.m_BodyStr = m_BodyStr;
m_export1.m_RateStr = m_RateStr;
m_export1.m_TestWeightLevels = m_TestWeightLevels;
m_export1.m_WeightPositionLevels = m_WeightPositionLevels;
m_export1.m_OriginValues = m_OriginValues;
m_export1.m_BodyAngleStr = m_BodyAngleStr;
cbxSelectGZHPosition.SelectedIndex = 0;
cboSelectTestItemsSD.SelectedIndex = 0;
}
private void InitButtons()
{
btnCJBOpen.BackColor = Color.Red;
btnCalibration0.BackColor = Color.LightGray;
btnCalibration25.BackColor = Color.LightGray;
btnCalibration50.BackColor = Color.LightGray;
btnCalibration75.BackColor = Color.LightGray;
btnCalibration100.BackColor = Color.LightGray;
btnCalib1.BackColor = Color.LightGray;
btnCalib2.BackColor = Color.LightGray;
btnCalib3.BackColor = Color.LightGray;
btnCalib4.BackColor = Color.LightGray;
btnCalib5.BackColor = Color.LightGray;
btnCalib6.BackColor = Color.LightGray;
txtWeight.Text = "";
btnGetWeight.BackColor = Color.LightGray;
CalibrateBtnsEnable(false);
btnExport.Enabled = false;
btnExport1.Enabled = false;
btnSDTest.BackColor = Color.LightGray;
btnSDTest.Enabled = false;
btnBodyTest.BackColor = Color.LightGray;
btnBodyTest.Enabled = false;
}
private void CJBOpenStatusButtons()
{
btnCJBOpen.BackColor = m_CJBIsConnect ? Color.Green : Color.Red;
if (m_CJBIsConnect)
{
CalibrateBtnsEnable(true);
Util.cbxValue2Ini("CJB_Com", cbxCom2);
}
}
private void CalibrateBtnsEnable(Boolean enable)
{
if (enable)
{
if (m_CJBIsConnect)
{
btnCalibration100.Enabled = true;
btnCalibration75.Enabled = true;
btnCalibration50.Enabled = true;
btnCalibration25.Enabled = true;
btnCalibration0.Enabled = true;
btnGetWeight.Enabled = true;
btnBodyTest.Enabled = true;
btnSDTest.Enabled = true;
btnCalib1.Enabled = true;
btnCalib2.Enabled = true;
btnCalib3.Enabled = true;
btnCalib4.Enabled = true;
btnCalib5.Enabled = true;
btnCalib6.Enabled = true;
}
}
else
{
btnCalibration100.Enabled = enable;
btnCalibration75.Enabled = enable;
btnCalibration50.Enabled = enable;
btnCalibration25.Enabled = enable;
btnCalibration0.Enabled = enable;
btnGetWeight.Enabled = enable;
btnSDTest.Enabled = enable;
btnBodyTest.Enabled = enable;
btnCalib1.Enabled = enable;
btnCalib2.Enabled = enable;
btnCalib3.Enabled = enable;
btnCalib4.Enabled = enable;
btnCalib5.Enabled = enable;
btnCalib6.Enabled = enable;
}
}
private void Params2Ini()
{
Util.cbxValue2Ini("CJB_Com", cbxCom2);
Util.cbxValue2Ini("CJB_Baud", cboxBaud2);
try
{
for (int i = 0; i < 9; i++)
{
INI.WriteIni("level" + (i + 1), string.Join(",", m_OriginValues[i]));
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
INI.WriteIni("ErrPerM1", m_ErrPerM1.ToString());
INI.WriteIni("ErrPerM2", m_ErrPerM2.ToString());
INI.WriteIni("TestWeightLevels", string.Join(",", m_TestWeightLevels));
INI.WriteIni("MachineTypeList", string.Join(",", m_MachineTypeList));
}
private void IniValue2Params()
{
//COM
Util.IniValue2Combox("CJB_Com", cbxCom2);
Util.IniValue2Combox("CJB_Baud", cboxBaud2);
//电阻
m_OriginValues = new float[9][] {
new float[5] { 510, 750, 50, 510, 750},
new float[5] { 360, 680, 43, 360, 680},
new float[5] { 300, 560, 36, 300, 560},
new float[5] { 250, 430, 30, 250, 430},
new float[5] { 200, 300, 20, 200, 300},
new float[5] { 150, 200, 10, 150, 200},
new float[5] { 200, 360, 15, 200, 360},
new float[5] { 340, 470, 27, 340, 470},
new float[5] { 470, 620, 36, 470, 620}
};
for (int i = 0; i < 9; i++)
{
try
{
string level = INI.GetValue("level" + (i + 1));
string[] strValues = level.Split(',');
if (strValues.Length == 5)
{
m_OriginValues[i] = Array.ConvertAll(strValues, float.Parse);
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
}
//误差
try
{
m_ErrPerM1 = float.Parse(INI.GetValue("ErrPerM1"));
m_ErrPerM2 = float.Parse(INI.GetValue("ErrPerM2"));
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
//体重测量位置
m_WeightPositionLevels = new string[] { "中心", "前", "后", "左", "右" };
cbxWeightPosition.Items.Clear();
foreach (string level in m_WeightPositionLevels)
{
cbxWeightPosition.Items.Add(level);
}
cbxWeightPosition.SelectedIndex = 0;
//体重测量选项
m_TestWeightLevels = new float[] { 10, 15, 50, 120 };
m_TestWeightResults = Util.CreateJaggedArray<float[][]>(-1, m_TestWeightLevels.Length, m_WeightPositionLevels.Length);
try
{
string levels = INI.GetValue("TestWeightLevels");
string[] arrLevels = levels.Split(',');
if (arrLevels.Length > 1)
{
m_TestWeightLevels = Array.ConvertAll(arrLevels, float.Parse); ;
m_TestWeightResults = Util.CreateJaggedArray<float[][]>(-1, arrLevels.Length, m_WeightPositionLevels.Length);
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
cbxTestWeight.Items.Clear();
foreach (float level in m_TestWeightLevels)
{
cbxTestWeight.Items.Add(level);
}
//设备设置
m_MachineTypeList = new string[] { "AiNST-CNDS20", "AiNST-CNDS10", "AiNST-KNDS20", "AiNST-KNDS10" };
try
{
string levels = INI.GetValue("MachineTypeList");
string[] arrLevels = levels.Split(',');
if (arrLevels.Length > 1)
{
m_MachineTypeList = arrLevels;
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.ToString());
}
cbxMachineType.Items.Clear();
foreach (string type in m_MachineTypeList)
{
cbxMachineType.Items.Add(type);
}
cbxMachineType.SelectedIndex = 0;
}
private void SendCommand2GZH(byte[] data)
{
try
{
if (data == null || data.Length <= 0)
{
Loger.send("GZH:发送数据为空");
return;
}
Loger.send("GZH:" + Util.logBytes(data));
if (m_GZHSerialPort != null && m_GZHSerialPort.IsOpen)
{
try
{
m_GZHSerialPort.DiscardOutBuffer();
m_GZHSerialPort.Write(data, 0, data.Length);
}
catch (Exception ex)
{
throw ex;
}
}
else
{
MessageBox.Show("工装串口未打开");
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.Message);
}
}
private void SendCommand2CJB(byte[] data)
{
try
{
if (data == null || data.Length <= 0)
{
Loger.send("CJB:发送数据为空");
return;
}
Loger.send("CJB:" + Util.logBytes(data));
if (m_CJBSerialPort != null && m_CJBSerialPort.IsOpen)
{
try
{
m_CJBSerialPort.DiscardOutBuffer();
m_CJBSerialPort.Write(data, 0, data.Length);
}
catch (Exception ex)
{
throw ex;
}
}
else
{
MessageBox.Show("采集板串口未打开");
}
}
catch (Exception ex)
{
if (m_ForDevelopment) Loger.send(ex.Message);
}
}
private void DealWithCJBData(List<byte> receivedBytes)
{
int length = ((receivedBytes[3] - 10) << 6) + (receivedBytes[2] - 10);
//长度验证
if (length != receivedBytes.Count - 6)
{
Loger.recv("CJB:长度不一样 " + (length + 6) + ", " + receivedBytes.Count);
return;
}
byte[] bytes = new byte[length + 6];
Array.Copy(receivedBytes.ToArray(), 0, bytes, 0, length + 6);
if (m_ForDevelopment)
{
// log decrypted message
string msg = string.Empty;
msg = Util.logBytes(bytes);
msg += "==>" + System.Text.Encoding.Default.GetString(bytes);
Loger.recv("CJB:" + msg);
}
//CRC校验
if (Util.getCRC(bytes, 1, length + 3) != bytes[length + 4])
{
Loger.recv("CJB:CRC校验失败");
//Loger.recv("CJB:重新发送指令");
//TODO
//SendCommand2CJB(m_ReadStep);
//return;
}
//体重校准
if (bytes[4] == 0x4c && bytes[5] == 0x43 && bytes[6] == 0x58)
{
switch (m_CJBCommandStep)
{
case 100:
btnCalibration100.BackColor = Color.Green;
Loger.recv("CJB:体重校准100kg完成");
CalibrateBtnsEnable(true);
break;
case 101:
btnCalibration75.BackColor = Color.Green;
Loger.recv("CJB:体重校准75kg完成");
CalibrateBtnsEnable(true);
break;
case 102:
btnCalibration50.BackColor = Color.Green;
Loger.recv("CJB:体重校准50kg完成");
CalibrateBtnsEnable(true);
break;
case 103:
btnCalibration25.BackColor = Color.Green;
Loger.recv("CJB:体重校准25kg完成");
CalibrateBtnsEnable(true);
break;
case 104:
btnCalibration0.BackColor = Color.Green;
Loger.recv("CJB:体重校准0kg完成");
CalibrateBtnsEnable(true);
break;
default:
break;
}
return;
}
//体重的读取
if (m_CJBCommandStep == 105 && bytes[4] == 0x4c && bytes[5] == 0x43 && bytes[6] == 0x4c)
{
byte[] bytes1 = new byte[8];
Array.Copy(bytes, 8, bytes1, 0, 8);
string weightStr = System.Text.Encoding.Default.GetString(bytes1);
float weightf = Util.toFloat(Util.exchange(weightStr));
if (m_SelectedTestWeight > -1)
{
m_TestWeightResults[m_SelectedTestWeight][m_SelectedWeightPosition] = weightf;
}
this.Invoke(new EventHandler(delegate
{
btnGetWeight.BackColor = Color.Green;
Loger.recv("CJB: 体重:" + weightf.ToString());
CalibrateBtnsEnable(true);
txtWeight.Text = weightf.ToString();
}));
m_CJBCommandStep = -1;
return;
}
//校准完成
if (bytes[4] == 0x45 && bytes[5] == 0x57 && length > 6)
{
Loger.recv(String.Format("CJB: 校准{0}完成", Int32.Parse(System.Text.Encoding.Default.GetString(new byte[] { bytes[8] })) + 1));
}
//节段检测 sp m_counter 02 43 0c 0a 53 50 46 03
if (bytes[4] == 0x53 && bytes[5] == 0x50)
{
Loger.recv("阻抗: " + string.Format("{0}", m_Counter++));
}
//档位校准完成
if (bytes[4] == 0x43 && bytes[5] == 0x44 && bytes[6] == 0x52)
{
switch (bytes[7])
{
case 0x30:
Loger.recv("CJB:6档位校准完成");
btnCalib6.BackColor = Color.Green;
break;
case 0x31:
Loger.recv("CJB:5档位校准完成");
btnCalib5.BackColor = Color.Green;
break;
case 0x32:
Loger.recv("CJB:4档位校准完成");
btnCalib4.BackColor = Color.Green;
break;
case 0x33:
Loger.recv("CJB:3档位校准完成");
btnCalib3.BackColor = Color.Green;
break;
case 0x34:
Loger.recv("CJB:2档位校准完成");
btnCalib2.BackColor = Color.Green;
break;
case 0x35:
Loger.recv("CJB:1档位校准完成");
btnCalib1.BackColor = Color.Green;
break;
default:
break;
}
CalibrateBtnsEnable(true);
return;
}
//测试完成
if (bytes[4] == 0x4d)
{
switch (bytes[5])
{
case 0x31:
Loger.recv(String.Format("CJB: {0}档位:M1测试完成", m_GZHCommandStep + 1));
SendCommand2CJB(61);
break;
case 0x32:
Loger.recv(String.Format("CJB: {0}档位:M2测试完成", m_GZHCommandStep + 1));
SendCommand2CJB(71);
break;
case 0x33:
Loger.recv(String.Format("CJB: {0}档位:M3测试完成", m_GZHCommandStep + 1));