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

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

2.添加 AD2021.B2 的 TimeGridAdvHelper 添加输入口的GetIStatusRange
parent d7368b50
...@@ -26,11 +26,7 @@ namespace GeneralGommunication ...@@ -26,11 +26,7 @@ namespace GeneralGommunication
/// 包出错次数 /// 包出错次数
/// </summary> /// </summary>
public int ErrCnt { get; protected set; } public int ErrCnt { get; protected set; }
/// <summary>
/// 连接成功;
/// 当命令多次发送失败,IsConnected = false
/// </summary>
public bool IsConnected { get; private set; }
/// <summary> /// <summary>
/// 通讯速度测量模块 /// 通讯速度测量模块
...@@ -67,6 +63,9 @@ namespace GeneralGommunication ...@@ -67,6 +63,9 @@ namespace GeneralGommunication
/// currTran 重发次数 /// currTran 重发次数
/// </summary> /// </summary>
int retryCnt = 0; int retryCnt = 0;
List<byte> deviceConnected = new List<byte>();
public event DeviceConnectEventHander DeviceConnectEvent;
System.Timers.Timer timer3d5t;//通讯包的 3.5T 时间间隔。 实际只是 15ms定时而已 System.Timers.Timer timer3d5t;//通讯包的 3.5T 时间间隔。 实际只是 15ms定时而已
...@@ -87,6 +86,13 @@ namespace GeneralGommunication ...@@ -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) private void Timer3d5t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ {
ParsePack(); ParsePack();
...@@ -96,24 +102,33 @@ namespace GeneralGommunication ...@@ -96,24 +102,33 @@ namespace GeneralGommunication
private void TimerTimeOut_Elapsed(object sender, System.Timers.ElapsedEventArgs e) private void TimerTimeOut_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ {
if (!IsConnected)
return;
if (currTran == null)//没有数据需要发送 if (currTran == null)//没有数据需要发送
return; return;
//大于1秒也没回复,异常 //大于1秒也没回复,异常
//重试3 //重试2
retryCnt++; retryCnt++;
if (retryCnt >= 3) if (retryCnt >= 2)
{ {
//已经重试了3次,放弃 //已经发送了2次,放弃
IsConnected = false; if (deviceConnected.Contains(currTran.deviceNo))
currTran = null; {
//之前连接是好的。
deviceConnected.Remove(currTran.deviceNo);
//清空 指令队列 //触发通知
Transactions.Clear(); DeviceConnectEvent?.Invoke(this, new DeviceConnectEventArgs()
{
deviceNo = currTran.deviceNo,
isConnected = false
});
}
//删除发送给这个设备的全部指令
Transactions.RemoveAll(t => t.deviceNo == currTran.deviceNo);
currTran = null;
return; return;
} }
else else
...@@ -129,8 +144,6 @@ namespace GeneralGommunication ...@@ -129,8 +144,6 @@ namespace GeneralGommunication
public void RecMsg(byte[] recBuf) public void RecMsg(byte[] recBuf)
{ {
IsConnected = true;
//停止超时检查 //停止超时检查
timerTimeOut.Stop(); timerTimeOut.Stop();
...@@ -197,12 +210,25 @@ namespace GeneralGommunication ...@@ -197,12 +210,25 @@ namespace GeneralGommunication
/// </summary> /// </summary>
public void ResetMsg() public void ResetMsg()
{ {
Transactions.Clear();
currTran = null; currTran = null;
timer3d5t.Stop(); timer3d5t.Stop();
timerTimeOut.Stop(); timerTimeOut.Stop();
csm.Reset(); 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) string bytes2hex(IEnumerable<byte> pack)
...@@ -378,7 +404,9 @@ namespace GeneralGommunication ...@@ -378,7 +404,9 @@ namespace GeneralGommunication
UInt16 rec_crc = currPack.ToUInt16_Little_Endian(currTran.expRecBytes - 2); UInt16 rec_crc = currPack.ToUInt16_Little_Endian(currTran.expRecBytes - 2);
if (crc != rec_crc) 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++; ErrCnt++;
currPack.Clear(); currPack.Clear();
TimerTimeOut_Elapsed(null, null); TimerTimeOut_Elapsed(null, null);
...@@ -390,6 +418,12 @@ namespace GeneralGommunication ...@@ -390,6 +418,12 @@ namespace GeneralGommunication
csm.IncPack(1); csm.IncPack(1);
currPack.Clear(); currPack.Clear();
//触发连接通知
if (!deviceConnected.Contains(currTran.deviceNo))
{
deviceConnected.Add(currTran.deviceNo);
DeviceConnectEvent?.Invoke(this, new DeviceConnectEventArgs() { deviceNo = currTran.deviceNo, isConnected = true });
}
switch (currTran.func) switch (currTran.func)
{ {
...@@ -635,4 +669,12 @@ namespace GeneralGommunication ...@@ -635,4 +669,12 @@ namespace GeneralGommunication
public object asyncContext; 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 ...@@ -50,12 +50,6 @@ namespace FlyADBase
/// </summary> /// </summary>
public DateTime BeResetTime { get; private set; } public DateTime BeResetTime { get; private set; }
/// <summary>
/// 当前在同步状态
/// </summary>
public bool IsSync { get; private set; }
/// <summary> /// <summary>
/// 只要connect成功,获取systick被复位,都会从AD盒设备读取参数; /// 只要connect成功,获取systick被复位,都会从AD盒设备读取参数;
/// 否则, 设置参数 到 AD盒 /// 否则, 设置参数 到 AD盒
...@@ -69,7 +63,7 @@ namespace FlyADBase ...@@ -69,7 +63,7 @@ namespace FlyADBase
/// AD盒地址, 当为 TCP通讯时,为 192.168.251.200:20020; /// AD盒地址, 当为 TCP通讯时,为 192.168.251.200:20020;
/// 当为 串口通讯时, 为 COM6 /// 当为 串口通讯时, 为 COM6
/// </summary> /// </summary>
public string Addr { get; set; } public string Addr { get; set; } = "192.168.251.10:20006";
/// <summary> /// <summary>
/// 从flyad7 获取 的systick 转换的时间 /// 从flyad7 获取 的systick 转换的时间
......
...@@ -54,11 +54,6 @@ namespace FlyADBase ...@@ -54,11 +54,6 @@ namespace FlyADBase
/// </summary> /// </summary>
DateTime BeResetTime { get; } DateTime BeResetTime { get; }
/// <summary>
/// 当前在同步状态
/// </summary>
bool IsSync { get; }
/// <summary> /// <summary>
/// 只要connect成功,获取systick被复位,都会从AD盒设备读取参数; /// 只要connect成功,获取systick被复位,都会从AD盒设备读取参数;
/// 否则, 设置参数 到 AD盒 /// 否则, 设置参数 到 AD盒
......
...@@ -816,6 +816,90 @@ namespace FlyADBase ...@@ -816,6 +816,90 @@ namespace FlyADBase
grids_cnt[grid_idx]++; grids_cnt[grid_idx]++;
grids_sum[grid_idx] += ad; 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 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