Commit 73a08e4b authored by 潘栩锋's avatar 潘栩锋 🚴

1.修复 Dev7E 线程安全优化

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