finish web test api
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using DotNext;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace WebProtocol
|
||||
{
|
||||
|
||||
public enum PackSign
|
||||
{
|
||||
SendAddr = 0x00,
|
||||
@@ -15,36 +17,179 @@ namespace WebProtocol
|
||||
FixedBurst = 0b01,
|
||||
}
|
||||
|
||||
public struct SendAddrPackOptions
|
||||
{
|
||||
public BurstType burstType;
|
||||
public byte commandID;
|
||||
public bool isWrite;
|
||||
public byte burstLength;
|
||||
public UInt32 address;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this);
|
||||
}
|
||||
}
|
||||
|
||||
public struct RecvPackOptions
|
||||
{
|
||||
public byte commandID;
|
||||
public bool isSuccess;
|
||||
}
|
||||
|
||||
public struct SendAddrPackage
|
||||
{
|
||||
readonly byte Sign = (byte)PackSign.SendAddr;
|
||||
readonly byte sign = (byte)PackSign.SendAddr;
|
||||
readonly byte commandType;
|
||||
readonly byte burstLength;
|
||||
readonly byte _reserved;
|
||||
readonly byte _reserved = 0;
|
||||
readonly UInt32 address;
|
||||
|
||||
public SendAddrPackage(SendAddrPackOptions opts)
|
||||
{
|
||||
byte byteBurstType = Convert.ToByte((byte)opts.burstType << 5);
|
||||
byte byteCommandID = Convert.ToByte((opts.commandID & 0x03) << 3);
|
||||
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)
|
||||
{
|
||||
commandType = (
|
||||
((byte)burstType << 5) |
|
||||
((commandID & (byte)0x03) << 3) |
|
||||
(isWrite ? (byte)0x01 : (byte)0x00)
|
||||
);
|
||||
byte byteBurstType = Convert.ToByte((byte)burstType << 5);
|
||||
byte byteCommandID = Convert.ToByte((commandID & 0x03) << 3);
|
||||
byte byteIsWrite = (isWrite ? (byte)0x01 : (byte)0x00);
|
||||
this.commandType = Convert.ToByte(byteBurstType | byteCommandID | byteIsWrite);
|
||||
}
|
||||
|
||||
public void toBytes()
|
||||
public SendAddrPackage(byte commandType, byte burstLength, UInt32 address)
|
||||
{
|
||||
this.commandType = commandType;
|
||||
this.burstLength = burstLength;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public byte[] ToBytes()
|
||||
{
|
||||
var arr = new byte[8];
|
||||
arr[0] = sign;
|
||||
arr[1] = commandType;
|
||||
arr[2] = burstLength;
|
||||
arr[3] = _reserved;
|
||||
|
||||
var bytesAddr = Common.NumberProcessor.NumberToBytes(address, 4).Value;
|
||||
Array.Copy(bytesAddr, 0, arr, 4, bytesAddr.Length);
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
SendAddrPackOptions opts;
|
||||
opts.burstType = (BurstType)(commandType >> 5);
|
||||
opts.commandID = Convert.ToByte((commandType >> 3) & 0b0011);
|
||||
opts.isWrite = Convert.ToBoolean(commandType & 0x01);
|
||||
opts.burstLength = burstLength;
|
||||
opts.address = address;
|
||||
|
||||
return JsonConvert.SerializeObject(opts);
|
||||
}
|
||||
|
||||
public static Result<SendAddrPackage> FromBytes(byte[] bytes, bool checkSign = true)
|
||||
{
|
||||
if (bytes.Length != 8)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"Bytes are not equal to 8 bytes.",
|
||||
nameof(bytes)
|
||||
);
|
||||
}
|
||||
|
||||
if (checkSign && bytes[0] != (byte)PackSign.SendAddr)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"The sign of bytes is not SendAddr Package, but you can disable it by set checkSign false.",
|
||||
nameof(bytes)
|
||||
);
|
||||
}
|
||||
|
||||
var address = Common.NumberProcessor.BytesToNumber(bytes[4..]).Value;
|
||||
return new SendAddrPackage(bytes[1], bytes[2], Convert.ToUInt32(address));
|
||||
}
|
||||
}
|
||||
|
||||
public struct RecvRackage
|
||||
public struct SendDataPackage
|
||||
{
|
||||
readonly byte sign = (byte)PackSign.SendData;
|
||||
readonly byte[] _reserved = new byte[3];
|
||||
readonly byte[] bodyData;
|
||||
|
||||
public void toBytes()
|
||||
public SendDataPackage(byte[] bodyData)
|
||||
{
|
||||
this.bodyData = bodyData;
|
||||
}
|
||||
|
||||
public byte[] ToBytes()
|
||||
{
|
||||
var bodyDataLen = bodyData.Length;
|
||||
var arr = new byte[4 + bodyDataLen];
|
||||
|
||||
arr[0] = sign;
|
||||
|
||||
Array.Copy(bodyData, 0, arr, 4, bodyDataLen);
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public struct RecvDataPackage
|
||||
{
|
||||
readonly byte sign = (byte)PackSign.RecvData;
|
||||
readonly byte commandID;
|
||||
readonly byte resp;
|
||||
readonly byte _reserved = 0;
|
||||
readonly byte[] bodyData;
|
||||
|
||||
public RecvDataPackage(byte commandID, byte resp, byte[] bodyData)
|
||||
{
|
||||
this.commandID = commandID;
|
||||
this.resp = resp;
|
||||
this.bodyData = bodyData;
|
||||
}
|
||||
|
||||
public RecvPackOptions Options()
|
||||
{
|
||||
RecvPackOptions opts;
|
||||
opts.commandID = commandID;
|
||||
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? true : false);
|
||||
|
||||
return opts;
|
||||
}
|
||||
}
|
||||
|
||||
public struct RecvRespPackage
|
||||
{
|
||||
readonly byte sign = (byte)PackSign.RecvResp;
|
||||
readonly byte commandID;
|
||||
readonly byte resp;
|
||||
readonly byte _reserved = 0;
|
||||
|
||||
public RecvRespPackage(byte commandID, byte resp)
|
||||
{
|
||||
this.commandID = commandID;
|
||||
this.resp = resp;
|
||||
}
|
||||
|
||||
public RecvPackOptions Options()
|
||||
{
|
||||
RecvPackOptions opts;
|
||||
opts.commandID = commandID;
|
||||
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? true : false);
|
||||
|
||||
return opts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user