Explorar el Código

重构串口读取超时问题

dev1
永攀 张 hace 2 semanas
padre
commit
aee40c7b6f
Se han modificado 3 ficheros con 83 adiciones y 39 borrados
  1. +74
    -30
      ModbusDemo/Device/ModbusRTU.cs
  2. +8
    -8
      ModbusDemo/VIew/SettingsUC.xaml
  3. +1
    -1
      ModbusDemo/VIewModel/SettingsUCViewModel.cs

+ 74
- 30
ModbusDemo/Device/ModbusRTU.cs Ver fichero

@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

@@ -56,17 +57,28 @@ namespace ModbusDemo.Device
byte[] sendByte = sendByteList.ToArray();
int maxRetries = 3; // 最大重试次数
int attempt = 0;

byte[] response = null;
List<byte> responseData = new List<byte>();
_serialPort.Write(sendByte, 0, sendByte.Length);
while (attempt < maxRetries)
{
try
{
// 清空接收缓冲区(避免残留数据干扰)
_serialPort.DiscardInBuffer();
_serialPort.Write(sendByte, 0, sendByte.Length);
Thread.Sleep(300);
byte[] response = new byte[_serialPort.BytesToRead];
_serialPort.Read(response, 0, _serialPort.BytesToRead);
//使用时间去轮询查询数据
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalMilliseconds < _serialPort.ReadTimeout)
{
int bytesToRead = _serialPort.BytesToRead;
if (bytesToRead > 0)
{
byte[] buffer = new byte[bytesToRead];
int bytesRead = _serialPort.Read(buffer, 0, bytesToRead);
responseData.AddRange(buffer.Take(bytesRead));
}
//延迟20毫秒,节约cpu资源
Task.Delay(20);
}
response = responseData.ToArray();
if (response[0] == 0x00)
{
continue;
@@ -203,16 +215,27 @@ namespace ModbusDemo.Device
int maxRetries = 3; // 最大重试次数
int attempt = 0;
byte[] response = null;
List<byte> responseData = new List<byte>();
_serialPort.Write(sendByte, 0, sendByte.Length);
while (attempt < maxRetries)
{
try
{
// 清空接收缓冲区(避免残留数据干扰)
_serialPort.DiscardInBuffer();
_serialPort.Write(sendByte, 0, sendByte.Length);
Thread.Sleep(300);
response = new byte[_serialPort.BytesToRead];
_serialPort.Read(response, 0, _serialPort.BytesToRead);
//使用时间去轮询查询数据
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalMilliseconds < _serialPort.ReadTimeout)
{
int bytesToRead = _serialPort.BytesToRead;
if (bytesToRead > 0)
{
byte[] buffer = new byte[bytesToRead];
int bytesRead = _serialPort.Read(buffer, 0, bytesToRead);
responseData.AddRange(buffer.Take(bytesRead));
}
//延迟20毫秒,节约cpu资源
Task.Delay(20);
}
response = responseData.ToArray();
if (response[0] != 0x00)
{
break;
@@ -271,17 +294,29 @@ namespace ModbusDemo.Device
byte[] sendByte = sendByteList.ToArray();
int maxRetries = 3; // 最大重试次数
int attempt = 0;
List<byte> responseData = new List<byte>();
byte[] response = null;
_serialPort.Write(sendByte, 0, sendByte.Length);
while (attempt < maxRetries)
{
try
{
// 清空接收缓冲区(避免残留数据干扰)
_serialPort.DiscardInBuffer();

_serialPort.Write(sendByte, 0, sendByte.Length);
Thread.Sleep(300);
byte[] response = new byte[_serialPort.BytesToRead];
_serialPort.Read(response, 0, _serialPort.BytesToRead);
//使用时间去轮询查询数据
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalMilliseconds < _serialPort.ReadTimeout)
{
int bytesToRead = _serialPort.BytesToRead;
if (bytesToRead > 0)
{
byte[] buffer = new byte[bytesToRead];
int bytesRead = _serialPort.Read(buffer, 0, bytesToRead);
responseData.AddRange(buffer.Take(bytesRead));
}
//延迟20毫秒,节约cpu资源
Task.Delay(20);
}
response = responseData.ToArray();
if (response[0] == 0x00)
{
continue;
@@ -388,18 +423,27 @@ namespace ModbusDemo.Device
int maxRetries = 3; // 最大重试次数
int attempt = 0;
byte[] response = null;

while (attempt< maxRetries)
List<byte> responseData = new List<byte>();
_serialPort.Write(sendByte, 0, sendByte.Length);
while (attempt < maxRetries)
{
try
{
// 清空接收缓冲区(避免残留数据干扰)
_serialPort.DiscardInBuffer();

_serialPort.Write(sendByte, 0, sendByte.Length);
Thread.Sleep(1000);
response = new byte[_serialPort.BytesToRead];
_serialPort.Read(response, 0, _serialPort.BytesToRead);
//使用时间去轮询查询数据
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalMilliseconds < _serialPort.ReadTimeout)
{
int bytesToRead = _serialPort.BytesToRead;
if (bytesToRead > 0)
{
byte[] buffer = new byte[bytesToRead];
int bytesRead = _serialPort.Read(buffer, 0, bytesToRead);
responseData.AddRange(buffer.Take(bytesRead));
}
//延迟20毫秒,节约cpu资源
Task.Delay(20);
}
response = responseData.ToArray();
if (response[0] != 0x00)
{
break;
@@ -410,14 +454,14 @@ namespace ModbusDemo.Device
attempt++;
}
}
//将操作的指令,插入数据库
string RequestStr = ByteArrayToString(sendByte);
string responseStr = ByteArrayToString(response);
ModbusLog m = new ModbusLog();
m.OperationType = "写寄存器";
m.ResponseData = responseStr;
m.RequestData = responseStr;
m.RequestData = RequestStr;
_modbusDbContext.Add(m);
_modbusDbContext.SaveChanges();



+ 8
- 8
ModbusDemo/VIew/SettingsUC.xaml Ver fichero

@@ -31,7 +31,7 @@
<RowDefinition>
</RowDefinition>
</Grid.RowDefinitions>
<Button Width="80" Height="30" Click="Button_Click">连接</Button>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition>
@@ -72,7 +72,7 @@
Height="30"
Margin="10,0,0,5"
HorizontalAlignment="Left"
materialDesign:HintAssist.Hint="19200"
materialDesign:HintAssist.Hint="9600"
SelectedItem="{Binding BaudRate}">
<ComboBoxItem Content="4800" />
<ComboBoxItem Content="9600" />
@@ -139,18 +139,18 @@
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Button
Width="80"
Height="30"
materialDesign:ButtonAssist.CornerRadius="15"
Width="100"
Height="50"
materialDesign:ButtonAssist.CornerRadius="25"
Style="{StaticResource MaterialDesignRaisedDarkButton}"
Content="连接"
Command="{Binding ConnectionCmm}">
</Button>
<Button
Grid.Column="1"
Width="80"
Height="30"
materialDesign:ButtonAssist.CornerRadius="15"
Width="100"
Height="50"
materialDesign:ButtonAssist.CornerRadius="25"
Style="{StaticResource MaterialDesignRaisedDarkButton}"
Content="断开"
Command="{Binding DisConnectionCmm}">


+ 1
- 1
ModbusDemo/VIewModel/SettingsUCViewModel.cs Ver fichero

@@ -38,7 +38,7 @@ namespace ModbusDemo.VIewModel
}

//设置波特率
private string _baudRate = "19200";
private string _baudRate = "9600";

public string BaudRate
{


Cargando…
Cancelar
Guardar