Skip to content

Commit

Permalink
🔥 Support serial freqwri
Browse files Browse the repository at this point in the history
  • Loading branch information
SydneyOwl committed Aug 11, 2024
1 parent 6cbfb0c commit 368b730
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DataModels/Shx8800Pro/FMChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SenhaixFreqWriter.DataModels.Shx8800Pro;

public partial class FmChannel : ObservableObject
{
[ObservableProperty] private int[] _channels = new int[15];
[ObservableProperty] private int[] _channels = new int[30];
[ObservableProperty] private int _curFreq = 904;
}

Expand Down
36 changes: 18 additions & 18 deletions Utils/Serial/WriFreq8800Pro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public class WriFreq8800Pro

public ConcurrentQueue<ProgressBarValue> statusQueue = new();

public WriFreq8800Pro(OpType opType)
public WriFreq8800Pro(MySerialPort port, OpType opType)
{
this.port = MySerialPort.GetInstance();
this.port = port;
this.opType = opType;
this.appData = AppData.GetInstance();
helper = new DataHelper();
Expand Down Expand Up @@ -135,7 +135,7 @@ private bool HandShake(CancellationToken cancellationToken)
{
case Step.StepHandshake1:
array = Encoding.ASCII.GetBytes("PROGRAMSHXPU");
port.Write(array, 0, array.Length);
port.WriteByte(array, 0, array.Length);
progressVal = 0;
progressCont = "握手...";
statusQueue.Enqueue(new ProgressBarValue(progressVal,progressCont));
Expand All @@ -144,23 +144,23 @@ private bool HandShake(CancellationToken cancellationToken)
step = Step.StepHandshake2;
break;
case Step.StepHandshake2:
if (port.BytesToRead >= 1)
if (port.BytesToReadFromCache >= 1)
{
port.Read(rxBuffer, 0, 1);
port.ReadByte(rxBuffer, 0, 1);
if (rxBuffer[0] == 6)
{
array = new byte[1] { 70 };
port.Write(array, 0, array.Length);
port.WriteByte(array, 0, array.Length);
resetRetryCount();
step = Step.StepHandshake3;
}
}

break;
case Step.StepHandshake3:
if (port.BytesToRead >= 16)
if (port.BytesToReadFromCache >= 16)
{
port.Read(rxBuffer, 0, 16);
port.ReadByte(rxBuffer, 0, 16);
timer.Stop();
resetRetryCount();
progressVal = 0;
Expand Down Expand Up @@ -190,7 +190,7 @@ private bool HandShake(CancellationToken cancellationToken)

timesOfRetry--;
flagRetry = false;
port.Write(array, 0, array.Length);
port.WriteByte(array, 0, array.Length);
}
}

Expand Down Expand Up @@ -346,7 +346,7 @@ private bool Write(CancellationToken cancellationToken)
}

array = helper.LoadPackage(87, num, array2, (byte)array2.Length);
port.Write(array, 0, array.Length);
port.WriteByte(array, 0, array.Length);
timer.Start();
progressVal = num * 100 / 45056;
if (progressVal > 100)
Expand All @@ -358,12 +358,12 @@ private bool Write(CancellationToken cancellationToken)
step = Step.StepWrite2;
break;
case Step.StepWrite2:
if (port.BytesToRead < 1)
if (port.BytesToReadFromCache < 1)
{
break;
}

port.Read(rxBuffer, 0, 1);
port.ReadByte(rxBuffer, 0, 1);
if (rxBuffer[0] == 6)
{
timer.Stop();
Expand Down Expand Up @@ -408,7 +408,7 @@ private bool Write(CancellationToken cancellationToken)

timesOfRetry--;
flagRetry = false;
port.Write(array, 0, array.Length);
port.WriteByte(array, 0, array.Length);
}
}

