Commit 73a08e4b authored by 潘栩锋's avatar 潘栩锋 :bicyclist:

1.修复 Dev7E 线程安全优化

2.修复 flyad2021B2,连接断开恢复优化
parent 8593d013
...@@ -43,9 +43,9 @@ namespace GeneralGommunication ...@@ -43,9 +43,9 @@ namespace GeneralGommunication
/// <summary> /// <summary>
/// 有数据需要发送 /// 有数据需要发送
/// </summary> /// </summary>
public event SendDataEventHander SendMsgEvent; public event SendDataEventHandler SendMsgEvent;
public event TimeOutEventHandler TimeOutEvent;
...@@ -125,6 +125,8 @@ namespace GeneralGommunication ...@@ -125,6 +125,8 @@ namespace GeneralGommunication
{ {
//已经重试了3次,放弃 //已经重试了3次,放弃
ResetMsg(); ResetMsg();
//连接断开
TimeOutEvent?.Invoke(this);
return; return;
} }
else else
...@@ -140,25 +142,30 @@ namespace GeneralGommunication ...@@ -140,25 +142,30 @@ namespace GeneralGommunication
//IsConnected = true; //IsConnected = true;
csm.IncRec(recBuf.Count()); csm.IncRec(recBuf.Count());
for (int i = 0; i < recBuf.Count(); i++) List<byte[]> packs = new List<byte[]>();
lock (currPack)
{ {
if (recBuf[i] == 0x7e) for (int i = 0; i < recBuf.Count(); i++)
{ {
//找到头了 if (recBuf[i] == 0x7e)
//结束之前的包
if (currPack.Count > 0)
{ {
var pack = currPack.ToArray(); //找到头了
ParsePack(pack); //结束之前的包
currPack.Clear(); if (currPack.Count > 0)
csm.IncPack(1); {
var pack = currPack.ToArray();
packs.Add(pack);
currPack.Clear();
csm.IncPack(1);
}
} }
currPack.Add(recBuf[i]);
} }
currPack.Add(recBuf[i]);
} }
for (int i = 0; i < packs.Count(); i++) {
//OnPoll_TimeOut(); ParsePack(packs[i]);
}
} }
......
This diff is collapsed.
...@@ -28,7 +28,7 @@ namespace GeneralGommunication ...@@ -28,7 +28,7 @@ namespace GeneralGommunication
/// <summary> /// <summary>
/// 有数据需要发送 /// 有数据需要发送
/// </summary> /// </summary>
event SendDataEventHander SendMsgEvent; event SendDataEventHandler SendMsgEvent;
#region 模块运行接口 #region 模块运行接口
......
...@@ -21,7 +21,7 @@ namespace GeneralGommunication ...@@ -21,7 +21,7 @@ namespace GeneralGommunication
/// <summary> /// <summary>
/// 有数据需要发送 /// 有数据需要发送
/// </summary> /// </summary>
event SendDataEventHander SendMsgEvent; event SendDataEventHandler SendMsgEvent;
/// <summary> /// <summary>
/// 设备连接状态改变 /// 设备连接状态改变
......
...@@ -37,7 +37,7 @@ namespace GeneralGommunication ...@@ -37,7 +37,7 @@ namespace GeneralGommunication
/// <summary> /// <summary>
/// 有数据需要发送 /// 有数据需要发送
/// </summary> /// </summary>
public event SendDataEventHander SendMsgEvent; public event SendDataEventHandler SendMsgEvent;
public event DeviceConnectEventHander DeviceConnectEvent; public event DeviceConnectEventHander DeviceConnectEvent;
......
...@@ -36,7 +36,7 @@ namespace GeneralGommunication ...@@ -36,7 +36,7 @@ namespace GeneralGommunication
/// <summary> /// <summary>
/// 有数据需要发送 /// 有数据需要发送
/// </summary> /// </summary>
public event SendDataEventHander SendMsgEvent; public event SendDataEventHandler SendMsgEvent;
/// <summary> /// <summary>
/// 对于全部 有返回的函数调用,都使用Dispatcher,使线程同步 /// 对于全部 有返回的函数调用,都使用Dispatcher,使线程同步
......
...@@ -101,5 +101,8 @@ namespace GeneralGommunication ...@@ -101,5 +101,8 @@ namespace GeneralGommunication
public string errMsg; public string errMsg;
} }
public delegate void SendDataEventHander(object sender, byte[] data); public delegate void SendDataEventHandler(object sender, byte[] data);
public delegate void TimeOutEventHandler(object sender);
} }
...@@ -10,6 +10,7 @@ using System; ...@@ -10,6 +10,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
...@@ -276,6 +277,10 @@ namespace FlyADBase ...@@ -276,6 +277,10 @@ namespace FlyADBase
IsReadyContext isReadyContext = new IsReadyContext(); IsReadyContext isReadyContext = new IsReadyContext();
SysTickContext sysTickContext = new SysTickContext(); SysTickContext sysTickContext = new SysTickContext();
CalSpeed calSpeed = new CalSpeed(); CalSpeed calSpeed = new CalSpeed();
/// <summary>
/// 检查推送周期 10ms 有一个。 1秒内都没收到,应该断开了, 重连
/// </summary>
Stopwatch stopwatch_pushIntervalCheck = new Stopwatch();
public FlyAd2021B2Core Core => core; public FlyAd2021B2Core Core => core;
...@@ -334,6 +339,9 @@ namespace FlyADBase ...@@ -334,6 +339,9 @@ namespace FlyADBase
core.PushDataEvent += (sender, e) => core.PushDataEvent += (sender, e) =>
{ {
//喂狗
stopwatch_pushIntervalCheck.Restart();
//这个线程非主线程, 数据接收完,应该快速返回。 //这个线程非主线程, 数据接收完,应该快速返回。
//事件触发,都放在主线程操作。 //事件触发,都放在主线程操作。
//要有线程锁!!!!! //要有线程锁!!!!!
...@@ -434,6 +442,34 @@ namespace FlyADBase ...@@ -434,6 +442,34 @@ namespace FlyADBase
} }
}, TimeSpan.FromSeconds(1)); }, TimeSpan.FromSeconds(1));
PollModule.Current.Poll_Config(onPoll_CheckPush);
}
/// <summary>
/// 推送检查 ,没推送,就是连接断开
/// </summary>
private void onPoll_CheckPush()
{
if (!IsConnected)
{
if (stopwatch_pushIntervalCheck.IsRunning)
stopwatch_pushIntervalCheck.Stop();
return;
}
if (!stopwatch_pushIntervalCheck.IsRunning)
{
stopwatch_pushIntervalCheck.Restart();
}
else
{
if (stopwatch_pushIntervalCheck.ElapsedMilliseconds > 1000)
{
//1000ms 都没收到推送,连接断开
ReConnect();
}
}
} }
void GetRunResult() void GetRunResult()
...@@ -608,6 +644,8 @@ namespace FlyADBase ...@@ -608,6 +644,8 @@ namespace FlyADBase
} }
void _core_PushDataEvent(PushDataEventArgs e) { void _core_PushDataEvent(PushDataEventArgs e) {
Now = sysTickContext.ToDateTime(e.SysTick); Now = sysTickContext.ToDateTime(e.SysTick);
if (e.ENC1 != null) if (e.ENC1 != null)
...@@ -649,16 +687,6 @@ namespace FlyADBase ...@@ -649,16 +687,6 @@ namespace FlyADBase
advPushData( advPushData(
Now, e.AD, e.AD2); Now, e.AD, e.AD2);
} }
private void Core_PushDataEvent(object sender, PushDataEventArgs _e)
{
//这个线程非主线程, 数据接收完,应该快速返回。
//事件触发,都放在主线程操作。
//要有线程锁!!!!!
lock (pushEventArgs) {
pushEventArgs.Add(_e);
}
}
/// <summary> /// <summary>
/// 连接后初始化 /// 连接后初始化
...@@ -826,12 +854,20 @@ namespace FlyADBase ...@@ -826,12 +854,20 @@ namespace FlyADBase
} }
comm.DataReceived += Comm_DataReceived; comm.DataReceived += Comm_DataReceived;
comm.PropertyChanged += Comm_PropertyChanged; comm.PropertyChanged += Comm_PropertyChanged;
core.TimeOutEvent += Core_TimeOutEvent;
core.ResetMsg(); core.ResetMsg();
} }
private void Core_TimeOutEvent(object sender)
{
//回复超时,可能连接断开, 重新连接
ReConnect();
}
void disposeComm() void disposeComm()
{ {
comm.DataReceived -= Comm_DataReceived; comm.DataReceived -= Comm_DataReceived;
comm.PropertyChanged -= Comm_PropertyChanged; comm.PropertyChanged -= Comm_PropertyChanged;
core.TimeOutEvent -= Core_TimeOutEvent;
core.ResetMsg(); core.ResetMsg();
} }
private void Comm_DataReceived(IGeneralComm sender, byte[] msg) private void Comm_DataReceived(IGeneralComm sender, byte[] msg)
......
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