2023-11-02 10:28:53 +08:00
|
|
|
|
using Aitex.Core.Util;
|
|
|
|
|
using MECF.Framework.Common.Communications;
|
2023-11-03 10:04:22 +08:00
|
|
|
|
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Sensors;
|
2023-11-02 10:28:53 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps
|
|
|
|
|
{
|
|
|
|
|
public class SensorTempConnection : SerialPortConnectionBase
|
|
|
|
|
{
|
|
|
|
|
private List<byte> _lstCacheBuffer = new List<byte>();
|
|
|
|
|
public SensorTempConnection(string portName) : base(portName, 115200, 8, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r", false)
|
|
|
|
|
{
|
|
|
|
|
deviceTimer = new DeviceTimer();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public DeviceTimer deviceTimer;
|
|
|
|
|
|
|
|
|
|
private int ErrorCount = 0;
|
|
|
|
|
public override bool SendMessage(byte[] message)
|
|
|
|
|
{
|
|
|
|
|
_lstCacheBuffer.Clear();//每次发送前清除接收区缓存
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return base.SendMessage(message);
|
|
|
|
|
}
|
|
|
|
|
protected override MessageBase ParseResponse(byte[] rawMessage)
|
|
|
|
|
{
|
2023-11-03 10:04:22 +08:00
|
|
|
|
|
2023-11-02 10:28:53 +08:00
|
|
|
|
_lstCacheBuffer.AddRange(rawMessage);//持续添加通讯缓存
|
|
|
|
|
//验证最后一位是不是结束符\r
|
|
|
|
|
if (rawMessage[rawMessage.Length - 1] != 13)//byte 13 是字符串\r,SerialPortConnectionBase通讯中添加结束符\r
|
2023-11-03 10:04:22 +08:00
|
|
|
|
{
|
|
|
|
|
var mes= IsTimeOut() ? ReceiveTempMessage(true, false, _lstCacheBuffer.ToArray()) : ReceiveTempMessage(false,false, _lstCacheBuffer.ToArray());
|
|
|
|
|
if (deviceTimer.IsIdle())
|
|
|
|
|
deviceTimer.Start(5000);
|
|
|
|
|
return mes;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 10:28:53 +08:00
|
|
|
|
|
|
|
|
|
Reset();//复位状态和计数
|
|
|
|
|
|
2023-11-03 10:04:22 +08:00
|
|
|
|
return ReceiveTempMessage(false,true, _lstCacheBuffer.ToArray());
|
2023-11-02 10:28:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsTimeOut()
|
|
|
|
|
{
|
|
|
|
|
if (deviceTimer.IsTimeout())
|
|
|
|
|
{
|
|
|
|
|
deviceTimer.Stop();
|
|
|
|
|
ErrorCount += 1;
|
2023-11-03 10:04:22 +08:00
|
|
|
|
if (ErrorCount >= 3)
|
2023-11-02 10:28:53 +08:00
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Reset()
|
|
|
|
|
{
|
|
|
|
|
ErrorCount = 0;
|
|
|
|
|
deviceTimer.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-03 10:04:22 +08:00
|
|
|
|
private BinaryMessage ReceiveTempMessage(bool isError, bool isComplete, byte[] bytes)
|
2023-11-02 10:28:53 +08:00
|
|
|
|
{
|
|
|
|
|
BinaryMessage msg = new BinaryMessage();
|
2023-11-03 10:04:22 +08:00
|
|
|
|
msg.IsError = isError;
|
|
|
|
|
msg.IsComplete = isComplete;
|
2023-11-02 10:28:53 +08:00
|
|
|
|
msg.RawMessage = bytes;
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|