Expand Down Expand Up @@ -701,7 +701,7 @@ private bool Read(CancellationToken cancellationToken)
(byte)num,
64
};
port.Write(array2, 0, array2.Length);
port.WriteByte(array2, 0, array2.Length);
progressVal = num * 100 / 45056;
if (progressVal > 100)
{
Expand All @@ -716,14 +716,14 @@ private bool Read(CancellationToken cancellationToken)
break;
case Step.StepRead2:
{
if (port.BytesToRead < 68)
if (port.BytesToReadFromCache < 68)
{
break;
}

timer.Stop();
resetRetryCount();
port.Read(rxBuffer, 0, port.BytesToRead);
port.ReadByte(rxBuffer, 0, port.BytesToReadFromCache);
byte b = (byte)(num >> 8);
byte b2 = (byte)num;
if (rxBuffer[1] != b || rxBuffer[2] != b2)
Expand Down Expand Up @@ -889,7 +889,7 @@ private bool Read(CancellationToken cancellationToken)
progressCont = "完成";
statusQueue.Enqueue(new ProgressBarValue(progressVal,progressCont));
array2 = new byte[1] { 69 };
port.Write(array2, 0, array2.Length);
port.WriteByte(array2, 0, array2.Length);
flagTransmitting = false;
return true;
}
Expand All @@ -906,7 +906,7 @@ private bool Read(CancellationToken cancellationToken)

timesOfRetry--;
flagRetry = false;
port.Write(array2, 0, array2.Length);
port.WriteByte(array2, 0, array2.Length);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="150"
x:Class="SenhaixFreqWriter.Views.Shx8x00.PortSelectionWindow"
x:Class="SenhaixFreqWriter.Views.Common.PortSelectionWindow"
Title="端口"
xmlns:vm="using:SenhaixFreqWriter.Views.Shx8x00"
xmlns:vm="using:SenhaixFreqWriter.Views.Common"
x:DataType="vm:PortSelectionWindow"
WindowStartupLocation="CenterScreen">
<Canvas>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Avalonia.Interactivity;
using SenhaixFreqWriter.Utils.Serial;

namespace SenhaixFreqWriter.Views.Shx8x00;
namespace SenhaixFreqWriter.Views.Common;

public partial class PortSelectionWindow : Window
{
Expand Down
12 changes: 12 additions & 0 deletions Views/Shx8800Pro/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Header="读写频方式设置">
<MenuItem Header="写频线" Click="portSel_OnClick" Name="cable">
<MenuItem.Icon>
<PathIcon Data="{StaticResource usb_stick_regular}" />
</MenuItem.Icon>
</MenuItem>
<!-- <MenuItem Header="蓝牙" Click="MenuConnectBT_OnClick"> -->
<!-- <MenuItem.Icon> -->
<!-- <PathIcon Data="{StaticResource bluetooth_regular}" /> -->
<!-- </MenuItem.Icon> -->
<!-- </MenuItem> -->
</MenuItem>
<MenuItem Header="工具">
<MenuItem Header="修改开机画面" Click="BootImageMenuItem_OnClick">
<MenuItem.Icon>
Expand Down
7 changes: 4 additions & 3 deletions Views/Shx8800Pro/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
using SenhaixFreqWriter.DataModels.Shx8800Pro;
using SenhaixFreqWriter.Views.Common;
using SenhaixFreqWriter.Views.Plugin;
#if WINDOWS
using SenhaixFreqWriter.Utils.BLE.Platforms.Windows;
#endif

namespace SenhaixFreqWriter.Views.Shx8800Pro;

Expand Down Expand Up @@ -499,4 +496,8 @@ private void DebugWindowMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
DebugWindow.GetInstance().Show();
}
private void portSel_OnClick(object? sender, RoutedEventArgs e)
{
new PortSelectionWindow().ShowDialog(this);
}
}
10 changes: 9 additions & 1 deletion Views/Shx8800Pro/ProgressBarWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public partial class ProgressBarWindow : Window

private Thread _threadProgress;

private MySerialPort port;

public ProgressBarWindow(OpType op)
{
_operation = op;
InitializeComponent();
_com = new WriFreq8800Pro(op);
port = MySerialPort.GetInstance();
_com = new WriFreq8800Pro(port,op);
}

private async void StartButton_OnClick(object? sender, RoutedEventArgs e)
Expand All @@ -48,6 +51,7 @@ private async void StartButton_OnClick(object? sender, RoutedEventArgs e)
return;
}

port.OpenSerial();
_cancelSource = new CancellationTokenSource();
StartButton.IsEnabled = false;
CloseButton.IsEnabled = true;
Expand All @@ -71,6 +75,10 @@ private void Task_Communication(CancellationToken token)
DebugWindow.GetInstance().updateDebugContent(a.Message);
// Console.Write(a);
}
finally
{
port.CloseSerial();
}

// DebugWindow.GetInstance().updateDebugContent("We've done write!");
Dispatcher.UIThread.Invoke(() => HandleResult(flag));
Expand Down

0 comments on commit 368b730

Please sign in to comment.