feat: 将摄像头数据从生成的数据改为读取实际数据

This commit is contained in:
2025-07-03 15:47:00 +08:00
parent bed0158a5f
commit 178ac0de67
4 changed files with 583 additions and 80 deletions

View File

@@ -177,13 +177,13 @@ public class UDPClientPool
}
/// <summary>
/// [TODO:description]
/// 读取设备地址数据
/// </summary>
/// <param name="endPoint">[TODO:parameter]</param>
/// <param name="taskID">[TODO:parameter]</param>
/// <param name="devAddr">[TODO:parameter]</param>
/// <param name="timeout">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>读取结果,包含接收到的数据包</returns>
public static async ValueTask<Result<RecvDataPackage>> ReadAddr(
IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000)
{
@@ -218,15 +218,15 @@ public class UDPClientPool
}
/// <summary>
/// [TODO:description]
/// 读取设备地址数据并校验结果
/// </summary>
/// <param name="endPoint">[TODO:parameter]</param>
/// <param name="taskID">[TODO:parameter]</param>
/// <param name="devAddr">[TODO:parameter]</param>
/// <param name="result">[TODO:parameter]</param>
/// <param name="resultMask">[TODO:parameter]</param>
/// <param name="timeout">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param>
/// <param name="result">期望的结果值</param>
/// <param name="resultMask">结果掩码,用于位校验</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>校验结果true表示数据匹配期望值</returns>
public static async ValueTask<Result<bool>> ReadAddr(
IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int timeout = 1000)
{
@@ -257,15 +257,15 @@ public class UDPClientPool
}
/// <summary>
/// [TODO:description]
/// 读取设备地址数据并等待直到结果匹配或超时
/// </summary>
/// <param name="endPoint">[TODO:parameter]</param>
/// <param name="taskID">[TODO:parameter]</param>
/// <param name="devAddr">[TODO:parameter]</param>
/// <param name="result">[TODO:parameter]</param>
/// <param name="resultMask">[TODO:parameter]</param>
/// <param name="timeout">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param>
/// <param name="result">期望的结果值</param>
/// <param name="resultMask">结果掩码,用于位校验</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>校验结果true表示在超时前数据匹配期望值</returns>
public static async ValueTask<Result<bool>> ReadAddrWithWait(
IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int timeout = 1000)
{
@@ -305,16 +305,93 @@ public class UDPClientPool
return false;
}
/// <summary>
/// 从设备地址读取字节数组数据(支持大数据量分段传输)
/// </summary>
/// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param>
/// <param name="dataLength">要读取的数据长度(字节)</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>读取结果,包含接收到的字节数组</returns>
public static async ValueTask<Result<byte[]>> ReadAddrBytes(
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, int timeout = 1000)
{
var ret = false;
var opts = new SendAddrPackOptions();
var resultData = new List<byte>();
opts.BurstType = BurstType.FixedBurst;
opts.CommandID = Convert.ToByte(taskID);
opts.Address = devAddr;
opts.IsWrite = false;
// Check Msg Bus
if (!MsgBus.IsRunning)
return new(new Exception("Message bus not working!"));
// Calculate read times and segments
var maxBytesPerRead = 256 * (32 / 8); // 1024 bytes per read
var hasRest = dataLength % maxBytesPerRead != 0;
var readTimes = hasRest ?
dataLength / maxBytesPerRead + 1 :
dataLength / maxBytesPerRead;
for (var i = 0; i < readTimes; i++)
{
// Calculate current segment size
var isLastSegment = i == readTimes - 1;
var currentSegmentSize = isLastSegment && hasRest ?
dataLength % maxBytesPerRead :
maxBytesPerRead;
// Set burst length (in 32-bit words)
opts.BurstLength = (byte)(currentSegmentSize / 4 - 1);
// Update address for current segment
opts.Address = devAddr + (uint)(i * maxBytesPerRead);
// Send read address package
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
if (!ret) return new(new Exception($"Send address package failed at segment {i}!"));
// Wait for data response
var retPack = await MsgBus.UDPServer.WaitForDataAsync(
endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
if (!retPack.IsSuccessful) return new(retPack.Error);
if (!retPack.Value.IsSuccessful)
return new(new Exception($"Read address package failed at segment {i}"));
var retPackOpts = retPack.Value.Options;
if (retPackOpts.Data is null)
return new(new Exception($"Data is null at segment {i}, package: {retPackOpts.ToString()}"));
// Validate received data length
if (retPackOpts.Data.Length != currentSegmentSize)
return new(new Exception($"Expected {currentSegmentSize} bytes but received {retPackOpts.Data.Length} bytes at segment {i}"));
// Add received data to result
resultData.AddRange(retPackOpts.Data);
}
// Validate total data length
if (resultData.Count != dataLength)
return new(new Exception($"Expected total {dataLength} bytes but received {resultData.Count} bytes"));
return resultData.ToArray();
}
/// <summary>
/// [TODO:description]
/// 向设备地址写入32位数据
/// </summary>
/// <param name="endPoint">[TODO:parameter]</param>
/// <param name="taskID">[TODO:parameter]</param>
/// <param name="devAddr">[TODO:parameter]</param>
/// <param name="data">[TODO:parameter]</param>
/// <param name="timeout">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param>
/// <param name="data">要写入的32位数据</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>写入结果true表示写入成功</returns>
public static async ValueTask<Result<bool>> WriteAddr(
IPEndPoint endPoint, int taskID, UInt32 devAddr, UInt32 data, int timeout = 1000)
{
@@ -348,14 +425,14 @@ public class UDPClientPool
}
/// <summary>
/// [TODO:description]
/// 向设备地址写入字节数组数据(支持大数据量分段传输)
/// </summary>
/// <param name="endPoint">[TODO:parameter]</param>
/// <param name="taskID">[TODO:parameter]</param>
/// <param name="devAddr">[TODO:parameter]</param>
/// <param name="dataArray">[TODO:parameter]</param>
/// <param name="timeout">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param>
/// <param name="dataArray">要写入的字节数组</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>写入结果true表示写入成功</returns>
public static async ValueTask<Result<bool>> WriteAddr(
IPEndPoint endPoint, int taskID, UInt32 devAddr, byte[] dataArray, int timeout = 1000)
{
@@ -404,5 +481,4 @@ public class UDPClientPool
return true;
}
}