Skip to content

Commit

Permalink
fix: 修复发送文件时进度条显示异常的问题(对串行端口实现异步读写)
Browse files Browse the repository at this point in the history
Signed-off-by: Leven <levenchn@163.com>
  • Loading branch information
leven99 committed Oct 14, 2019
1 parent 3bf1aaf commit 422bc40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
29 changes: 17 additions & 12 deletions ViewModels/MainWindowVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,9 @@ public void InitAutoSendTimer()
AutoSendDispatcherTimer.Tick += AutoSendDispatcherTimer_Tick;
}

public void AutoSendDispatcherTimer_Tick(object sender, EventArgs e)
public async void AutoSendDispatcherTimer_Tick(object sender, EventArgs e)
{
Send();
await SendAsync().ConfigureAwait(false);
}

public void StartAutoSendTimer(int interval)
Expand All @@ -763,7 +763,7 @@ public void StopAutoSendTimer()
#endregion

#region 发送
public void Send()
public async Task SendAsync()
{
if (SPserialPort == null)
{
Expand Down Expand Up @@ -794,13 +794,14 @@ public void Send()
sendData[SendCount++] = byte.Parse(tmp, NumberStyles.AllowHexSpecifier, cultureInfo);
}

SPserialPort.Write(sendData, 0, SendCount);
await SPserialPort.BaseStream.WriteAsync(sendData, 0, SendCount).ConfigureAwait(false);

}
else
{
SendCount = SPserialPort.Encoding.GetByteCount(SendModel.SendData);
SPserialPort.Write(SPserialPort.Encoding.GetBytes(SendModel.SendData), 0, SendCount);
await SPserialPort.BaseStream.WriteAsync(SPserialPort.Encoding.GetBytes(SendModel.SendData), 0, SendCount)
.ConfigureAwait(false);
}

if (SendModel.NonesEnable)
Expand All @@ -809,17 +810,20 @@ public void Send()
}
else if (SendModel.CrEnable)
{
SPserialPort.Write(SPserialPort.Encoding.GetBytes("\r"), 0, 1);
await SPserialPort.BaseStream.WriteAsync(SPserialPort.Encoding.GetBytes("\r"), 0, 1)
.ConfigureAwait(false);
SendModel.SendDataCount += (SendCount + 1);
}
else if (SendModel.LfEnable)
{
SPserialPort.Write(SPserialPort.Encoding.GetBytes("\n"), 0, 1);
await SPserialPort.BaseStream.WriteAsync(SPserialPort.Encoding.GetBytes("\n"), 0, 1)
.ConfigureAwait(false);
SendModel.SendDataCount += (SendCount + 1);
}
else if (SendModel.CrLfEnable)
{
SPserialPort.Write(SPserialPort.Encoding.GetBytes("\r\n"), 0, 2);
await SPserialPort.BaseStream.WriteAsync(SPserialPort.Encoding.GetBytes("\r\n"), 0, 2)
.ConfigureAwait(false);
SendModel.SendDataCount += (SendCount + 2);
}
}
Expand All @@ -831,7 +835,7 @@ public void Send()
#endregion

#region 发送文件
public void SendFile()
public async Task SendFileAsync()
{
if (SPserialPort == null)
{
Expand Down Expand Up @@ -877,7 +881,8 @@ public void SendFile()
DepictInfo = string.Format(cultureInfo, "文件正在发送......");
HelpModel.StatusBarProgressBarIsIndeterminate = true;

SPserialPort.Write(SPserialPort.Encoding.GetBytes(fileContent), 0, SendCount);
await SPserialPort.BaseStream.WriteAsync(SPserialPort.Encoding.GetBytes(fileContent), 0, SendCount)
.ConfigureAwait(false);

HelpModel.StatusBarProgressBarIsIndeterminate = false;
DepictInfo = string.Format(cultureInfo, "文件发送完毕");
Expand Down Expand Up @@ -941,14 +946,14 @@ public void ClearCount()
#endregion

#region 数据接收事件实现
public void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
public async void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort _SerialPort = (SerialPort)sender;

int _bytesToRead = _SerialPort.BytesToRead;
byte[] recvData = new byte[_bytesToRead];

_SerialPort.Read(recvData, 0, _bytesToRead);
await _SerialPort.BaseStream.ReadAsync(recvData, 0, _bytesToRead).ConfigureAwait(false);

if (RecvModel.EnableRecv)
{
Expand Down
8 changes: 4 additions & 4 deletions Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,16 @@ private void OpenCloseSP(object sender, RoutedEventArgs e)
#endregion

#region 发送
private void Send(object sender, RoutedEventArgs e)
private async void Send(object sender, RoutedEventArgs e)
{
mainWindowViewModel.Send();
await mainWindowViewModel.SendAsync().ConfigureAwait(false);
}
#endregion

#region 发送文件
private void SendFile(object sender, RoutedEventArgs e)
private async void SendFile(object sender, RoutedEventArgs e)
{
mainWindowViewModel.SendFile();
await mainWindowViewModel.SendFileAsync().ConfigureAwait(false);
}
#endregion

Expand Down

0 comments on commit 422bc40

Please sign in to comment.