-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
2380 lines (2052 loc) · 80.2 KB
/
Form1.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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using Common;
using Common.Do;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using DevComponents.DotNetBar;
namespace comTest
{
//public delegate void DelegatLog(string msg);
public delegate void DelegatBack(Back back);
public partial class Form1 : RibbonForm
{
private string m_comPort;
private byte m_baud;
private byte m_baud08;
private SerialPort m_serialPort=null;
private byte m_gender;
List<byte> m_recvBytes=new List<byte>();
FileStream fs = null;
StreamWriter sw = null;
private bool m_isWorkStyle = true;
private byte[] m_bytes = null;
private int m_calibariton_type = 0;
private bool m_isAscii = true;
private float m_LAStand, m_RAStand, m_TTStand, m_LLStand, m_RLStand;
private float m_LAP, m_RAP, m_TTP, m_LLP, m_RLP;
private bool m_forDevelopment=false;
private int m_ReadStep = 0;
//记录阻抗数量
private int m_counter;
public Form1()
{
InitializeComponent();
Loger.LogRecived += new DelegateLog(OnLog);
Loger.LogSend += new DelegateLog(OnSendLog);
try
{
fs = new FileStream("log.txt", FileMode.OpenOrCreate);
sw = new StreamWriter(fs, Encoding.Unicode);
}
catch (Exception ex)
{
Loger.send(ex.ToString());
}
}
private void sendLog(string msg)
{
richBoxSend.Text +=msg + "\r\n";
richBoxSend.SelectionStart = richBoxSend.Text.Length;
richBoxSend.ScrollToCaret();
if (richBoxSend.TextLength >5000) 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.Text +=msg + "\r\n";
logFile(timeStr + msg + "\r\n");
richboxReceive.SelectionStart = richboxReceive.Text.Length;
richboxReceive.ScrollToCaret();
if (richboxReceive.TextLength >5000) richboxReceive.Clear();
}
private void OnLog(string msg)
{
try
{
this.Invoke(new DelegatLog(Log), new object[] { msg });
}
catch
{
}
}
private void cbxCom_TabIndexChanged(object sender, EventArgs e)
{
m_comPort = cbxCom.SelectedText;
}
private void cbxBaud_SelectedIndexChanged(object sender, EventArgs e)
{
//1khz
//5khz
//10khz
//20khz
//50khz
//100khz
//200khz
//250khz
//500khz
//1000khz
}
/// <summary>
/// 发送命令
/// </summary>
/// <param name="SendData">发送数据</param>
/// <param name="ReceiveData">接收数据</param>
/// <param name="Overtime">超时时间</param>
/// <returns></returns>
public int SendCommand(byte[] SendData)
{
if (m_serialPort != null && m_serialPort.IsOpen)
{
try
{
m_serialPort.WriteTimeout = 500;
m_serialPort.ReadTimeout = 500;
m_serialPort.DiscardInBuffer(); //清空接收缓冲区
m_serialPort.Write(SendData, 0, SendData.Length);
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
else
{
MessageBox.Show("串口未打开");
}
return -1;
}
//异或校验码生成
private byte getCRC(byte [] value, int index, int number)
{
int res = 0;
for (int i = 0; i < number; i++)
{
res += value[i + index];
}
return (byte)((res & 0x3F) + 10);
}
private void btnSender_Click(object sender, EventArgs e)
{
try
{
if (m_serialPort!=null && m_serialPort.IsOpen)
{
byte[] sendData = new byte[6];
sendData[0] = 0x64;
sendData[1] = 0x95;
sendData[2] = 0x02;
sendData[3] = 0x04;
sendData[4] = m_baud;
sendData[5] = getCRC(sendData, 0, sendData.Length-1);
string msg = string.Empty;
foreach (byte temp in sendData)
{
msg += temp.ToString("x2");
msg += " ";
}
Loger.send(string.Format("发送:{0}", msg));
int n = SendCommand(sendData);
}
else
Loger.send("串口未打开");
}
catch (Exception ex)
{
Loger.send("打开串口失败!");
Loger.send(ex.Message);
}
}
private void cbxCom_SelectedValueChanged(object sender, EventArgs e)
{
m_comPort = cbxCom.Text;
}
private void cbxBaud_SelectedValueChanged(object sender, EventArgs e)
{
//1khz
//5khz
//10khz
//20khz
//50khz
//100khz
//200khz
//250khz
//500khz
//1000khz
//switch (cbxBaud.Text.Trim())
//{
// case "1khz":
// m_baud = 0;
// break;
// case "5khz":
// m_baud = 1;
// break;
// case "10khz":
// m_baud = 2;
// break;
// case "20khz":
// m_baud = 3;
// break;
// case "50khz":
// m_baud = 4;
// break;
// case "100khz":
// m_baud = 5;
// break;
// case "200khz":
// m_baud = 6;
// break;
// case "250khz":
// m_baud = 7;
// break;
// case "500khz":
// m_baud = 8;
// break;
// case "1000khz":
// m_baud = 9;
// break;
//}
}
private void Form1_Load(object sender, EventArgs e)
{
cbxCom.SelectedIndex = 0;
////cbxBaud.SelectedIndex = 0;
//cbxGender.SelectedIndex = 0;
////cbxBaud08.SelectedIndex = 0;
//cbxWork.SelectedIndex = 0;
cboxBaud.SelectedIndex = 2;
//radiobtnAscii.Checked = true;
IniValue2Text("LA", txtLAIm);
IniValue2Text("RA", txtRAIm);
IniValue2Text("TT", txtTTIm);
IniValue2Text("LL", txtLLIm);
IniValue2Text("RL", txtRLIm);
IniValue2Text("LAP", txtLAP);
IniValue2Text("RAP", txtRAP);
IniValue2Text("TTP", txtTTP);
IniValue2Text("LLP", txtLLP);
IniValue2Text("RLP", txtRLP);
IniValue2Combox("com", cbxCom);
IniValue2Combox("baud", cboxBaud);
}
private void IniValue2Combox(string key, ComboBox cbx)
{
try
{
cbx.Text = INI.GetValue(key);
}
catch (Exception ex)
{
Loger.send(ex.ToString());
}
}
private void IniValue2Text(string key, TextBox txtb)
{
try
{
txtb.Text = INI.GetValue(key);
}
catch (Exception ex)
{
Loger.send(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (m_serialPort != null)
{
m_serialPort.DataReceived -= new SerialDataReceivedEventHandler(m_serialPort_DataReceived);
m_serialPort.Close();
}
int baudRate = 0;
try
{
baudRate = Int32.Parse(cboxBaud.Text);
m_comPort = cbxCom.Text;
}
catch (Exception ex)
{
Loger.send(ex.ToString());
return;
}
m_serialPort = new SerialPort(m_comPort, baudRate, Parity.None, 8, StopBits.One);
m_serialPort.Open();
m_serialPort.DataReceived+=new SerialDataReceivedEventHandler(m_serialPort_DataReceived);
if (m_serialPort.IsOpen)
{
Loger.send("打开串口成功");
}
else
Loger.send("打开串口失败");
}
catch(Exception ex)
{
Loger.send(ex.ToString());
}
}
private void m_serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
Thread.Sleep(300);
int nReviceBytesNum = m_serialPort.BytesToRead; ///收到的字节数。
byte[] ReadBuf = new byte[nReviceBytesNum]; ///定义接收字节数组
m_serialPort.Read(ReadBuf, 0, nReviceBytesNum); ///接收数据
foreach (byte temp in ReadBuf) m_recvBytes.Add(temp);
if (m_recvBytes.Count <= 0) return;
if (m_recvBytes.Count > 500) m_recvBytes.Clear();
List<int> indexList = new List<int>();
//查找0xaa
int index = -1;
foreach (byte item in m_recvBytes)
{
index++;
if (item == (byte)0x02/* && (m_recvBytes[index + 1] == 0x53 || m_recvBytes[index + 1] == 0x43)*/)
{
indexList.Add(index);
}
}
foreach (int item in indexList)
{
try
{
dealWithData(item);
}
catch (Exception ex)
{
Loger.send(ex.ToString());
}
}
m_recvBytes.Clear();
}
catch (Exception ex)
{
Loger.recv(ex.Message);
}
}
private float toFloat(string str)
{
uint num = uint.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatVals = BitConverter.GetBytes(num);
float f = BitConverter.ToSingle(floatVals, 0);
return f;
}
//交换字符串
private string exchange(string temp)
{
string value = "";
char[] cs = temp.ToCharArray();
//交换
char cTemp;
cTemp = cs[0];
cs[0] = cs[6];
cs[6] = cTemp;
cTemp = cs[1];
cs[1] = cs[7];
cs[7] = cTemp;
cTemp = cs[2];
cs[2] = cs[4];
cs[4] = cTemp;
cTemp = cs[3];
cs[3] = cs[5];
cs[5] = cTemp;
value = new string(cs);
return value;
}
private void dealWithData(int index1)
{
int length = ((m_recvBytes[index1 + 3] - 10) << 6) + (m_recvBytes[index1 + 2] - 10);
if (length > m_recvBytes.Count - 6)
{
return;
}
byte[] bytes = new byte[length+6];
Array.Copy(m_recvBytes.ToArray(), index1, bytes,0, length+6);
//输出到日志
string logMsg = String.Empty;
// Decrypt message
//codec(bytes, 0);
// log decrypted message
string msg = string.Empty ;
msg = logBytes(bytes);
msg += "==>" + System.Text.Encoding.Default.GetString(bytes);
index1 = 0;
//lcn
//if (bytes[4] == 0x4c && bytes[ 5] == 0x43 && bytes[ 6] == 0x4E)
//{
// int a = 5;
//}
if(m_forDevelopment)Loger.recv(msg);
//lcx 02 d4 0e 0a 4c 43 58 1b 32 03
if (bytes[index1 + 4] == 0x4c && bytes[index1 + 5] == 0x43 && bytes[index1 + 6] == 0x58)
{
switch (m_calibariton_type)
{
case 0:
btnCalibration0.TextColor = Color.Green;
Loger.recv("体重校准0kg完成");
break;
case 25:
btnCalibration0.TextColor = Color.Green;
Loger.recv("体重校准25kg完成");
break;
case 50:
btnCalibration0.TextColor = Color.Green;
Loger.recv("体重校准50kg完成");
break;
case 75:
btnCalibration0.TextColor = Color.Green;
Loger.recv("体重校准75kg完成");
break;
case 100:
btnCalibration0.TextColor = Color.Green;
Loger.recv("体重校准100kg完成");
break;
default:
break;
}
}
//EW 45 57
if (bytes[index1 + 4] == 0x45 && bytes[index1 + 5] == 0x57)
{
if (!m_forDevelopment)
Loger.recv("校准: " + string.Format("{0}", m_counter++));
}
//节段检测 sp m_counter 02 43 0c 0a 53 50 46 03
if (bytes[index1 + 4] == 0x53 && bytes[index1 + 5] == 0x50)
{
if (!m_forDevelopment)
Loger.recv("阻抗: " + string.Format("{0}", m_counter++));
}
//体重的读取
if (bytes[index1 + 4] == 0x4c && bytes[index1 + 5] == 0x43 && bytes[index1 + 6] == 0x4c)
{
byte[] bytes1 = new byte[8];
Array.Copy(bytes, index1 + 8, bytes1, 0, 8);
string weightStr = System.Text.Encoding.Default.GetString(bytes1);
float weightf = toFloat(exchange(weightStr));
this.Invoke(new EventHandler(delegate
{
btnWeight.TextColor = Color.Green;
Loger.recv("体重:" + weightf.ToString());
textWeight.Text = weightf.ToString();
}));
}
//位相角的读取
if (bytes[index1 + 4] == 0x56 && bytes[index1 + 5] == 0x48)
{
byte[] bytes1 = new byte[9];
Array.Copy(bytes, index1 + 6, bytes1, 0, 8);
string Str = System.Text.Encoding.Default.GetString(bytes1);
string phaseStr = exchange(Str);
float phasef = toFloat(phaseStr);
this.Invoke(new EventHandler(delegate
{
//btnWeight.TextColor = Color.Green;
Loger.recv("位相角:" + phasef.ToString());
PHA_TT.Text = phasef.ToString();
}));
}
if (bytes[index1 + 4] == 0x56 && bytes[index1 + 5] == 0x49)
{
byte[] bytes1 = new byte[9];
Array.Copy(bytes, index1 + 6, bytes1, 0, 8);
string Str = System.Text.Encoding.Default.GetString(bytes1);
string phaseStr = exchange(Str);
float phasef = toFloat(phaseStr);
this.Invoke(new EventHandler(delegate
{
//btnWeight.TextColor = Color.Green;
Loger.recv("位相角:" + phasef.ToString());
PHA_TT.Text = phasef.ToString();
}));
}
//测量完成M1 02 43 0c 0a 4d 31 21 03
if (bytes[index1 + 4] == 0x4d && bytes[index1 + 5] == 0x31)
{
Loger.recv("M1阻抗检测完成");
btnM1.TextColor = Color.Green;
clearImpedance();
send(new byte[] { 0x02, 0x53, 0x13, 0x0a, 0x43, 0x43, 0x30, 0x31, 0x31, 0x31, 0x31, 0x30, 0x1b, 0x3f, 0x03 }); // CC
}
//测量完成M2 02 43 0c 0a 4d 32 21 03
if (bytes[index1 + 4] == 0x4d && bytes[index1 + 5] == 0x32)
{
Loger.recv("M2 impedance m complate");
send(new byte[] { 0x02, 0x53, 0x13, 0x0a, 0x43, 0x43, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x1b, 0x3d, 0x03 }); // CC1
}
//cdr0 02 c0 13 0a 43 44 52 30 31 31 31 30 1b 21 03
if (bytes[index1 + 4] == 0x43 && bytes[index1 + 5] == 0x44 && bytes[index1 + 6] == 0x52 && bytes[index1 + 7] == 0x30)
{
Loger.recv("档位3校准完成");
btnCDR0.TextColor = Color.Green;
}
//cdr0 02 c0 13 0a 43 44 52 30 31 31 31 30 1b 21 03
if (bytes[index1 + 4] == 0x43 && bytes[index1 + 5] == 0x44 && bytes[index1 + 6] == 0x52 && bytes[index1 + 7] == 0x31)
{
Loger.recv("档位2校准完成");
btnCDR1.TextColor = Color.Green;
}
//cdr0 02 c0 13 0a 43 44 52 30 31 31 31 30 1b 21 03
if (bytes[index1 + 4] == 0x43 && bytes[index1 + 5] == 0x44 && bytes[index1 + 6] == 0x52 && bytes[index1 + 7] == 0x32)
{
Loger.recv("档位1校准完成");
btnCDR2.TextColor = Color.Green;
}
//读取阻抗
if (bytes[index1 + 4] == 0x43 && bytes[index1 + 5] == 0x43) //CC command
{
int n = 2;
if (m_ReadStep == 1 || m_ReadStep == 3)
{
Loger.recv("M1 阻抗读取成功");
n = 4;
}
else if(m_ReadStep == 2)
{
Loger.recv("M2 阻抗读取成功");
n = 2;
}
float[] res=new float[n * 5];
byte[]bytes1 = new byte[n * 5 * 9];
Array.Copy(bytes, index1 + 6, bytes1, 0, n * 5 * 9);
for (int i = 0; i < n * 5; i++)
{
byte[] zukang = new byte[8];
Array.Copy(bytes1, i * 9, zukang, 0, 8);
string str = System.Text.Encoding.Default.GetString(zukang);
string impedenceStr = exchange(str);
float impedencef = toFloat(impedenceStr);
res[i] = impedencef;
}
//阻抗数据进行交换
for (int i = 0; i < n; i++)
{
float temp = res[i * 5];
temp = res[i * 5];
res[i * 5] = res[i * 5+4];
res[i * 5 + 4] = temp;
}
//阻抗数据进行交换
for (int i = 0; i < n; i++)
{
float temp = res[i * 5+3];
res[i * 5+3] = res[i * 5 + 4];
res[i * 5 + 4] = temp;
}
//阻抗数据进行交换
for (int i = 0; i < n; i++)
{
float temp = res[i * 5];
temp = res[i * 5];
res[i * 5] = res[i * 5 + 1];
res[i * 5 + 1] = temp;
}
//输出阻抗到显示
String impedanceStr = DateTime.Now.ToString()+ "-------------------------------\r\n";
int j=0;
foreach (float item in res)
{
if (j % 5 == 0)
{
String zukangStr = String.Format("{0}, {1}, {2} , {3}, {4}\r\n", res[j + 0], res[j + 1], res[j + 2], res[j + 3], res[j + 4]);
impedanceStr += zukangStr;
}
j++;
}
if (m_ReadStep == 1 || m_ReadStep == 3)
{
this.Invoke(new EventHandler(delegate
{
//richboxImpedance.Text += impedanceStr;
//if (richboxImpedance.Text.Length > 5000) richboxImpedance.Clear();
//richboxImpedance.SelectionStart = richboxImpedance.Text.Length;
//richboxImpedance.ScrollToCaret();
labelRA5.Text = res[0].ToString();
labelRA50.Text = res[5].ToString();
labelRA250.Text = res[10].ToString();
labelRA500.Text = res[15].ToString();
labelLA5.Text = res[1].ToString();
labelLA50.Text = res[6].ToString();
labelLA250.Text = res[11].ToString();
labelLA500.Text = res[16].ToString();
labelTT5.Text = res[2].ToString();
labelTT50.Text = res[7].ToString();
labelTT250.Text = res[12].ToString();
labelTT500.Text = res[17].ToString();
labelRL5.Text = res[3].ToString();
labelRL50.Text = res[8].ToString();
labelRL250.Text = res[13].ToString();
labelRL500.Text = res[18].ToString();
labelLL5.Text = res[4].ToString();
labelLL50.Text = res[9].ToString();
labelLL250.Text = res[14].ToString();
labelLL500.Text = res[19].ToString();
//PHA_LA.Text = res[20].ToString();
//PHA_RA.Text = res[21].ToString();
//PHA_TT.Text = res[22].ToString();
//PHA_LL.Text = res[23].ToString();
//PHA_RL.Text = res[24].ToString();
}));
if(m_ReadStep == 1)
// send M2
send(new byte[] { 0x02, 0x53, 0x13, 0x0a, 0x4d, 0x32, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x1b, 0x36, 0x03 }); // M2
else
send(new byte[] { 0x02, 0x53, 0x13, 0x0a, 0x43, 0x43, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x1b, 0x3d, 0x03 }); // CC1
m_ReadStep = 2;
}
else if (m_ReadStep == 2)
{
this.Invoke(new EventHandler(delegate
{
btnReadImpedence.TextColor = Color.Green;
btnM1.TextColor = Color.Green;
//richboxImpedance.Text += impedanceStr;
//if (richboxImpedance.Text.Length > 5000) richboxImpedance.Clear();
//richboxImpedance.SelectionStart = richboxImpedance.Text.Length;
//richboxImpedance.ScrollToCaret();
labelRA1K.Text = res[0].ToString();
labelRA1M.Text = res[5].ToString();
labelLA1K.Text = res[1].ToString();
labelLA1M.Text = res[6].ToString();
labelTT1K.Text = res[2].ToString();
labelTT1M.Text = res[7].ToString();
labelRL1K.Text = res[3].ToString();
labelRL1M.Text = res[8].ToString();
labelLL1K.Text = res[4].ToString();
labelLL1M.Text = res[9].ToString();
}));
m_ReadStep = 0;
}
}
}
private byte getrandom(byte seed)
{
return (byte)(((((UInt32)seed * 31421 + 6927)) & 0x7F) + 0x80);
}
private void codec(byte[] recvBytes, int index = 0)
{
byte key = recvBytes[index + 1];
int length = ((recvBytes[index + 3] - 10) << 6) + (recvBytes[index + 2] - 10);
for (int pos = 0; pos < length; pos++)
{
key = getrandom(key);
byte codecbyte = (byte)(recvBytes[index + pos + 4] ^ key);
recvBytes[index + pos + 4] = codecbyte;
}
}
private void funCode4(List<byte> recvBytes, int index)
{
Back4 back = new Back4();
if (m_recvBytes[index + back.Length + 3] == getCRC(recvBytes.ToArray(), index, back.Length + 3))
{
//判断不同的关键词,调用不同类的处理方法
byte[] datas = new byte[back.Length];
Array.Copy(m_recvBytes.ToArray(), index + 3, datas, 0, back.Length);
//根据不同的功能码,获取不同的返回数据
back.DataBytes = datas;
string msg = string.Format("{0}, 频段号,{1},体重,{2},阻抗1,{3},阻抗2,{4},阻抗3,{5},阻抗4,{6},阻抗5,{7},阻抗6,{8}",
DateTime.Now.ToString("HH:mm:ss.fff"), back.频段号, back.体重, back.阻抗1, back.阻抗2, back.阻抗3, back.阻抗4, back.阻抗5, back.阻抗6);
logFile(msg);
Loger.recv(string.Format("接受:{0}", msg));
this.Invoke(new DelegatBack(backRecieved), new object[] { back });
m_recvBytes.RemoveRange(0, index + back.Length + 4);
}
//删除前21个字节
m_recvBytes.RemoveRange(0, index + 1);
}
private void funCode2(List<byte> recvBytes, int index)
{
Back2 back = new Back2();
if (m_recvBytes[index + back.Length + 3] == getCRC(recvBytes.ToArray(), index, back.Length + 3))
{
//判断不同的关键词,调用不同类的处理方法
byte[] datas = new byte[back.Length];
Array.Copy(m_recvBytes.ToArray(), index + 3, datas, 0, back.Length);
//根据不同的功能码,获取不同的返回数据
back.DataBytes = datas;
string msg = string.Format("{0}", back.codeId);
logFile(DateTime.Now.ToString("HH:mm:ss.fff")+": "+ msg);
Loger.recv(string.Format("接受:{0}", msg));
//删除前n个字节
m_recvBytes.RemoveRange(0, index + back.Length + 4);
}
else
m_recvBytes.RemoveRange(0, index + 1);
}
private void funCode5(List<byte> recvBytes, int index)
{
Back5 back = new Back5();
if (m_recvBytes[index + back.Length + 3] == getCRC(recvBytes.ToArray(), index, back.Length + 3))
{
//判断不同的关键词,调用不同类的处理方法
byte[] datas = new byte[back.Length];
Array.Copy(m_recvBytes.ToArray(), index + 3, datas, 0, back.Length);
//根据不同的功能码,获取不同的返回数据
back.DataBytes = datas;
string msg = string.Format("{0}: 体重系数:K={1},B={2}; 通道1:K={3},B={4}; ; 通道2:K={5},B={6}; 通道3:K={7},B={8}; 通道4:K={9},B={10}; 通道5:K={11},B={12}; 通道6:K={13},B={14};",
DateTime.Now.ToString("HH:mm:ss.fff"),
back.WeightRatio.K, back.WeightRatio.B,
back.Resistance1Ratio.K,back.Resistance1Ratio.B,
back.Resistance2Ratio.K,back.Resistance2Ratio.B,
back.Resistance3Ratio.K,back.Resistance3Ratio.B,
back.Resistance4Ratio.K,back.Resistance4Ratio.B,
back.Resistance5Ratio.K,back.Resistance5Ratio.B,
back.Resistance6Ratio.K,back.Resistance6Ratio.B);
logFile(msg);
Loger.recv(string.Format("接受:{0}", msg));
this.Invoke(new DelegatBack(back5Recieved), new object[] { back });
m_recvBytes.RemoveRange(0, index + back.Length + 4);
}
else
m_recvBytes.RemoveRange(0, index + 1);
}
private void back5Recieved(Back back)
{
//Back5 back5 = back as Back5;
//txt5WeightK.Text = back5.WeightRatio.K.ToString();
//txt5WeightB.Text = back5.WeightRatio.B.ToString();
//txt5Resist1K.Text = back5.Resistance1Ratio.K.ToString();
//txt5Resist1B.Text = back5.Resistance1Ratio.B.ToString();
//txt5Resist2K.Text = back5.Resistance2Ratio.K.ToString();
//txt5Resist2B.Text = back5.Resistance2Ratio.B.ToString();
//txt5Resist3K.Text = back5.Resistance3Ratio.K.ToString();
//txt5Resist3B.Text = back5.Resistance3Ratio.B.ToString();
//txt5Resist4K.Text = back5.Resistance4Ratio.K.ToString();
//txt5Resist4B.Text = back5.Resistance4Ratio.B.ToString();
//txt5Resist5K.Text = back5.Resistance5Ratio.K.ToString();
//txt5Resist5B.Text = back5.Resistance5Ratio.B.ToString();
//txt5Resist6K.Text = back5.Resistance6Ratio.K.ToString();
//txt5Resist6B.Text = back5.Resistance6Ratio.B.ToString();
}
private void funCode8(List<byte> recvBytes, int index)
{
Back8_2 back = new Back8_2();
if (m_recvBytes[index + back.Length + 3] == getCRC(recvBytes.ToArray(), index, back.Length + 3))
{
byte[] datas = new byte[back.Length];
Array.Copy(m_recvBytes.ToArray(), index + 3, datas, 0, back.Length);
//根据不同的功能码,获取不同的返回数据
back.DataBytes = datas;
string msg = string.Format("{0}: 频段号,{1},体重,{2},阻抗1,{3},阻抗2,{4}",
DateTime.Now.ToString("HH:mm:ss.fff"), back.频段号, back.体重, back.阻抗1, back.阻抗2);
logFile(msg);
Loger.recv(string.Format("接受:{0}", msg));
this.Invoke(new DelegatBack(back8Recieved), new object[] { back });
//删除前n个字节
m_recvBytes.RemoveRange(0, index + back.Length + 4);
}
else
//删除前n个字节
m_recvBytes.RemoveRange(0, index + 1);
}
private void funCode8_3(List<byte> recvBytes, int index)
{
Back8_3 back = new Back8_3();
if (true)
{
byte[] datas = new byte[back.Length];
Array.Copy(m_recvBytes.ToArray(), index + 3, datas, 0, back.Length);
//根据不同的功能码,获取不同的返回数据
back.DataBytes = datas;
//if (back.通道类型 == 2)
{
string msg = string.Format("{0}: 频段号,{1},体重,{2},阻抗1,{3},阻抗2,{4},阻抗3,{5},阻抗4,{6},阻抗5,{7}",
DateTime.Now.ToString("HH:mm:ss.fff"),
back.频段号, back.体重, back.阻抗1, back.阻抗2,
back.阻抗3, back.阻抗4, back.阻抗5);
logFile(msg);
Loger.recv(string.Format("接受:{0}", msg));
this.Invoke(new DelegatBack(back8Recieved), new object[] { back });
}
//删除前n个字节
m_recvBytes.RemoveRange(0, index + back.Length + 3);
}
else
//删除前n个字节
m_recvBytes.RemoveRange(0, index + 1);
}
//
private void funCodeA(List<byte> recvBytes, int index)
{
BackA back = new BackA();
if (m_recvBytes[index + back.Length + 3] == getCRC(recvBytes.ToArray(), index, back.Length + 3))
{
byte[] datas = new byte[back.Length];
Array.Copy(m_recvBytes.ToArray(), index + 3, datas, 0, back.Length);
//根据不同的功能码,获取不同的返回数据
back.DataBytes = datas;
try
{
string msg = string.Format("{0},体重K,{1},体重B,{2},5khz1K,{3},5khz1B,{4},5khz2K,{5},5khz2B,{6},250khz1K,{7},250khz1B,{8},250khz2K,{9},250khz2B,{10}",
DateTime.Now.ToString("HH:mm:ss.fff"),
back.体重K, back.体重B,
back.阻抗5K1, back.阻抗5B1,
back.阻抗5K2, back.阻抗5B2,
back.阻抗250K1, back.阻抗250B1,
back.阻抗250K2,back.阻抗250B2
);
logFile(msg);
Loger.recv(string.Format("接受:{0}", msg));
this.Invoke(new DelegatBack(backARecieved), new object[] { back });
}
catch
{
}
//删除前n个字节
m_recvBytes.RemoveRange(0, index + back.Length + 4);
}
else
//删除前n个字节
m_recvBytes.RemoveRange(0, index + 1);
}
private void backARecieved(Back back)
{
//BackA backa = back as BackA;
//txt体重k.Text = backa.体重K.ToString();
//txt体重b.Text = backa.体重B.ToString();
//txt5k1.Text = backa.阻抗5K1.ToString();
//txt5b1.Text = backa.阻抗5B1.ToString();
////txt5b2
//txt5k2.Text = backa.阻抗5K2.ToString();
//txt5b2.Text = backa.阻抗5B2.ToString();
//txt250k1.Text = backa.阻抗250K1.ToString();
//txt250b1.Text = backa.阻抗250B1.ToString();
//txt250k2.Text = backa.阻抗250K2.ToString();
//txt250b2.Text = backa.阻抗250B2.ToString();
}
private void back8Recieved(Back back)
{
Back8_3 back8 = back as Back8_3;
//txt8BaudId.Text = back8.频段号.ToString();
//txt8Weight.Text = back8.体重.ToString();
//txtWeight.Text = back8.体重.ToString();
//if (back8.频段号 == 7)
//{
// txt250ra.Text = back8.阻抗1.ToString();
// txt250la.Text = back8.阻抗2.ToString();
// txt250tr.Text = back8.阻抗3.ToString();
// txt250rl.Text = back8.阻抗4.ToString();
// txt250ll.Text = back8.阻抗5.ToString();
// caclPBF();
//}
//else if(back8.频段号 == 4)
//{
// txt50ra.Text = back8.阻抗1.ToString();
// txt50la.Text = back8.阻抗2.ToString();
// txt50tr.Text = back8.阻抗3.ToString();
// txt50rl.Text = back8.阻抗4.ToString();
// txt50ll.Text = back8.阻抗5.ToString();
//}
}
private void logFile(string msg)
{
sw.WriteLine(msg);
sw.Flush();
}
private void backRecieved(Back back)