Sic.Framework-Nanjing-Baishi/MECF.Framework.RT.Equipment.../HardwareUnits/Robots/Sunway/SunwayRobotConnection.cs

100 lines
2.9 KiB
C#

using Aitex.Core.RT.Log;
using MECF.Framework.Common.Communications;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps.AE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.Sunway
{
public class SunwayRobotMessage : AsciiMessage
{
public string Data { get; set; }
public string ErrorText { get; set; }
}
public class SunwayRobotTCPConnection : TCPPortConnectionBase
{
private bool _isAckMode = false;//should read from config file
public SunwayRobotTCPConnection(string address, string endof)
: base(address, endof, true)
{
}
public override bool SendMessage(string message)
{
return base.SendMessage(message);
}
protected override void ActiveHandlerProceedMessage(MessageBase msg)
{
lock (_lockerActiveHandler)
{
if (_activeHandler != null)
{
if (msg.IsFormatError || !((SunwayRobotHandler)_activeHandler).HasResponse || (_activeHandler.HandleMessage(msg, out bool transactionComplete) && transactionComplete))
{
_activeHandler = null;
}
}
}
}
protected override MessageBase ParseResponse(string rawText)
{
SunwayRobotMessage msg = new SunwayRobotMessage();
msg.RawMessage = rawText;
if (rawText.Length <= 0)
{
LOG.Error($"empty response,");
msg.IsFormatError = true;
return msg;
}
rawText = rawText.Replace("\r", "").Replace("\n", "");
if (rawText.Contains("_ERR"))
{
msg.IsError = true;
var rewTextArray = rawText.Split(' ');
msg.Data = rewTextArray[1];
msg.IsComplete = true;
}
else if (rawText.Contains("_BG_RDY"))
{
if (!_isAckMode)
{
LOG.Error($"response format check failed, BG_RDY is not valid in Mode 1");
msg.IsFormatError = true;
return msg;
}
msg.Data = rawText;
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
}
else if (rawText.Contains("_RDY"))
{
msg.Data = rawText;
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
}
else
{
msg.Data = rawText;
msg.IsResponse = true;
msg.IsAck = true;
msg.IsComplete = true;
}
return msg;
}
}
}