Commit d9583bc2 authored by 潘栩锋's avatar 潘栩锋 🚴

1.添加 ModbusRTUAsync 添加通讯状态推送

2.添加 AD2021.B2 的 TimeGridAdvHelper 添加输入口的GetIStatusRange
parent d7368b50
......@@ -26,12 +26,8 @@ namespace GeneralGommunication
/// 包出错次数
/// </summary>
public int ErrCnt { get; protected set; }
/// <summary>
/// 连接成功;
/// 当命令多次发送失败,IsConnected = false
/// </summary>
public bool IsConnected { get; private set; }
/// <summary>
/// 通讯速度测量模块
/// </summary>
......@@ -67,8 +63,11 @@ namespace GeneralGommunication
/// currTran 重发次数
/// </summary>
int retryCnt = 0;
List<byte> deviceConnected = new List<byte>();
public event DeviceConnectEventHander DeviceConnectEvent;
System.Timers.Timer timer3d5t;//通讯包的 3.5T 时间间隔。 实际只是 15ms定时而已
System.Timers.Timer timerTimeOut;//等待回复超时
public ModbusRTUAsync()
......@@ -87,6 +86,13 @@ namespace GeneralGommunication
}
public bool IsConnected(byte deviceNo)
{
if (deviceConnected.Contains(deviceNo))
return true;
return false;
}
private void Timer3d5t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ParsePack();
......@@ -96,24 +102,33 @@ namespace GeneralGommunication
private void TimerTimeOut_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!IsConnected)
return;
if (currTran == null)//没有数据需要发送
return;
//大于1秒也没回复,异常
//重试3
//重试2
retryCnt++;
if (retryCnt >= 3)
if (retryCnt >= 2)
{
//已经重试了3次,放弃
IsConnected = false;
currTran = null;
//已经发送了2次,放弃
if (deviceConnected.Contains(currTran.deviceNo))
{
//之前连接是好的。
deviceConnected.Remove(currTran.deviceNo);
//触发通知
DeviceConnectEvent?.Invoke(this, new DeviceConnectEventArgs()
{
deviceNo = currTran.deviceNo,
isConnected = false
});
}
//删除发送给这个设备的全部指令
Transactions.RemoveAll(t => t.deviceNo == currTran.deviceNo);
//清空 指令队列
Transactions.Clear();
currTran = null;
return;
}
else
......@@ -129,8 +144,6 @@ namespace GeneralGommunication
public void RecMsg(byte[] recBuf)
{
IsConnected = true;
//停止超时检查
timerTimeOut.Stop();
......@@ -197,12 +210,25 @@ namespace GeneralGommunication
/// </summary>
public void ResetMsg()
{
Transactions.Clear();
currTran = null;
timer3d5t.Stop();
timerTimeOut.Stop();
csm.Reset();
IsConnected = false;
Transactions.Clear();
//触发通知
var isConneced = deviceConnected.ToArray();
deviceConnected.Clear();
foreach (var deviceNo in isConneced)
{
//触发通知
DeviceConnectEvent?.Invoke(this, new DeviceConnectEventArgs()
{
deviceNo = deviceNo,
isConnected = false
});
}
}
string bytes2hex(IEnumerable<byte> pack)
......@@ -378,7 +404,9 @@ namespace GeneralGommunication
UInt16 rec_crc = currPack.ToUInt16_Little_Endian(currTran.expRecBytes - 2);
if (crc != rec_crc)
{
logger.Error($"ACK 指令码:{currTran.func:X2} CRC 校验出错 接收:{rec_crc:X4} 计算:{crc:X4}");
string errMsg = $"ACK 指令码:{currTran.func:X2} CRC 校验出错 接收:{rec_crc:X4} 计算:{crc:X4}";
logger.Error(errMsg);
DeviceConnectEvent?.Invoke(this, new DeviceConnectEventArgs() { deviceNo = currTran.deviceNo, isConnected = false, errMsg = errMsg });
ErrCnt++;
currPack.Clear();
TimerTimeOut_Elapsed(null, null);
......@@ -390,6 +418,12 @@ namespace GeneralGommunication
csm.IncPack(1);
currPack.Clear();
//触发连接通知
if (!deviceConnected.Contains(currTran.deviceNo))
{
deviceConnected.Add(currTran.deviceNo);
DeviceConnectEvent?.Invoke(this, new DeviceConnectEventArgs() { deviceNo = currTran.deviceNo, isConnected = true });
}
switch (currTran.func)
{
......@@ -635,4 +669,12 @@ namespace GeneralGommunication
public object asyncContext;
}
public delegate void DeviceConnectEventHander(object sender, DeviceConnectEventArgs e);
public class DeviceConnectEventArgs:EventArgs
{
public byte deviceNo;
public bool isConnected;
public string errMsg;
}
}
......@@ -50,12 +50,6 @@ namespace FlyADBase
/// </summary>
public DateTime BeResetTime { get; private set; }
/// <summary>
/// 当前在同步状态
/// </summary>
public bool IsSync { get; private set; }
/// <summary>
/// 只要connect成功,获取systick被复位,都会从AD盒设备读取参数;
/// 否则, 设置参数 到 AD盒
......@@ -69,7 +63,7 @@ namespace FlyADBase
/// AD盒地址, 当为 TCP通讯时,为 192.168.251.200:20020;
/// 当为 串口通讯时, 为 COM6
/// </summary>
public string Addr { get; set; }
public string Addr { get; set; } = "192.168.251.10:20006";
/// <summary>
/// 从flyad7 获取 的systick 转换的时间
......
......@@ -54,11 +54,6 @@ namespace FlyADBase
/// </summary>
DateTime BeResetTime { get; }
/// <summary>
/// 当前在同步状态
/// </summary>
bool IsSync { get; }
/// <summary>
/// 只要connect成功,获取systick被复位,都会从AD盒设备读取参数;
/// 否则, 设置参数 到 AD盒
......
......@@ -816,6 +816,90 @@ namespace FlyADBase
grids_cnt[grid_idx]++;
grids_sum[grid_idx] += ad;
}
/// <summary>
/// <para>data0s 的 输入口=false 序号范围</para>
/// <para>data1s 的 输入口=true 序号范围</para>
/// </summary>
/// <param name="dataPool">数据池</param>
/// <param name="newestTime">数据池最后一个数据时间</param>
/// <param name="istatusIndex">输入口序号</param>
/// <param name="beginTime">开始时间</param>
/// <param name="endTime">结束时间</param>
/// <param name="data0s">输入口=false 序号范围<</param>
/// <param name="data1s">输入口=true 序号范围</param>
public static void GetIStatusRange(
List<DateTimeUnit5> dataPool,
DateTime newestTime,
int istatusIndex,
DateTime beginTime, DateTime endTime,
out List<Range> data0s, out List<Range> data1s)
{
data0s = new List<Range>();
data1s = new List<Range>();
Range range = new Range();
bool is1 = false;
int begin_idx = dataPool.Count() - (int)((newestTime - beginTime).TotalMilliseconds);
if (begin_idx < 0)
begin_idx = 0;
if (begin_idx > dataPool.Count() - 1)
begin_idx = dataPool.Count() - 1;
int end_idx = dataPool.Count() - (int)((newestTime - endTime).TotalMilliseconds);
if (end_idx < 0)
end_idx = 0;
if (end_idx > dataPool.Count() - 1)
end_idx = dataPool.Count() - 1;
for (int i = begin_idx; i <= end_idx; i++)
{
var data = dataPool[i];
bool istatus_bit = Misc.MyBase.CHECKBIT(data.istatus, istatusIndex);
if (!range.IsValid)
{
//第1次
range.Begin = i;
range.End = i;
is1 = istatus_bit;
if (is1)
{
data1s.Add(range);
}
else
{
data0s.Add(range);
}
}
else
{
if (istatus_bit == is1)
{
//信号一样
range.End = i;
}
else {
//信号改变了
range = new Range();
range.Begin = i;
range.End = i;
is1 = istatus_bit;
if (is1)
{
data1s.Add(range);
}
else
{
data0s.Add(range);
}
}
}
}
}
}
public class Range_DateTime
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment