using EGFramework; using Newtonsoft.Json; public class RequestSerialPort : IRequest { public string SerialPort { set; get; } public string StringData { set; get; } public byte[] Data { set; get; } public RequestSerialPort(byte[] data) { this.Data = data; } public RequestSerialPort(ResponseSerialServer data) { this.Data = data.GetBytes(); } public string ToProtocolData() { return null; } public byte[] ToProtocolByteData() { return Data; } } public class ResponseSerialPort : IResponse { public byte[] Data { set; get; } public bool TrySetData(string protocolData, byte[] protocolBytes) { try { this.Data = protocolBytes; if(Data == null) { return false; } return true; } catch (System.Exception) { return false; // throw; } } } public class RequestSerialRevert : IRequest, IEGFramework { public string SerialPort { set; get; } public bool IsByte { set; get; } public string StringData { set; get; } public RequestSerialRevert(byte[] data,string serialPort) { this.StringData = data.ToStringByHex(); this.IsByte = true; this.SerialPort = serialPort; } public byte[] ToProtocolByteData() { return null; } public string ToProtocolData() { return JsonConvert.SerializeObject(this); } } public class ResponseSerialServer : IResponse, IEGFramework { public string SerialPort { set; get; } public bool IsByte { set; get; } public string StringData { set; get; } public int BaudRate { set; get; } private byte[] Data { set; get; } public bool TrySetData(string protocolData, byte[] protocolBytes) { try { if (!protocolData.StartsWith('{') && !protocolData.StartsWith('[')) { return false; } ResponseSerialServer data = JsonConvert.DeserializeObject(protocolData); if (data != null && data.SerialPort != null && data.SerialPort != "") { this.SerialPort = data.SerialPort; this.IsByte = data.IsByte; this.StringData = data.StringData; this.BaudRate = data.BaudRate; this.Data = data.StringData.ToHexByString(); return true; } else { return false; } } catch (System.Exception) { return false; } } public byte[] GetBytes() { return Data; } }