同步发送复位指令测试
This commit is contained in:
@ -14,6 +14,11 @@ namespace WCS.BLL.HardWare
|
|||||||
public class MXL4ShelfModule : IModuleBase
|
public class MXL4ShelfModule : IModuleBase
|
||||||
{
|
{
|
||||||
#region 协议
|
#region 协议
|
||||||
|
/// <summary>
|
||||||
|
/// 复位命令
|
||||||
|
/// </summary>
|
||||||
|
public byte[] ResetData = { 0xBE, 0x52, 0x54, 0x41, 0x52, 0x54, 0x0A, 0xED };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 进入入库模式
|
/// 进入入库模式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -66,10 +71,7 @@ namespace WCS.BLL.HardWare
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[] GoOutStockTakingModeData = { 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
public byte[] GoOutStockTakingModeData = { 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 复位命令
|
|
||||||
/// </summary>
|
|
||||||
public byte[] ResetData = { 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询当前电压值
|
/// 查询当前电压值
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -122,7 +124,7 @@ namespace WCS.BLL.HardWare
|
|||||||
.OrderBy(t => t.LightNumber)
|
.OrderBy(t => t.LightNumber)
|
||||||
.ToList();
|
.ToList();
|
||||||
char[] data = "0000000000000000".ToCharArray();
|
char[] data = "0000000000000000".ToCharArray();
|
||||||
|
|
||||||
//禁用
|
//禁用
|
||||||
char[] dataBan = "0000000000000000".ToCharArray();
|
char[] dataBan = "0000000000000000".ToCharArray();
|
||||||
|
|
||||||
|
@ -34,6 +34,9 @@ namespace WCS.BLL
|
|||||||
public ConcurrentDictionary<int, MessageDto> MessageList { get; set; } = new ConcurrentDictionary<int, MessageDto>();
|
public ConcurrentDictionary<int, MessageDto> MessageList { get; set; } = new ConcurrentDictionary<int, MessageDto>();
|
||||||
|
|
||||||
public TcpClient tcpClient { get; set; }
|
public TcpClient tcpClient { get; set; }
|
||||||
|
|
||||||
|
//同步发送等待客户端 等待上一条指令发送成功并收到响应之后才发送下一条指令
|
||||||
|
public IWaitingClient<TcpClient> waitClient;
|
||||||
|
|
||||||
public object receivdLockObject = new object();
|
public object receivdLockObject = new object();
|
||||||
public object sendLockObject = new object();
|
public object sendLockObject = new object();
|
||||||
@ -66,6 +69,8 @@ namespace WCS.BLL
|
|||||||
|
|
||||||
tcpClient = new TcpClient();
|
tcpClient = new TcpClient();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(BindIPHost))
|
if (string.IsNullOrEmpty(BindIPHost))
|
||||||
{
|
{
|
||||||
//载入配置
|
//载入配置
|
||||||
@ -83,6 +88,19 @@ namespace WCS.BLL
|
|||||||
//添加控制台日志注入
|
//添加控制台日志注入
|
||||||
a.AddConsoleLogger();
|
a.AddConsoleLogger();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
var waitClient = tcpClient.CreateWaitingClient(new WaitingOptions()
|
||||||
|
{
|
||||||
|
FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
|
||||||
|
{
|
||||||
|
if (response.Data.Length == 13)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -183,42 +201,42 @@ namespace WCS.BLL
|
|||||||
return EasyTask.CompletedTask;
|
return EasyTask.CompletedTask;
|
||||||
};
|
};
|
||||||
|
|
||||||
//配置数据重发机制
|
////配置数据重发机制
|
||||||
Task.Run(async () =>
|
//Task.Run(async () =>
|
||||||
{
|
//{
|
||||||
while (true)
|
// while (true)
|
||||||
{
|
// {
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
//TODO如果指令已发两次 则取消重发
|
// //TODO如果指令已发两次 则取消重发
|
||||||
await Task.Delay(100);
|
// await Task.Delay(100);
|
||||||
if (MessageList.Count > 0)
|
// if (MessageList.Count > 0)
|
||||||
{
|
// {
|
||||||
var failedMessage = MessageList.Where(t => t.Value.LastSendTime < DateTime.Now.AddSeconds(-1))
|
// var failedMessage = MessageList.Where(t => t.Value.LastSendTime < DateTime.Now.AddSeconds(-1))
|
||||||
.ToList();
|
// .ToList();
|
||||||
foreach (var message in failedMessage)
|
// foreach (var message in failedMessage)
|
||||||
{
|
// {
|
||||||
Logs.Write("【指令重发】" + BitConverter.ToString(message.Value.Message) + "指令超时1s未响应", LogsType.InstructionResend);
|
// Logs.Write("【指令重发】" + BitConverter.ToString(message.Value.Message) + "指令超时1s未响应", LogsType.InstructionResend);
|
||||||
}
|
// }
|
||||||
MessageList.RemoveWhen(t => t.Value.SendTimes >= 2);
|
// MessageList.RemoveWhen(t => t.Value.SendTimes >= 2);
|
||||||
foreach (var item in MessageList)
|
// foreach (var item in MessageList)
|
||||||
{
|
// {
|
||||||
if (item.Value.LastSendTime < DateTime.Now.AddSeconds(-1))
|
// if (item.Value.LastSendTime < DateTime.Now.AddSeconds(-1))
|
||||||
{
|
// {
|
||||||
|
|
||||||
Send(item.Value.Message);
|
// Send(item.Value.Message);
|
||||||
item.Value.SendTimes++;
|
// item.Value.SendTimes++;
|
||||||
item.Value.LastSendTime = DateTime.Now;
|
// item.Value.LastSendTime = DateTime.Now;
|
||||||
Logs.Write("【指令重发】" + BitConverter.ToString(item.Value.Message) + "已进行重发", LogsType.InstructionResend);
|
// Logs.Write("【指令重发】" + BitConverter.ToString(item.Value.Message) + "已进行重发", LogsType.InstructionResend);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
catch
|
// catch
|
||||||
{
|
// {
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
});
|
//});
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -273,6 +291,35 @@ namespace WCS.BLL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendThenReturn(byte[] message, bool IsReSend = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var boardId = (message[3] << 8) + message[4];
|
||||||
|
lock (sendLockObject)
|
||||||
|
{
|
||||||
|
var clientIpHost = tcpClient.IP + ":" + tcpClient.Port;
|
||||||
|
Logs.Write($"【同步等待发送{clientIpHost}】{BitConverter.ToString(message)}", LogsType.Instructions);
|
||||||
|
|
||||||
|
waitClient.SendThenReturn(message);
|
||||||
|
Thread.Sleep(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logs.Write("【同步等待发送指令时发生异常】" + ex.Message, LogsType.Instructions);
|
||||||
|
//因异常断连时(网线已经被断了) 手动重连一次
|
||||||
|
if (ex is NotConnectedException)
|
||||||
|
{
|
||||||
|
Task.Run(() =>
|
||||||
|
{
|
||||||
|
ReConnectAsync();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//生成协议明细
|
//生成协议明细
|
||||||
public byte[] GenerateMessage(int boardId, byte[] data)
|
public byte[] GenerateMessage(int boardId, byte[] data)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user