Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
fix: 读完串口所有数据再返回
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxuuu committed Aug 14, 2023
1 parent bf83685 commit 16ccba2
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions AirISP/BasicOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ public static bool ResetAPP()
}
}

/// <summary>
/// 读取连续完整的串口数据
/// </summary>
/// <returns></returns>
private static byte[] readSerial()
{
//一次性读完,防止读不全
var buff = new List<byte>();
var toRead = serial.BytesToRead;
while (toRead > 0)
{
var buffer = new byte[toRead];
serial.Read(buffer, 0, toRead);
buff.AddRange(buffer);
Thread.Sleep(1);
toRead = serial.BytesToRead;
}
return buff.ToArray();
}

/// <summary>
/// 向芯片中写入一系列数据,并检查是否有ACK
/// </summary>
Expand Down Expand Up @@ -303,8 +323,7 @@ public static bool Write(byte[] data, int timeOut = 200, int ACKCount = 1)
length = serial.BytesToRead;
if (length > 0)
{
var rev = new byte[length];
serial.Read(rev, 0, length);
var rev = readSerial();
if (baseParameter.Trace == true)
{
ColorfulConsole.LogLine($"Retrieved data: {BitConverter.ToString(rev)}");
Expand Down Expand Up @@ -346,8 +365,7 @@ public static bool Write(byte[] data, int timeOut = 200, int ACKCount = 1)
length = serial.BytesToRead;
if (length > 0)
{
var rev = new byte[length];
serial.Read(rev, 0, length);
var rev = readSerial();
if (baseParameter.Trace == true)
{
ColorfulConsole.LogLine($"Retrieved data: {BitConverter.ToString(rev)}");
Expand Down

0 comments on commit 16ccba2

Please sign in to comment.