-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXTREM.cs
1222 lines (943 loc) · 36.4 KB
/
XTREM.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.Globalization;
using System.IO.Ports;
using System.Timers;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Timer = System.Timers.Timer;
using System.Windows.Threading;
namespace PC_XTREM
{
public class Xtrem : INotifyPropertyChanged
{
//events
public event EventHandler WeightChanged;
public event EventHandler Recallscaledef;
//public event EventHandler NameChanged;
public event EventHandler NewStableWeight;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
public event EventHandler ConnectProgress;
private const string StartSendingCommand = "\u000200FFE10110000\u0003\r\n";
private const string AdcCtsWriteCommand = "\u000200FFR01110000\u0003\r\n";
private const string VinCtsWriteCommand = "\u000200FFR02100000\u0003\r\n";
private const string XTempWriteCommand = "\u000200FFR02020000\u0003\r\n";
private const string GetScaleInfoCommand = "\u000200FFE10100000\u0003\r\n";
private const int IdParam = 0x01;
private const int TypeParam = 0x07;
private const int SerialNumberParam = 0;
private const int ResolutionParam = 0x28;
private const int DecimalPlacesParam = 0x26;
private const int ResFactorParam = 0x28;
private const int CurMaxParam = 0x22;
private const int CurEscParam = 0x23;
private const int UnitParam = 0x20;
private const int BaudRateCodeParam = 0x10;
private const int OutputRateParam = 0x13;
private const int FirmwareVersionParam = 0x08;
private const int SealSwitchStateParam = 0x09;
private const int RangeModeParam = 0x21;
private const int NameParam = 0x500;
private const int ZeroTrackingParam = 0x50;
private const int ZeroTrackingRangeParam = 0x51;
private const int ZeroInitParam = 0x52;
private const int ZeroInitRangeParam = 0x53;
private const int TareAutoParam = 0x61;
private const int TareOnStabilityParam = 0x62;
private const int TarModeParam = 0x60;
private const int NegativeWeightParam = 0x29;
private const int FilterLevelParam = 0x70;
private const int FilterAnimalParam = 0x72;
private const int StabilityRangeParam = 0x73;
private const int WifiBoardCodeParam = 0x0A;
private const int ApPasswordParam = 0x501;
private const int ApIpAddressParam = 0x502;
private const int ApDHCPParam = 0x503;
private const int WifiApParam = 0x504;
private const int StaSsidParam = 0x600;
private const int StaPasswordParam = 0x601;
private const int StaIpAddressParam = 0x602;
private const int StaDHCPParam = 0x603;
private const int TcpServerPortParam = 0x702;
private const int UdpApRemotePortParam = 0x700;
private const int UdpApLocalPortParam = 0x701;
private const int InitZeroParam = 0x0030;
private const int SlopeFactorParam = 0x0031;
private const int MaxCountsParam = 0x0032;
private const int GeoLocalParam = 0x0041;
private const int GeoAdjustParam = 0x0042;
private const int VinMinParam = 0x0002;
private const int VinMaxParam = 0x0003;
private const int VoutMinParam = 0x0004;
private const int VoutMaxParam = 0x0005;
private const int BullModeParam = 0x0015;
//UDP comms
public bool Udp = false;
public UdpClient Listener;
public Thread ReadUdpData;
public bool StopThread = false;
private IPEndPoint udpSendEndpoint;
public IPEndPoint UdpSendEndpoint { get => udpSendEndpoint; set => udpSendEndpoint = value; }
public int UdpRecPort;
public bool IsWaitingData = true;
//end UDP comms
//communications error
public Timer aTimer;
private bool isNotConnected;
private static bool ConnectState;
public string rx_buffer = "";
private static bool scale_info = false;
private int id = 0xff;
//Scale definition
private int curEsc;
private int decimalPlaces;
public int ResFactor;
//adjust information
private string vInput;
private long adcCts;
private string maxCounts;
//weighing information
private double w_Brut = 0;
private double w_Tare = 0;
private double w_Net = 0;
private string w_Display = "";
private string w_Unit = "";
private bool w_Flag_Zero = false;
private bool w_Flag_Tare = false;
private bool w_Flag_Stability = false;
private DispatcherTimer StabilityTimer;
private TimeSpan StabilityElapsedTime;
private double stabilityTime;
private bool w_Flag_NetoDisp = false;
private bool w_Flag_HighRes = false;
public static bool holdWeightChange;
public double W_Hold;
public bool HoldMode = false;
private static readonly string[] Unit = { "", "g ", "kg", "oz", "lb" };
public double W_Net
{
get => w_Net;
set
{
w_Net = value;
OnPropertyChanged("W_Net");
}
}
public double W_Tare
{
get => w_Tare;
set
{
w_Tare = value;
OnPropertyChanged("W_Tare");
}
}
public double W_Brut
{
get => w_Brut;
set
{
w_Brut = value;
OnPropertyChanged("W_Brut");
}
}
public string VInput
{
get => vInput;
set
{
vInput = value;
OnPropertyChanged("VInput");
}
}
public long AdcCts
{
get => adcCts;
set
{
adcCts = value;
OnPropertyChanged("AdcCts");
}
}
public string W_Display
{
get => w_Display;
set
{
w_Display = value;
OnPropertyChanged("W_Display");
}
}
public bool IsNotConnected
{
get => isNotConnected;
set
{
isNotConnected = value;
OnPropertyChanged("IsNotConnected");
}
}
private string vinCts;
private string vinVolt;
public string VinCts
{
get => vinCts;
set
{
vinCts = value;
OnPropertyChanged("VinCts");
}
}
public string VinVolt
{
get => vinVolt;
set
{
vinVolt = value;
OnPropertyChanged("VinVolt");
}
}
protected virtual void OnWeightChanged()
{
WeightChanged?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnRecallscaledef()
{
Recallscaledef?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnNewStableWeight()
{
NewStableWeight?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnConnectProgress()
{
ConnectProgress?.Invoke(this, EventArgs.Empty);
}
private async void Listen_UDP_Async()
{
UdpReceiveResult rec;
while (!StopThread)
{
try
{
rec = await Listener.ReceiveAsync();
rx_buffer = System.Text.Encoding.ASCII.GetString(rec.Buffer);
//Console.WriteLine(udpSendEndpoint.ToString() + " " + rx_buffer);
if (IsWaitingData == false)
{
ParseWeightStream(rx_buffer.Substring(1, rx_buffer.Length - 4));
}
//Thread.Sleep(5);
}
catch (Exception e)
{
Console.WriteLine(e);
//Console.WriteLine("Error en Listen_UDP_Async()");
}
}
//Console.WriteLine("Stop thread ReadUdpData");
}
public void Init_Udp_Comms()
{
//UDP communication
//byte[] _ip = udpSendEndpoint.Address.GetAddressBytes();
IPEndPoint LocalIP = new IPEndPoint(address: IPAddress.Any, port: UdpRecPort);
Listener = new UdpClient()
{
EnableBroadcast = false,
ExclusiveAddressUse = false,
};
Listener.Client.Blocking = false;
Listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Listener.Client.Bind(LocalIP);
Listener.Client.Connect(udpSendEndpoint);
ReadUdpData = new Thread(Listen_UDP_Async);
StopThread = false;
ReadUdpData.IsBackground = true;
ReadUdpData.Start();
}
public void Init_Scale()
{
ConnectState = false;
IsWaitingData = false;
//QR encoder
//Encoder.ErrorCorrectionLevel = ErrorCorrectionLevel.H;
//Start sending
SendCommand(StartSendingCommand);
// Create a timer and set a two second interval.
aTimer = new Timer
{
Interval = 1000
};
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset = true;
// Start the timer
aTimer.Enabled = true;
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
//Timer for stability elapsed time
StabilityTimer = new DispatcherTimer()
{
Interval = TimeSpan.FromMilliseconds(50),
};
StabilityElapsedTime = TimeSpan.Zero;
StabilityTimer.Tick += StabilityTimer_Tick;
}
private void StabilityTimer_Tick(object sender, EventArgs e)
{
StabilityElapsedTime += StabilityTimer.Interval;
//throw new NotImplementedException();
}
public void GetScaleDef()
{
string _get;
int unit = 0;
int _rangemode;
int datacount = 0;
if (aTimer != null)
{
aTimer.Enabled = false;
}
scale_info = true;
SendCommand(GetScaleInfoCommand);
OnConnectProgress();
Thread.Sleep(1);
_get = Get_Param(IdParam, 5);
if (_get != "-")
{
if (int.TryParse(_get, NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out int result))
{
id = result;
datacount++;
}
}
OnConnectProgress();
Thread.Sleep(1);
_get = Get_Param(ResolutionParam, 5);
if (_get != "-")
{
datacount++;
if (_get == "10")
{
w_Flag_HighRes = true;
}
else
{
w_Flag_HighRes = false;
}
}
OnConnectProgress();
Thread.Sleep(1);
_get = Get_Param(DecimalPlacesParam, 5);
if (_get != "-")
{
datacount++;
decimalPlaces = Convert.ToInt32(_get);
if (w_Flag_HighRes) decimalPlaces--;
}
OnConnectProgress();
Thread.Sleep(1);
_get = Get_Param(ResFactorParam, 5);
if (_get != "-")
{
datacount++;
ResFactor = Convert.ToInt32(_get);
}
OnConnectProgress();
Thread.Sleep(1);
int dec = decimalPlaces - (int)ResFactor / 10;
_get = Get_Param(CurEscParam, 5);
if (_get != "-")
{
datacount++;
curEsc = Convert.ToInt32(_get);
}
OnConnectProgress();
Thread.Sleep(1);
_get = Get_Param(UnitParam, 5);
if (_get != "-")
{
datacount++;
unit = Convert.ToInt32(_get);
}
OnConnectProgress();
Thread.Sleep(1);
w_Unit = Unit[unit];
double Esc = ((double)curEsc / Math.Pow(10, dec));
double Min = (20 * Esc);
_get = Get_Param(MaxCountsParam, 5);
if (_get != "-")
{
datacount++;
maxCounts = _get;
}
OnConnectProgress();
Thread.Sleep(1);
scale_info = true;
isNotConnected = false;
SendCommand(StartSendingCommand);
if (aTimer != null)
{
aTimer.Enabled = true;
}
}
public string Get_Param(int param, int cops)
{
string resposta_esperada;
string data = "-";
int l;
int n_send = 0;
ConnectState = false;
//prepara comando lectura a XTREM
string command = string.Format("\u000200{0:X2}R{1:X4}0000\u0003\r\n", 0xff, param);
//respuesta esperada
resposta_esperada = string.Format("\u0002..00r{0:X4}..*..\u0003", param);
string datarec = "";
//DataReceived recv = new DataReceived();
int _cops = cops * 2;
byte[] Udp_Send = System.Text.Encoding.UTF8.GetBytes(command);
IsWaitingData = true;
while (data == "-")
{
//rx_buffer = "";
Listener.Send(Udp_Send, Udp_Send.Length, UdpSendEndpoint);
bool stop = false;
decimal milliseconds = DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond;
decimal currentms = 0;
decimal timeout = 200;
while (!stop)
{
if (rx_buffer.Length > 0)
{
if (Regex.IsMatch(rx_buffer, resposta_esperada) == true)
{
datarec = rx_buffer;
stop = true;
}
}
currentms = DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond;
if ((currentms - milliseconds) > timeout)
{
stop = true;
}
}
//Console.WriteLine("Stops before timeout ends " + (currentms - milliseconds) + "," + n_send);
if (datarec.Length > 0)
{
int first = Regex.Match(datarec, resposta_esperada).Index;
int len = Regex.Match(datarec, resposta_esperada).Length;
if (first > -1)
{
if (len > 11)
{
l = Convert.ToInt32(datarec.Substring(first + 10, 2), 16);
if (len >= 14 + l)
{
data = datarec.Substring(first + 12, l);
}
}
}
//rx_buffer = "";
//ConnectState = true;
}
if (data == "-")
{
//Console.WriteLine("Get_Param(" + string.Format("0x{0:X4}", param) + ") error in " + udpSendEndpoint.Address + ":" + udpSendEndpoint.Port);
}
//Console.Write(string.Format("{1} {0:X4}", param, n_send) + " | " + string.Format("{0:X4}", recv.Address) + ":" + recv.Data + " | " + rx_buffer);
n_send++;
if (n_send > _cops)
{
break;
}
}
IsWaitingData = false;
return data;
}
public int WriteParam(int param, string value)
{
string data = "";
IsWaitingData = true;
try
{
//Send write command
string command = string.Format("\u000200{0:X2}W{1:X4}{2:X2}{3}00\u0003\r\n", id, param, value.Length, value);
byte[] Udp_Send = System.Text.ASCIIEncoding.UTF8.GetBytes(command);
DataReceived recv = new DataReceived();
IPEndPoint local = new IPEndPoint(0, 0);
int n_send = 0;
int _cops = 5;
if (param == 0x500 || param == 0x501 || param == 0x502 || param == 0x700 || param == 0x701 || param == 0x702)
{
_cops = 20;
}
while (data.Length == 0)
{
rx_buffer = "";
Listener.Send(Udp_Send, Udp_Send.Length, UdpSendEndpoint);
bool stop = false;
decimal milliseconds = DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond;
decimal currentms = 0;
decimal timeout = 200;
while (!stop)
{
if (rx_buffer.Length > 0)
{
recv = ParseDataReceived(rx_buffer);
if (recv.Address == param)
{
stop = true;
}
}
currentms = DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond;
if ((currentms - milliseconds) > timeout)
{
stop = true;
}
}
//Console.WriteLine("Stops before timeout ends " + (currentms - milliseconds) + "," + n_send);
if (recv.Address == param)
{
data = recv.Data;
}
n_send++;
if (n_send > _cops)
{
break;
}
}
isNotConnected = false;
IsWaitingData = false;
if (data == "0")
{
return 0;
}
else
{
return -1;
}
}
catch (Exception ex)
{
isNotConnected = false;
IsWaitingData = false;
Console.WriteLine(ex.Message);
//Console.WriteLine("Error en WriteParam() UDP");
return 1;
}
}
public void SendCommand(string command)
{
IsWaitingData = true;
try
{
byte[] Udp_Send;
string data = "";
int param = int.Parse(command.Substring(6, 4), NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo);
//Send command
Udp_Send = System.Text.ASCIIEncoding.UTF8.GetBytes(command);
Listener.Send(Udp_Send, Udp_Send.Length, UdpSendEndpoint);
DataReceived recv = new DataReceived();
IPEndPoint local = new IPEndPoint(0, 0);
int n_send = 0;
int _cops = 5;
while (data.Length == 0)
{
Listener.Send(Udp_Send, Udp_Send.Length, UdpSendEndpoint);
bool stop = false;
decimal milliseconds = DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond;
decimal currentms = 0;
decimal timeout = 200;
while (!stop)
{
if (rx_buffer.Length > 0)
{
recv = ParseDataReceived(rx_buffer);
if (recv.Address == param)
{
stop = true;
}
}
currentms = DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond;
if ((currentms - milliseconds) > timeout)
{
stop = true;
}
}
//onsole.WriteLine("Stops before timeout ends " + (currentms - milliseconds) + "," + n_send);
if (recv.Address == param)
{
data = recv.Data;
isNotConnected = false;
}
n_send++;
if (n_send > _cops)
{
break;
}
}
IsWaitingData = false;
}
catch (Exception ex)
{
isNotConnected = true;
IsWaitingData = false;
Console.WriteLine(ex.Message);
//Console.WriteLine("Error en SendCommand() UDP");
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
//Console.WriteLine("Timeout elapsed at {0}", e.SignalTime);
if (ConnectState == false)
{
if (Udp == true && udpSendEndpoint.Address != null)
{
if (ReadUdpData.IsAlive == false)
{
ReadUdpData = new Thread(Listen_UDP_Async);
StopThread = false;
ReadUdpData.IsBackground = true;
ReadUdpData.Start();
}
string check = Get_Param(0x100, 2);
if (check != "-")
{
IsNotConnected = false;
if (scale_info == false)
{
OnRecallscaledef();
}
SendCommand(StartSendingCommand);
}
else
{
IsNotConnected = true;
W_Display = "";
}
}
}
else
{
if (IsNotConnected)
{
OnRecallscaledef();
}
IsNotConnected = false;
}
ConnectState = false;
}
public struct DataReceived
{
public int Address;
public string Data;
}
public DataReceived ParseDataReceived(string data_received)
{
DataReceived result = new DataReceived
{
Address = -1,
Data = ""
};
if (data_received.Length < 17)
{
return result;
}
//device destination
string dest_id = data_received.Substring(3, 2);
if (dest_id != "00" && dest_id != "FF")
{
//Console.WriteLine("dest_id=" + dest_id);
return result;
}
//Parameter code (Address)
string address = data_received.Substring(6, 4);
if (int.TryParse(address, NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out int ad) == true)
{
result.Address = ad;
}
//data length
if (int.TryParse(data_received.Substring(10, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out int l) == false)
{
//Console.WriteLine("Parse subs(9,2)=" + data_received.Substring(9, 2));
return result;
}
if (data_received.Length != l + 17)
{
//Console.WriteLine("data length = " + data_received.Length + " / l +13 = " + (l + 13));
return result;
}
result.Data = data_received.Substring(12, l);
return result;
}
public void ParseWeightStream(string data_received)
{
bool Flag_Change = false;
bool CurrentFlag;
string Disp_weight;
//Device Id
//string sender_id = cmd.Substring(0, 2);
//device destination
string dest_id = data_received.Substring(2, 2);
if (dest_id != "00" && dest_id != "FF")
{
//Console.WriteLine("dest_id=" + dest_id);
return;
}
//Console.WriteLine(data_received);
//Parameter code (Address)
string address = data_received.Substring(5, 4);
//data length
if (int.TryParse(data_received.Substring(9, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out int l) == false)
{
//Console.WriteLine("Parse subs(9,2)=" + data_received.Substring(9, 2));
return;
}
if (data_received.Length != l + 13)
{
//Console.WriteLine("data length = " + data_received.Length + " / l +13 = " + (l + 13));
return;
}
//data received
//string data_rec = cmd.Substring(11, l);
switch (address)
{
case "0107":
//decimal milliseconds = DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond;
//tlast = milliseconds - tlast;
//Console.WriteLine("tlast = " + tlast);
//tlast = milliseconds;
ConnectState = true;
//weighing flags
ushort Weight_status = 0;
if (ushort.TryParse(data_received.Substring(34, 3), NumberStyles.AllowHexSpecifier, NumberFormatInfo.CurrentInfo, out ushort result))
{
Weight_status = result;
}
CurrentFlag = Convert.ToBoolean(Weight_status & 1);
if (CurrentFlag != w_Flag_Zero)
{
Flag_Change |= true;
}
else
{
OnPropertyChanged("W_Flag_Zero");
}
_ = (CurrentFlag) == true
? w_Flag_Zero = true
: w_Flag_Zero = false;
CurrentFlag = Convert.ToBoolean(Weight_status & 2);
if (CurrentFlag != w_Flag_Tare)
{
Flag_Change |= true;
}
else
{
OnPropertyChanged("W_Flag_Tare");
}
_ = (CurrentFlag) == true
? w_Flag_Tare = true
: w_Flag_Tare = false;
CurrentFlag = Convert.ToBoolean(Weight_status & 8);
if (CurrentFlag != w_Flag_NetoDisp)
{
Flag_Change |= true;
}
else
{
OnPropertyChanged("W_Flag_NetoDisp");
}
_ = (CurrentFlag) == true
? w_Flag_NetoDisp = true
: w_Flag_NetoDisp = false;
CurrentFlag = Convert.ToBoolean(Weight_status & 4);
if (CurrentFlag != w_Flag_Stability)
{
Flag_Change |= true;
if (CurrentFlag == true)
{
if (StabilityTimer != null)
StabilityTimer.Start();
}
else
{
if (StabilityTimer != null)
StabilityTimer.Stop();
}
}
else
{
OnPropertyChanged("W_Flag_Stability");