feat: 将摄像头数据从生成的数据改为读取实际数据
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
|
||||
namespace Peripherals.CameraClient;
|
||||
|
||||
static class CameraAddr
|
||||
{
|
||||
public const UInt32 Base = 0x0000_0000;
|
||||
public const UInt32 FrameLength = 0x25800;
|
||||
}
|
||||
|
||||
class Camera
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
readonly int timeout = 2000;
|
||||
|
||||
readonly int port;
|
||||
readonly string address;
|
||||
private IPEndPoint ep;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化摄像头客户端
|
||||
/// </summary>
|
||||
/// <param name="address">摄像头设备IP地址</param>
|
||||
/// <param name="port">摄像头设备端口</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
public Camera(string address, int port, int timeout = 2000)
|
||||
{
|
||||
if (timeout < 0)
|
||||
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取一帧图像数据
|
||||
/// </summary>
|
||||
/// <returns>包含图像数据的字节数组</returns>
|
||||
public async ValueTask<Result<byte[]>> ReadFrame()
|
||||
{
|
||||
// 清除UDP服务器接收缓冲区
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 3);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
|
||||
// 使用UDPClientPool读取图像帧数据
|
||||
var result = await UDPClientPool.ReadAddrBytes(
|
||||
this.ep,
|
||||
3, // taskID
|
||||
CameraAddr.Base,
|
||||
(int)CameraAddr.FrameLength,
|
||||
this.timeout);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read frame from camera {this.address}:{this.port}, error: {result.Error}");
|
||||
return new(result.Error);
|
||||
}
|
||||
|
||||
logger.Debug($"Successfully read frame from camera {this.address}:{this.port}, data length: {result.Value.Length} bytes");
|
||||
return result.Value;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user