Skip to content

Commit

Permalink
🎉 gt12 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SydneyOwl committed May 1, 2024
1 parent 172c6c2 commit 09474f5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
33 changes: 21 additions & 12 deletions Utils/HID/DataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions Utils/HID/HIDCommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -472,7 +481,7 @@ private bool Write(CancellationToken token)
flagRetry = false;
hid.Send(byData);
}

}
return false;
}

Expand Down
11 changes: 10 additions & 1 deletion Utils/HID/HIDTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -112,6 +120,7 @@ public bool Send(byte[] byData)

public void CloseDevice()
{
// Console.WriteLine("closed...");
Gt12Device = null;
hidStream.Close();
}
Expand Down
8 changes: 5 additions & 3 deletions Views/Gt12/ProgressBarWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
Expand Down Expand Up @@ -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));
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Views/Shx8x00/ProgressBarWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 09474f5

Please sign in to comment.