Commit 9a3e3595 authored by 潘栩锋's avatar 潘栩锋 🚴

GeneralGommunication 添加调试

FlyAd2021.B2 删除多余的机架修正
parent 0e52555c
......@@ -133,7 +133,7 @@ namespace GeneralGommunication
public void RecMsg(byte[] recBuf)
{
IsConnected = true;
//IsConnected = true;
csm.IncRec(recBuf.Count());
for (int i = 0; i < recBuf.Count(); i++)
......@@ -309,39 +309,12 @@ namespace GeneralGommunication
return;
}
IsConnected = true;
ParsePackAfterCheckCRC8(pack);
}
protected abstract void ParsePackAfterCheckCRC8(byte[] buf);
public string bytes2hexChar(byte[] pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
{
if (pack[i] >= 0x21 && pack[i] <= 0x7e)
{
//这个字符可以打印
sb.Append($"{pack[i]:X2}({(char)pack[i]}) ");
}
else {
sb.Append($"{pack[i]:X2}( ) ");
}
}
return sb.ToString();
}
public string bytes2hex(byte[] pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
{
sb.Append($"{pack[i]:X2} ");
}
return sb.ToString();
}
/// <summary>
/// 功能包解析
/// </summary>
......@@ -374,7 +347,7 @@ namespace GeneralGommunication
if (commReq.ResponseTotalLen + PrefixIndex > pack.Count())
{
//失败,指令长度不对!!
logger.Error($"ACK expect:{commReq.PrefixString} len={commReq.ResponseTotalLen + 1}, but reponse len ={pack.Count()} reponse: {bytes2hexChar(pack)}");
logger.Error($"ACK expect:{commReq.PrefixString} len={commReq.ResponseTotalLen + 1}, but reponse len ={pack.Count()} reponse: {pack.ToString_Bytes2HexAndAscii()}");
ErrCnt++;
return false;
}
......@@ -384,7 +357,7 @@ namespace GeneralGommunication
if (!commReq.IsMatch(commReq, pack, PrefixIndex, commReq.IsMatchContext, ref retData))
{
//回复对不上请求
logger.Error($"ACK expect:{commReq.PrefixString} len={commReq.ResponseTotalLen + 1}, but reponse len ={pack.Count()} reponse: {bytes2hexChar(pack)}");
logger.Error($"ACK expect:{commReq.PrefixString} len={commReq.ResponseTotalLen + 1}, but reponse len ={pack.Count()} reponse: {pack.ToString_Bytes2HexAndAscii()}");
ErrCnt++;
return false;
}
......@@ -394,7 +367,7 @@ namespace GeneralGommunication
if (!IsMatch(commReq, pack))
{
//回复对不上请求
logger.Error($"ACK expect:{commReq.PrefixString} len={commReq.ResponseTotalLen + 1}, but reponse len ={pack.Count()} reponse: {bytes2hexChar(pack)}");
logger.Error($"ACK expect:{commReq.PrefixString} len={commReq.ResponseTotalLen + 1}, but reponse len ={pack.Count()} reponse: {pack.ToString_Bytes2HexAndAscii()}");
ErrCnt++;
return false;
}
......@@ -502,7 +475,7 @@ namespace GeneralGommunication
if (commReq == null) {
//不能解析
logger.Error($"ACK multi reponse len ={pack.Count()} reponse: {bytes2hexChar(pack)}");
logger.Error($"ACK multi reponse len ={pack.Count()} reponse: {pack.ToString_Bytes2HexAndAscii()}");
ErrCnt++;
return false;
}
......
......@@ -76,6 +76,9 @@ namespace GeneralGommunication
/// <param name="recBuf"></param>
public void RecMsg(byte[] recBuf)
{
if (logger.IsDebugEnabled) {
logger.Debug($"REQ {recBuf.ToString_Bytes2Hex()}");
}
csm.IncRec(recBuf.Count());
for (int i = 0; i < recBuf.Count(); i++)
......@@ -110,6 +113,7 @@ namespace GeneralGommunication
Transactions.RemoveAt(0);
return GetSendPack(tran);
}
protected virtual byte[] GetSendPack(COMMREQInServer_Transaction tran)
......@@ -179,37 +183,6 @@ namespace GeneralGommunication
}
protected abstract void ParsePackAfterCheckCRC8(byte[] buf);
public string bytes2hexChar(byte[] pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
{
if (pack[i] >= 0x21 && pack[i] <= 0x7e)
{
//这个字符可以打印
sb.Append($"{pack[i]:X2}({(char)pack[i]}) ");
}
else
{
sb.Append($"{pack[i]:X2}( ) ");
}
}
return sb.ToString();
}
public string bytes2hex(byte[] pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
{
sb.Append($"{pack[i]:X2} ");
}
return sb.ToString();
}
/// <summary>
/// 功能包解析
......@@ -222,10 +195,18 @@ namespace GeneralGommunication
if (commReq == null)
{
ErrCnt++;
logger.Error($"ACK 收到不支持的请求");
return;
}
//还有B0
var response = commReq.ParseFuncPack(pack, commReq.Prefix.Count() + PrefixIndex);
if (logger.IsDebugEnabled) {
if(response!=null)
logger.Debug($"ACK {commReq.PrefixString} {response.ToString_Bytes2Hex()}");
else
logger.Debug($"ACK {commReq.PrefixString}");
}
AddTran(new COMMREQInServer_Transaction()
{
commReq = commReq,
......
......@@ -63,13 +63,29 @@ namespace GeneralGommunication
public event DeviceConnectEventHander DeviceConnectEvent;
/// <summary>
/// 通讯中
/// </summary>
public bool IsBusy
{
get
{
if (currTran != null)
return true;
if (Transactions.Count() > 0)
return true;
return false;
}
}
System.Timers.Timer timerTimeOut;//等待回复超时
public ModbusAsciiAsync()
{
Transactions = new List<Modbus_Transaction>();
timerTimeOut = new System.Timers.Timer();
timerTimeOut.Interval = 500;
timerTimeOut.Interval = 100;
timerTimeOut.AutoReset = false;
timerTimeOut.Elapsed += TimerTimeOut_Elapsed;
}
......@@ -112,6 +128,11 @@ namespace GeneralGommunication
currTran = null;
retryCnt = 0;
if (Transactions.Count() != 0) //还要其它需要发送
{
SendMsgEvent?.Invoke(this);
}
return;
}
else
......@@ -134,8 +155,8 @@ namespace GeneralGommunication
if (logger.IsDebugEnabled)
{
string msg = recBuf.Bytes2Hex();
string msg2 = recBuf.Hex2Ascii();
string msg = recBuf.ToString_Bytes2Hex();
string msg2 = recBuf.ToString_Bytes2Ascii();
logger.Debug($"ACK {msg} | {msg2}");
}
......@@ -193,8 +214,8 @@ namespace GeneralGommunication
//调试: 打印发送信息
if (logger.IsDebugEnabled)
{
string msg = tran.sendBuf.Bytes2Hex();
string msg_ascii = tran.sendBuf.Hex2Ascii();
string msg = tran.sendBuf.ToString_Bytes2Hex();
string msg_ascii = tran.sendBuf.ToString_Bytes2Ascii();
if (tran.desription == null)
logger.Debug($"REQ {msg} | {msg_ascii}");
else
......@@ -239,7 +260,7 @@ namespace GeneralGommunication
{
if (logger.IsErrorEnabled)
{
string msg = currPack.Hex2Ascii();
string msg = currPack.ToString_Bytes2Ascii();
logger.Error($"ACK ID:{currTran.deviceNo} 指令码:{currTran.func:X2} {errMsg} {msg}");
}
ErrCnt++;
......
......@@ -67,6 +67,20 @@ namespace GeneralGommunication
public event DeviceConnectEventHander DeviceConnectEvent;
/// <summary>
/// 通讯中
/// </summary>
public bool IsBusy
{
get {
if (currTran != null)
return true;
if (Transactions.Count() > 0)
return true;
return false;
}
}
System.Timers.Timer timer3d5t;//通讯包的 3.5T 时间间隔。 实际只是 15ms定时而已
System.Timers.Timer timerTimeOut;//等待回复超时
......@@ -75,7 +89,7 @@ namespace GeneralGommunication
Transactions = new List<Modbus_Transaction>();
timer3d5t = new System.Timers.Timer();
timer3d5t.Interval = 35;//设得再小也没有用。 它只是用系统的15ms定时器触发而已。
timer3d5t.Interval = 30;//设得再小也没有用。 它只是用系统的15ms定时器触发而已。
timer3d5t.AutoReset = false;//触发一次
timer3d5t.Elapsed += Timer3d5t_Elapsed;
......@@ -93,6 +107,7 @@ namespace GeneralGommunication
return false;
}
private void Timer3d5t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ParsePack();
......@@ -102,13 +117,13 @@ namespace GeneralGommunication
private void TimerTimeOut_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (currTran == null)//没有数据需要发送
if (currTran == null)//没有数据需要发送,异常
return;
//大于1秒也没回复,异常
//重试2次
retryCnt++;
logger.Error($"TIMEOUT 第{retryCnt}次超时");
if (retryCnt >= 2)
{
//已经发送了2次,放弃
......@@ -127,9 +142,13 @@ namespace GeneralGommunication
//删除发送给这个设备的全部指令
Transactions.RemoveAll(t => t.deviceNo == currTran.deviceNo);
currTran = null;
retryCnt = 0;
if (Transactions.Count() != 0) //还要其它需要发送
{
SendMsgEvent?.Invoke(this);
}
return;
}
else
......@@ -148,15 +167,16 @@ namespace GeneralGommunication
//停止超时检查
timerTimeOut.Stop();
//15ms后再处理。
timer3d5t.Stop();
csm.IncRec(recBuf.Count());
currPack.AddRange(recBuf);
if (logger.IsDebugEnabled) {
string msg = bytes2hex(recBuf);
string msg = recBuf.ToString_Bytes2Hex();
logger.Debug($"ACK {msg}");
}
//15ms后再处理。
timer3d5t.Stop();
timer3d5t.Start();
}
......@@ -177,6 +197,7 @@ namespace GeneralGommunication
currTran = Transactions.First();
Transactions.RemoveAt(0);
}
else
......@@ -191,7 +212,7 @@ namespace GeneralGommunication
//调试: 打印发送信息
if(logger.IsDebugEnabled)
{
string msg = bytes2hex(tran.sendBuf);
string msg = tran.sendBuf.ToString_Bytes2Hex();
if (tran.desription == null)
logger.Debug($"REQ {msg}");
else
......@@ -230,16 +251,6 @@ namespace GeneralGommunication
}
}
string bytes2hex(IEnumerable<byte> pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
{
sb.Append($"{pack.ElementAt(i):X2} ");
}
return sb.ToString();
}
/// <summary>
/// 已经收完一份数据,不会再有更多,包解析
/// </summary>
......@@ -273,7 +284,7 @@ namespace GeneralGommunication
if (currPack[1] != currTran.func)
{
logger.Error($"ACK 错误码:{currPack[1]} 异常码:{currPack[2]}");
logger.Error($"ACK 错误码:{currPack[1]:X2} 异常码:{currPack[2]:X2}");
ErrCnt++;
currPack.Clear();
TimerTimeOut_Elapsed(null, null);
......@@ -294,9 +305,8 @@ namespace GeneralGommunication
UInt16 rec_crc = currPack.ToUInt16_Big_Endian(currTran.expRecBytes - 2);
if (crc != rec_crc)
{
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 });
logger.Error($"ACK 指令码:{currTran.func:X2} CRC 校验出错 接收:{rec_crc:X4} 计算:{crc:X4}");
//DeviceConnectEvent?.Invoke(this, new DeviceConnectEventArgs() { deviceNo = currTran.deviceNo, isConnected = false, errMsg = errMsg });
ErrCnt++;
currPack.Clear();
TimerTimeOut_Elapsed(null, null);
......
This diff is collapsed.
......@@ -297,12 +297,13 @@ namespace GeneralGommunication
#region 调试输出
/// <summary>
/// 调试打印出16进制 例如 3A 30 31 31 30 30 30
/// </summary>
/// <param name="pack"></param>
/// <returns></returns>
public static string Bytes2Hex(this IEnumerable<byte> pack)
public static string ToString_Bytes2Hex(this IEnumerable<byte> pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
......@@ -316,7 +317,7 @@ namespace GeneralGommunication
/// </summary>
/// <param name="pack"></param>
/// <returns></returns>
public static string Hex2Ascii(this IEnumerable<byte> pack)
public static string ToString_Bytes2Ascii(this IEnumerable<byte> pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
......@@ -332,7 +333,26 @@ namespace GeneralGommunication
}
return sb.ToString();
}
public static string ToString_Bytes2HexAndAscii(this IEnumerable<byte> pack)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pack.Count(); i++)
{
byte b = pack.ElementAt(i);
if (b >= 0x20 && b <= 0x7E)
{
//这个字符可以打印
sb.Append($"{b:X2}({(char)b}) ");
}
else
{
sb.Append($"{b:X2}( ) ");
}
}
return sb.ToString();
}
#endregion
#region u16 to u32 or u16 to float
......
......@@ -16,7 +16,8 @@
<!--不合法颜色-->
<SolidColorBrush x:Key="Brushes.Highlight" Color="Red"/>
<SolidColorBrush x:Key="Brushes.Text.White" Color="White"/>
<SolidColorBrush x:Key="Brushes.Text.Gray" Color="#FFC3C3C3"/>
<!--随机颜色-->
<x:Array x:Key="Brushes.Random" Type="SolidColorBrush">
<SolidColorBrush>#2195f2</SolidColorBrush>
......
......@@ -218,7 +218,7 @@ namespace FlyADBase
/// <summary>
/// 系统剩余时间
/// </summary>
public int Surplus { get; private set; }
public int Surplus { get; private set; } = 10000;
/// <summary>
/// 系统授权码access[8]
/// </summary>
......@@ -331,7 +331,8 @@ namespace FlyADBase
if (msg == null)
return;
logger.Debug($"W {core.bytes2hex(msg)}");
//if(logger.IsDebugEnabled)
// logger.Debug($"W {msg.ToString_Bytes2HexAndAscii()}");
comm?.Write(msg);
};
......@@ -364,9 +365,6 @@ namespace FlyADBase
PollModule.Current.Poll_Config(core.OnPoll_TimeOut, TimeSpan.FromSeconds(0.5));
core.PropertyChanged += Core_PropertyChanged;
Misc.BindingOperations.SetBinding(core, nameof(core.IsConnected), this, nameof(IsConnected));
Misc.BindingOperations.SetBinding(isReadyContext, nameof(isReadyContext.IsReady), this, nameof(IsReady));
......@@ -516,6 +514,16 @@ namespace FlyADBase
}
private void FlyAd2021_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IsConnected))
{
if (IsConnected)
{
ConnectCnt++;
AfterConnected();
}
return;
}
if (bShieldSetValueEx)
return;
......@@ -648,19 +656,6 @@ namespace FlyADBase
}
}
private void Core_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(core.IsConnected))
{
if (core.IsConnected)
{
ConnectCnt++;
AfterConnected();
}
}
}
/// <summary>
/// 连接后初始化
......@@ -693,14 +688,14 @@ namespace FlyADBase
{
//参数不保存在设备中, 保存在电脑。
//参数写入到设备。
core.BuildMultiTrans(() =>
{
//core.BuildMultiTrans(() =>
//{
core.SetSysParam_MotorType(MotorType, null, null);
core.SetSysParam_Ratio01(Ratio01, null, null);
core.SetSysParam_Ratio02(Ratio02, null, null);
core.SetSysParam_Zero(PosOffset, null, null);
core.SetSysParam_Jog(JogVelocity, null, null);
});
//});
//NotifyPropertyChanged(nameof(MotorType));
//NotifyPropertyChanged(nameof(Ratio01));
//NotifyPropertyChanged(nameof(Ratio02));
......
......@@ -54,11 +54,6 @@ namespace FlyADBase
/// </summary>
public bool IsFinish { get; set; }
/// <summary>
/// 机架修正
/// </summary>
public CorrectADsHandler CorrectADs { get; set; }
/// <summary>
/// ad滞后修正 单位ms
/// </summary>
......@@ -96,12 +91,12 @@ namespace FlyADBase
DateTime lastGridTime;
Stopwatch stopwatch_gridTime = new Stopwatch();
SGrid fGrid = new SGrid();
SGrid fGrid1 = new SGrid();
SGrid fGrid2 = new SGrid();
void advConstructor()
{
fGrid.SetSize(GridLen);
fGrid1.SetSize(GridLen);
fGrid2.SetSize(GridLen);
mTimeGridAdvHelper.Init();
......@@ -143,7 +138,7 @@ namespace FlyADBase
else if ((e.PropertyName == nameof(PosLen)) ||
(e.PropertyName == nameof(PosOfGrid)))
{
fGrid.SetSize(GridLen);
fGrid1.SetSize(GridLen);
fGrid2.SetSize(GridLen);
}
}
......@@ -223,7 +218,7 @@ namespace FlyADBase
for (int i = 0; i < dat.Length; i++)
{
int grid_num = grid_start + i;
if (grid_num >= fGrid.data[index].Length)
if (grid_num >= fGrid1.data[index].Length)
break;
if (GridSmooth > 0)
......@@ -235,11 +230,11 @@ namespace FlyADBase
int idx = grid_num - GridSmooth + j;
if (idx < 0)
continue;
if (idx >= fGrid.data[index].Length)
if (idx >= fGrid1.data[index].Length)
break;
if (Misc.MyBase.ISVALIDATA(fGrid.data[index][idx]))
if (Misc.MyBase.ISVALIDATA(fGrid1.data[index][idx]))
{
sum += fGrid.data[index][idx];
sum += fGrid1.data[index][idx];
cnt++;
}
}
......@@ -250,11 +245,10 @@ namespace FlyADBase
}
else
{
dat[i] = fGrid.data[index][grid_num];
dat[i] = fGrid1.data[index][grid_num];
}
}
CorrectADs?.Invoke(direction, grid_start, dat);
return;
}
......@@ -265,7 +259,7 @@ namespace FlyADBase
/// <param name="dat">grid 数据</param>
public void GetGrid(Misc.DIRECTION direction, out int[] dat, out int[] dat2)
{
GetGrid(direction, 0, fGrid.data[0].Length, out dat, out dat2);
GetGrid(direction, 0, fGrid1.data[0].Length, out dat, out dat2);
}
#endregion
......@@ -314,7 +308,7 @@ namespace FlyADBase
/// <summary>
/// <para>ScanMotion 使用。</para>
/// <para>给定按扫描的时间点。当探头停下来后,获取数据。</para>
/// <para>数据带机架修正,滞后修正,带运动方向。</para>
/// <para>数据带滞后修正,带运动方向。</para>
/// </summary>
/// <param name="beginTime"></param>
/// <returns></returns>
......@@ -341,22 +335,7 @@ namespace FlyADBase
//数据滞后
TimeGridAdvHelperExt.AdLagCorr(dataList, ref endTime, ADLag, AD2Lag);
//机架修正
if (CorrectADs != null)
{
for (int i = 0;i<dataList.Count();i++)
{
int index = i;
int gridIdx = dataList[index].pos / PosOfGrid;
var data = dataList[index];
data.ad = CorrectAD(direction, gridIdx, data.ad);
data.ad2 = CorrectAD(direction, gridIdx, data.ad2);
dataList[index] = data;
}
}
TimeGridAdv2EventArgs eventArgs = new TimeGridAdv2EventArgs();
eventArgs.Direction = direction;
......@@ -470,13 +449,6 @@ namespace FlyADBase
if (grid_start >= GridLen)
return;
//机架修正
if (CorrectADs != null)
{
CorrectADs(direction, grid_start, gridsOfAd1);
CorrectADs(direction, grid_start, gridsOfAd2);
}
int index = (direction == Misc.DIRECTION.BACKWARD) ? 1 : 0;
......@@ -485,7 +457,7 @@ namespace FlyADBase
int len = grid_end - grid_start + 1;
var grid_data1 = fGrid.data[index];
var grid_data1 = fGrid1.data[index];
var grid_data2 = fGrid2.data[index];
Array.Copy(gridsOfAd1, 0, grid_data1, grid_start, len);
......@@ -575,7 +547,7 @@ namespace FlyADBase
//放入对应方向缓存区
int index = (direction == Misc.DIRECTION.BACKWARD) ? 1 : 0;
Array.Copy(gridsOfAd1, 0, fGrid.data[index], 0, GridLen);
Array.Copy(gridsOfAd1, 0, fGrid1.data[index], 0, GridLen);
Array.Copy(gridsOfAd2, 0, fGrid2.data[index], 0, GridLen);
......@@ -596,22 +568,6 @@ namespace FlyADBase
grid_start = 0
});
}
int CorrectAD(int gridIdx, int ad)
{
return CorrectAD(DIRECTION.FIX, gridIdx, ad);
}
int CorrectAD(Misc.DIRECTION direction, int gridIdx, int ad)
{
int[] d = new int[1];
d[0] = ad;
CorrectADs?.Invoke(direction, gridIdx, d);
return d[0];
}
}
class CalSpeed
......
......@@ -16,11 +16,6 @@ namespace FlyADBase
/// </summary>
public interface IFlyADClientAdv : IFlyADClient
{
/// <summary>
/// 机架修正
/// </summary>
CorrectADsHandler CorrectADs { get; set; }
/// <summary>
/// grid 滤波
/// </summary>
......
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