using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeneralGommunication
{
public interface IDev7E : INotifyPropertyChanged
{
///
/// 通讯速度测量模块
///
CommSpeedMeasuring Csm { get; }
///
/// 包出错次数
///
int ErrCnt { get; }
///
/// 连接成功;
/// 当命令多次发送失败,IsConnected = false
///
bool IsConnected { get; }
///
/// 有数据需要发送
///
event SendDataEventHandler SendMsgEvent;
#region 模块运行接口
///
/// 在接收事件中调用;
/// 全部接收事件 及 超时事件也是在这里触发
///
///
void RecMsg(byte[] recBuf);
///
/// 复位全部状态,通常由于通讯模块检测到连接断开导致的
///
void ResetMsg();
#endregion
}
///
/// 有数据需要发送
///
///
public delegate void SendMsgEventHander(object sender);
///
/// 异常不回复
///
///
///
public delegate void CallBackHandler(object asyncContext, object retData);
public class CallBackHandlerContext
{
public CallBackHandler asyncDelegate;
public object asyncContext;
}
public class CallBackAttribute : Attribute
{
public Type RetType;
public CallBackAttribute(Type type)
{
RetType = type;
}
}
///
/// 通知指令
///
public class COMMREQ
{
private byte[] prefix;
///
/// 指令 前缀
///
public byte[] Prefix
{
get { return prefix; }
set {
prefix = value;
updatePrefixString();
}
}
public string PrefixString { get; private set; }
void updatePrefixString()
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < Prefix.Count(); i++)
{
if (Prefix[i] >= 33 && Prefix[i] <= 126)
stringBuilder.Append((char)Prefix[i]);
else
stringBuilder.Append($" 0x{Prefix[i]:X2} ");
}
PrefixString = stringBuilder.ToString();
}
///
/// 回复数据 长度, 不含前序
///
public int ResponseLen;
///
/// 回复数据 总长度, 含前序
///
public int ResponseTotalLen => Prefix.Length + ResponseLen;
public delegate object ParseFuncPackHandler(byte[] pack, int dataIdx);
public ParseFuncPackHandler ParseFuncPack;
public delegate bool IsMatchHandler(COMMREQ commReq, byte[] pack, int dataIdx, object context, ref object retData);
public IsMatchHandler IsMatch;
public object IsMatchContext;
public static byte[] ToPrefix(params object[] commands)
{
List bytes = new List();
foreach (var command in commands)
{
if (command is string)
{
var c = (string)command;
foreach (var _c in c)
{
bytes.Add((byte)_c);
}
}
else if (command is char)
{
var c = (char)command;
bytes.Add((byte)c);
}
else if (command is byte)
{
var c = (byte)command;
bytes.Add(c);
}
else if (command is UInt16)
{
var c = (UInt16)command;
var bs = BitConverter.GetBytes(c);
bytes.AddRange(bs);
}
else if (command is UInt32)
{
var c = (UInt32)command;
var bs = BitConverter.GetBytes(c);
bytes.AddRange(bs);
}
else if (command is int)
{
var c = (int)command;
var bs = BitConverter.GetBytes(c);
bytes.AddRange(bs);
}
else if (command is byte[])
{
var c = (byte[])command;
bytes.AddRange(c);
}
else
{
throw new Exception("不支持该类型");
}
}
if (bytes.Count < 2)
throw new Exception("指令少于 2bytes");
return bytes.ToArray();
}
}
public abstract class COMMREQ_TransactionBase
{
///
/// 回复 callback
///
public CallBackHandler asyncDelegate;
///
/// 上下文
///
public object asyncContext;
}
///
/// 交易集合包
///
public class COMMREQ_TransactionMulti : COMMREQ_TransactionBase
{
///
/// 交易集合
///
public List transactions = new List();
}
public class COMMREQ_Transaction: COMMREQ_TransactionBase
{
///
/// 请求
///
public COMMREQ commReq;
///
/// 数据
///
public byte[] datas;
///
/// 数据的object 表达,只用于调试而已
///
public object datasObj;
///
/// 数据已经回复
///
public bool hasRet;
///
/// 回复的数据,只用于调试而已
///
public object retData;
}
}