diff --git a/Utils/HID/DataHelper.cs b/Utils/HID/DataHelper.cs index 9a9716a..76c1610 100644 --- a/Utils/HID/DataHelper.cs +++ b/Utils/HID/DataHelper.cs @@ -48,18 +48,27 @@ public byte[] LoadPackage(byte cmd, ushort args, byte[] dat, byte len) public int AnalyzePackage(byte[] dat) { - lenOfPackage = dat[1]; - command = dat[2]; - args = (ushort)((dat[3] << 8) | dat[4]); - errorCode = (HID_ERRORS)args; - for (var i = 0; i < lenOfPackage - 5; i++) payload[i] = dat[i + 5]; - - var num = 2 + lenOfPackage - 2; - crc = (ushort)CrcValidation(dat, 2, lenOfPackage - 2); - var num2 = (ushort)((dat[num] << 8) | dat[num + 1]); - if (crc == num2) return 1; - - return -1; + try + { + lenOfPackage = dat[1]; + command = dat[2]; + args = (ushort)((dat[3] << 8) | dat[4]); + errorCode = (HID_ERRORS)args; + for (var i = 0; i < lenOfPackage - 5; i++) payload[i] = dat[i + 5]; + + var num = 2 + lenOfPackage - 2; + crc = (ushort)CrcValidation(dat, 2, lenOfPackage - 2); + var num2 = (ushort)((dat[num] << 8) | dat[num + 1]); + if (crc == num2) return 1; + return -1; + } + catch + { + // 写频完成后手台还会发几个包,。不知道为啥 + return -1; + } + + } private int CrcValidation(byte[] dat, int offset, int count) diff --git a/Utils/HID/HIDCommunication.cs b/Utils/HID/HIDCommunication.cs index 085f00c..ef05fb6 100644 --- a/Utils/HID/HIDCommunication.cs +++ b/Utils/HID/HIDCommunication.cs @@ -108,6 +108,7 @@ public bool DoIt(CancellationToken token) flagTransmitting = true; resetRetryCount(); step = STEP.STEP_HANDSHAKE_1; + HIDTools.getInstance().flagReceiveData = false; if (HandShake(token)) { if (opType == OP_TYPE.WRITE) @@ -128,10 +129,11 @@ private bool HandShake(CancellationToken token) var byData = new byte[1]; while (flagTransmitting && !token.IsCancellationRequested) if (!flagRetry) - { + { ; switch (step) { case STEP.STEP_HANDSHAKE_1: + // Console.WriteLine("strp1"); byData = Encoding.ASCII.GetBytes("PROGRAMGT12"); byData = helper.LoadPackage(1, 0, byData, (byte)byData.Length); hid.Send(byData); @@ -143,6 +145,7 @@ private bool HandShake(CancellationToken token) step = STEP.STEP_HANDSHAKE_2; break; case STEP.STEP_HANDSHAKE_2: + // Console.WriteLine("strp2"); if (HIDTools.getInstance().flagReceiveData) { HIDTools.getInstance().flagReceiveData = false; @@ -158,6 +161,7 @@ private bool HandShake(CancellationToken token) break; case STEP.STEP_HANDSHAKE_3: + // Console.WriteLine("strp3"); if (HIDTools.getInstance().flagReceiveData) { HIDTools.getInstance().flagReceiveData = false; @@ -173,6 +177,7 @@ private bool HandShake(CancellationToken token) break; case STEP.STEP_HANDSHAKE_4: + // Console.WriteLine("strp4"); if (!HIDTools.getInstance().flagReceiveData) break; HIDTools.getInstance().flagReceiveData = false; @@ -221,7 +226,11 @@ private bool Write(CancellationToken token) ushort num = 0; byte b = 0; var num2 = 0; - while (flagTransmitting && token.IsCancellationRequested) + + // Console.WriteLine("we're in writing"); + while (flagTransmitting && !token.IsCancellationRequested) + { + // Console.WriteLine("wE;RE WRITEING"); if (!flagRetry) { switch (step) @@ -472,7 +481,7 @@ private bool Write(CancellationToken token) flagRetry = false; hid.Send(byData); } - + } return false; } diff --git a/Utils/HID/HIDTools.cs b/Utils/HID/HIDTools.cs index 9d24a72..5bcc52c 100644 --- a/Utils/HID/HIDTools.cs +++ b/Utils/HID/HIDTools.cs @@ -63,11 +63,18 @@ private void ReadCompleted(IAsyncResult iResult) var array1 = new byte[64]; Array.Copy(e.reportBuff, 0, array1, 0, 64); rxBuffer = array1; + // Console.WriteLine(BitConverter.ToString(rxBuffer)); flagReceiveData = true; if (hidStream.CanRead) BeginAsyncRead(); + else + { + // Console.WriteLine("stop read..."); + CloseDevice(); + } } - catch + catch(Exception e) { + // Console.WriteLine("stop read. due to."+e.Message); CloseDevice(); } } @@ -95,6 +102,7 @@ public HID_STATUS Write(Report r) } catch (Exception ex) { + // Console.WriteLine("stop write. due to."+ex.Message); CloseDevice(); return HID_STATUS.NO_DEVICE_CONNECTED; } @@ -112,6 +120,7 @@ public bool Send(byte[] byData) public void CloseDevice() { + // Console.WriteLine("closed..."); Gt12Device = null; hidStream.Close(); } diff --git a/Views/Gt12/ProgressBarWindow.axaml.cs b/Views/Gt12/ProgressBarWindow.axaml.cs index aff0436..f43c555 100644 --- a/Views/Gt12/ProgressBarWindow.axaml.cs +++ b/Views/Gt12/ProgressBarWindow.axaml.cs @@ -1,4 +1,5 @@ -using System.Threading; +using System; +using System.Threading; using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; @@ -63,6 +64,7 @@ private async void StartButton_OnClick(object? sender, RoutedEventArgs e) private void Task_Communication(CancellationToken token) { var flag = com.DoIt(token); + Console.WriteLine("We've done write!"); Dispatcher.UIThread.Post(() => HandleResult(flag)); } @@ -73,7 +75,7 @@ private void Task_Progress(CancellationToken token) ProgressBarValue pgv; if (!com.statusQueue.TryDequeue(out pgv)) continue; ; - Dispatcher.UIThread.Post(() => statusLabel.DataContext = pgv.content); + Dispatcher.UIThread.Post(() => statusLabel.Content = pgv.content); Dispatcher.UIThread.Post(() => progressBar.Value = pgv.value); } } @@ -108,7 +110,7 @@ private void HandleResult(bool result) private void Cancel_OnClick(object? sender, RoutedEventArgs e) { - if (thread_progress != null || thread_Communication != null) + if ((thread_progress != null || thread_Communication != null)&&operation==OP_TYPE.READ) { cancelSource.Cancel(); thread_progress.Join(); diff --git a/Views/Shx8x00/ProgressBarWindow.axaml.cs b/Views/Shx8x00/ProgressBarWindow.axaml.cs index 325b5e2..47a1036 100644 --- a/Views/Shx8x00/ProgressBarWindow.axaml.cs +++ b/Views/Shx8x00/ProgressBarWindow.axaml.cs @@ -39,7 +39,7 @@ public ProgressBarWindow(OPERATION_TYPE opStatus) private void Cancel_OnClick(object? sender, RoutedEventArgs e) { - if (threadWF != null || threadProgress != null) + if ((threadWF != null || threadProgress != null)&&status==OPERATION_TYPE.READ) { tokenSource.Cancel(); threadWF.Join();