add nlog for server

This commit is contained in:
2025-04-09 19:42:29 +08:00
parent 5c0f5b2127
commit 143d6c634b
11 changed files with 358 additions and 77 deletions

View File

@@ -42,8 +42,10 @@ namespace WebProtocol
public BurstType burstType;
/// <example>1</example>
public byte commandID;
/// <summary> 标识写入还是读取 </summary>
/// <example> true </example>
public bool isWrite;
/// <summary> 突发长度0是32bits255是32bits x 256 </summary>
/// <example> 255 </example>
public byte burstLength;
/// <example> 0x10_00_00_00 </example>
@@ -62,6 +64,19 @@ namespace WebProtocol
/// <summary> Package Options which to receive from boards </summary>
public struct RecvPackOptions
{
/// <summary> 数据包类型 </summary>
public enum PackType
{
/// <summary> 读响应包 </summary>
ReadResp,
/// <summary> 写响应包 </summary>
WriteResp
};
/// <summary>
/// 数据包类型
/// </summary>
public PackType type;
/// <summary>
/// Task ID
/// </summary>
@@ -85,7 +100,7 @@ namespace WebProtocol
}
}
/// <summary> Package which send address to write </summary>
/// <summary> Server->FPGA 地址包 </summary>
public struct SendAddrPackage
{
readonly byte sign = (byte)PackSign.SendAddr;
@@ -94,22 +109,36 @@ namespace WebProtocol
readonly byte _reserved = 0;
readonly UInt32 address;
/// <summary>
/// 使用地址包选项构造地址包
/// </summary>
/// <param name="opts"> 地址包选项 </param>
public SendAddrPackage(SendAddrPackOptions opts)
{
byte byteBurstType = Convert.ToByte((byte)opts.burstType << 5);
byte byteCommandID = Convert.ToByte((opts.commandID & 0x03) << 3);
byte byteBurstType = Convert.ToByte((byte)opts.burstType << 6);
byte byteCommandID = Convert.ToByte((opts.commandID & 0x03) << 4);
byte byteIsWrite = (opts.isWrite ? (byte)0x01 : (byte)0x00);
this.commandType = Convert.ToByte(byteBurstType | byteCommandID | byteIsWrite);
this.burstLength = opts.burstLength;
this.address = opts.address;
}
public SendAddrPackage(BurstType burstType, byte commandID, bool isWrite)
/// <summary>
/// 使用完整参数构造地址包
/// </summary>
/// <param name="burstType"> 突发类型 </param>
/// <param name="commandID"> 任务ID </param>
/// <param name="isWrite"> 是否是写数据 </param>
/// <param name="burstLength"> 突发长度 </param>
/// <param name="address"> 设备地址 </param>
public SendAddrPackage(BurstType burstType, byte commandID, bool isWrite, byte burstLength, UInt32 address)
{
byte byteBurstType = Convert.ToByte((byte)burstType << 5);
byte byteCommandID = Convert.ToByte((commandID & 0x03) << 3);
byte byteBurstType = Convert.ToByte((byte)burstType << 6);
byte byteCommandID = Convert.ToByte((commandID & 0x03) << 4);
byte byteIsWrite = (isWrite ? (byte)0x01 : (byte)0x00);
this.commandType = Convert.ToByte(byteBurstType | byteCommandID | byteIsWrite);
this.burstLength = burstLength;
this.address = address;
}
public SendAddrPackage(byte commandType, byte burstLength, UInt32 address)
@@ -119,6 +148,10 @@ namespace WebProtocol
this.address = address;
}
/// <summary>
/// 将地址包转化为字节数组
/// </summary>
/// <returns> 字节数组 </returns>
public byte[] ToBytes()
{
var arr = new byte[8];
@@ -133,11 +166,15 @@ namespace WebProtocol
return arr;
}
/// <summary>
/// 讲地址包转化为Json格式的地址包选项
/// </summary>
/// <returns> 字符串 </returns>
public override string ToString()
{
SendAddrPackOptions opts;
opts.burstType = (BurstType)(commandType >> 5);
opts.commandID = Convert.ToByte((commandType >> 3) & 0b0011);
opts.burstType = (BurstType)(commandType >> 6);
opts.commandID = Convert.ToByte((commandType >> 4) & 0b0011);
opts.isWrite = Convert.ToBoolean(commandType & 0x01);
opts.burstLength = burstLength;
opts.address = address;
@@ -194,6 +231,7 @@ namespace WebProtocol
}
/// <summary> FPGA->Server 读数据包 </summary>
public struct RecvDataPackage
{
readonly byte sign = (byte)PackSign.RecvData;
@@ -202,6 +240,13 @@ namespace WebProtocol
readonly byte _reserved = 0;
readonly byte[] bodyData;
/// <summary>
/// FPGA->Server 读数据包
/// 构造函数
/// </summary>
/// <param name="commandID"> 任务ID号 </param>
/// <param name="resp"> 读数据包响应 </param>
/// <param name="bodyData"> 数据 </param>
public RecvDataPackage(byte commandID, byte resp, byte[] bodyData)
{
this.commandID = commandID;
@@ -209,13 +254,17 @@ namespace WebProtocol
this.bodyData = bodyData;
}
/// <summary>
/// 获取读数据包选项
/// </summary>
public RecvPackOptions Options
{
get
{
RecvPackOptions opts;
opts.type = RecvPackOptions.PackType.ReadResp;
opts.commandID = commandID;
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? true : false);
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
opts.data = bodyData;
return opts;
@@ -246,14 +295,28 @@ namespace WebProtocol
this.resp = resp;
}
public RecvPackOptions Options()
public RecvPackOptions Options
{
RecvPackOptions opts;
opts.commandID = commandID;
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? true : false);
opts.data = null;
get
{
RecvPackOptions opts;
opts.type = RecvPackOptions.PackType.WriteResp;
opts.commandID = commandID;
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
opts.data = null;
return opts;
return opts;
}
}
public static Result<RecvRespPackage> FromBytes(byte[] bytes)
{
if (bytes[0] != (byte)PackSign.RecvResp)
throw new ArgumentException(
"The sign of bytes is not RecvData Package!",
nameof(bytes)
);
return new RecvRespPackage(bytes[1], bytes[2]);
}
}
}