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

1. 优化 让AD2021.B2继承于Dev7E

2. 优化 把ModbusRTUAsync 时间间隔设置为35ms
parent ba42501c
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GeneralGommunication
{
/// <summary>
/// 测量通讯速度模块
/// </summary>
public class CommSpeedMeasuring : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 通讯速度 测量中
/// </summary>
public bool IsMeasuring { get; private set; }
/// <summary>
/// 通讯速度 byte/s
/// </summary>
public int CommSpeed { get; private set; }
/// <summary>
/// 通讯速度 单位 pack/s
/// </summary>
public int PackSpeed { get; private set; }
int packCnt = 0;
int recCnt = 0;
Stopwatch stopwatch = new Stopwatch();
protected CancellationTokenSource cancellationTokenSource;
public void StartMeasure()
{
if (IsMeasuring)
return;
IsMeasuring = true;
Reset();
cancellationTokenSource = new CancellationTokenSource();
//启动线程,测量速度
Task.Factory.StartNew(MeasureTask, cancellationTokenSource.Token);
}
public void StopMeasure()
{
cancellationTokenSource.Cancel();
//停止线程
IsMeasuring = false;
}
void MeasureTask()
{
stopwatch.Start();
while (!cancellationTokenSource.IsCancellationRequested)
{
if (stopwatch.Elapsed > TimeSpan.FromSeconds(1))
{
int rcnt, pcnt;
double totalsec;
lock (this)
{
totalsec = stopwatch.Elapsed.TotalSeconds;
stopwatch.Restart();
rcnt = recCnt;
pcnt = packCnt;
recCnt = 0;
packCnt = 0;
}
//1秒均值
CommSpeed = (int)(rcnt / totalsec);
PackSpeed = (int)(pcnt / totalsec);
}
Task.Delay(100).Wait();
}
stopwatch.Stop();
}
/// <summary>
/// 异常时调用
/// </summary>
public void Reset()
{
lock (this)
{
recCnt = 0;
packCnt = 0;
}
}
/// <summary>
/// 需要在 RecMsg(byte[] recBuf) 中调用
/// </summary>
/// <param name="cnt"></param>
public void IncRec(int cnt)
{
if (!IsMeasuring)
return;
lock (this)
{
recCnt += cnt;
}
}
/// <summary>
/// 需要在 ParsePack(pack); 后调用
/// </summary>
/// <param name="cnt"></param>
public void IncPack(int cnt)
{
if (!IsMeasuring)
return;
lock (this)
{
packCnt += cnt;
}
}
}
}
......@@ -205,11 +205,6 @@ namespace GeneralGommunication
logger.Debug($"REQ {commReq.PrefixString}");
else
logger.Debug($"REQ {commReq.PrefixString} {Newtonsoft.Json.JsonConvert.SerializeObject(tran.datasObj)}");
//byte[] prefix = COMMREQ.ToPrefix("RCD", 0);
//if (prefix.SequenceEqual(commReq.Prefix))
//{
//}
//转为 7E格式
var buf = GetSendPack(pack);
......@@ -305,6 +300,7 @@ namespace GeneralGommunication
return;
}
//解析后的 datas 没有了 帧包装 7E
byte crc8 = Misc.CRC.CRC8(pack, 0, pack.Count() - 1);
if (pack.Last() != crc8)
{
......@@ -312,6 +308,7 @@ namespace GeneralGommunication
ErrCnt++;
return;
}
ParsePackAfterCheckCRC8(pack);
}
protected abstract void ParsePackAfterCheckCRC8(byte[] buf);
......@@ -329,7 +326,7 @@ namespace GeneralGommunication
/// 功能包解析
/// </summary>
/// <param name="datas"></param>
protected void ParseFuncPack(byte[] pack)
protected virtual void ParseFuncPack(byte[] pack)
{
if (currTran == null)
{
......@@ -576,8 +573,6 @@ namespace GeneralGommunication
/// <param name="datas"></param>
protected virtual List<byte> GetSendPack(List<byte> datas)
{
//datas.Insert(0, 0x80);//插入B0
//需要在后面添加CRC8
byte crc8 = Misc.CRC.CRC8(datas, 0, datas.Count());
datas.Add(crc8);
......@@ -645,116 +640,5 @@ namespace GeneralGommunication
}
}
/// <summary>
/// 测量通讯速度模块
/// </summary>
public class CommSpeedMeasuring : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 通讯速度 测量中
/// </summary>
public bool IsMeasuring { get; private set; }
/// <summary>
/// 通讯速度 byte/s
/// </summary>
public double CommSpeed { get; private set; }
/// <summary>
/// 通讯速度 单位 pack/s
/// </summary>
public double PackSpeed { get; private set; }
int packCnt = 0;
int recCnt = 0;
Stopwatch stopwatch = new Stopwatch();
protected CancellationTokenSource cancellationTokenSource;
public void StartMeasure()
{
if (IsMeasuring)
return;
IsMeasuring = true;
Reset();
cancellationTokenSource = new CancellationTokenSource();
//启动线程,测量速度
Task.Factory.StartNew(MeasureTask, cancellationTokenSource.Token);
}
public void StopMeasure()
{
cancellationTokenSource.Cancel();
//停止线程
IsMeasuring = false;
}
void MeasureTask()
{
stopwatch.Start();
while (!cancellationTokenSource.IsCancellationRequested)
{
if (stopwatch.Elapsed > TimeSpan.FromSeconds(1))
{
int rcnt, pcnt;
double totalsec;
lock (this)
{
totalsec = stopwatch.Elapsed.TotalSeconds;
stopwatch.Restart();
rcnt = recCnt;
pcnt = packCnt;
recCnt = 0;
packCnt = 0;
}
//1秒均值
CommSpeed = rcnt / totalsec;
PackSpeed = pcnt / totalsec;
}
Task.Delay(100).Wait();
}
stopwatch.Stop();
}
/// <summary>
/// 异常时调用
/// </summary>
public void Reset()
{
lock (this)
{
recCnt = 0;
packCnt = 0;
}
}
/// <summary>
/// 需要在 RecMsg(byte[] recBuf) 中调用
/// </summary>
/// <param name="cnt"></param>
public void IncRec(int cnt)
{
if (!IsMeasuring)
return;
lock (this)
{
recCnt += cnt;
}
}
/// <summary>
/// 需要在 ParsePack(pack); 后调用
/// </summary>
/// <param name="cnt"></param>
public void IncPack(int cnt)
{
if (!IsMeasuring)
return;
lock (this)
{
packCnt += cnt;
}
}
}
}
......@@ -42,6 +42,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="CommSpeedMeasuring.cs" />
<Compile Include="Dev7E.cs" />
<Compile Include="GComm_SerialPort.cs" />
<Compile Include="GComm_TcpClient.cs" />
......@@ -57,10 +58,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="NLog">
<Version>5.0.0</Version>
<Version>5.0.5</Version>
</PackageReference>
<PackageReference Include="PropertyChanged.Fody">
<Version>3.4.1</Version>
<Version>3.3.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
......@@ -69,5 +70,8 @@
<Name>Misc</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
......@@ -75,7 +75,7 @@ namespace GeneralGommunication
Transactions = new List<Modbus_Transaction>();
timer3d5t = new System.Timers.Timer();
timer3d5t.Interval = 15;//设得再小也没有用。 它只是用系统的15ms定时器触发而已。
timer3d5t.Interval = 35;//设得再小也没有用。 它只是用系统的15ms定时器触发而已。
timer3d5t.AutoReset = false;//触发一次
timer3d5t.Elapsed += Timer3d5t_Elapsed;
......@@ -129,6 +129,7 @@ namespace GeneralGommunication
Transactions.RemoveAll(t => t.deviceNo == currTran.deviceNo);
currTran = null;
retryCnt = 0;
return;
}
else
......@@ -151,7 +152,7 @@ namespace GeneralGommunication
currPack.AddRange(recBuf);
if (logger.IsDebugEnabled) {
string msg = bytes2hex(recBuf);
logger.Debug($"REC {msg}");
logger.Debug($"ACK {msg}");
}
//15ms后再处理。
......@@ -176,7 +177,6 @@ namespace GeneralGommunication
currTran = Transactions.First();
retryCnt = 0;
Transactions.RemoveAt(0);
}
else
......@@ -191,13 +191,11 @@ namespace GeneralGommunication
//调试: 打印发送信息
if(logger.IsDebugEnabled)
{
string msg = $"REQ N:{tran.deviceNo} F:{tran.func:00} A:{tran.addr} C:{tran.cnt} ";
if (tran.desription != null)
msg += tran.desription;
logger.Debug(msg);
//msg = bytes2hex(tran.sendBuf);
//logger.Debug($"SEND {msg}");
string msg = bytes2hex(tran.sendBuf);
if (tran.desription == null)
logger.Debug($"REQ {msg}");
else
logger.Debug($"REQ {tran.desription} {msg}");
}
//开始计时
timerTimeOut.Start();
......@@ -212,6 +210,7 @@ namespace GeneralGommunication
{
Transactions.Clear();
currTran = null;
retryCnt = 0;
timer3d5t.Stop();
timerTimeOut.Stop();
csm.Reset();
......
......@@ -57,7 +57,6 @@
<Compile Include="Inc\IFlyADClientAdv.cs" />
<Compile Include="Inc\IFlyAd2021Core.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProtocolCommon.cs" />
<Compile Include="TimeGridAdvHelper.cs" />
</ItemGroup>
<ItemGroup>
......
using System.Collections.Generic;
using System.Linq;
namespace FlyADBase
{
public static class ProtocolCommon
{
/// <summary>
/// 第0个byte是 7E
/// </summary>
/// <param name="pdu"></param>
/// <returns></returns>
public static bool Pdu2Data(IEnumerable<byte> pdu, out List<byte> datas)
{
datas = new List<byte>();
for (int i = 1; i < pdu.Count(); i++)
{
byte p = pdu.ElementAt(i);
if (p == 0x7D)
{
if (i + 1 < pdu.Count())
{
i++;
p = pdu.ElementAt(i);
//转义符
if (p == 0x5E)
{
datas.Add(0x7E);
}
else if (p == 0x5D)
{
datas.Add(0x7D);
}
else
{
//异常,不能转义
return false;
}
}
else
{
//异常,没有转义信息
return false;
}
}
else
{
datas.Add(p);
}
}
return true;
}
/// <summary>
/// 第0个byte是 7E
/// </summary>
/// <param name="pdu"></param>
/// <returns></returns>
public static bool Data2Pdu(IEnumerable<byte> datas, out List<byte> pdu)
{
pdu = new List<byte>();
pdu.Add(0x7E);
for (int i = 0; i < datas.Count(); i++)
{
byte b = datas.ElementAt(i);
if (b == 0x7E)
{
pdu.Add(0x7D);
pdu.Add(0x5E);
}
else if (b == 0x7D)
{
pdu.Add(0x7D);
pdu.Add(0x5D);
}
else
{
pdu.Add(b);
}
}
return true;
}
}
}
......@@ -275,10 +275,8 @@ namespace FlyADBase
if (NewestTime == DateTime.MinValue)
return;
if (DataPool.Count() == 0)
return;
if (DataPool.Count() > (60 * 1.2) * 1000)
if (DataPool.Count() < (60 * 1.2) * 1000)
{
//比限定的多了0.2min 才会动作
return;
......
Subproject commit 3fd5bff0cbb36bfff39be5a8839295f43894af16
Subproject commit 4ed21f891f894ac67a2048af145b7ccc9261ed05
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