Product_Wms/WcsMain/EquipOperation/StackerConvey/StackerConveyOperation.cs

301 lines
11 KiB
C#

using System.Text.RegularExpressions;
using System.Text;
using WcsMain.Common;
using WcsMain.EquipOperation.Entity;
using WcsMain.EquipOperation.Entity.StackerConvey;
using WcsMain.Constant.WcsAttribute.AutoFacAttribute;
using WcsMain.Constant.Enum.Plc;
namespace WcsMain.EquipOperation.StackerConvey;
[Component]
public class StackerConveyOperation
{
/// <summary>
/// 读取输送机状态
/// </summary>
/// <param name="standId"></param>
/// <returns></returns>
public (string? errText, StackerConveyInfo? stackerConveyInfo) GetStackerConveyInfo(string standId)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
return ("设备尚未连接", default); // 未连接PLC
}
var readResult = CommonTool.Siemens.ReadByteWithName($"输送机状态反馈{standId}", 36);
if (!readResult.Success || readResult.Value == default)
{
return (readResult.Message, default); // 读取失败
}
var data = readResult.Value;
StackerConveyInfo stackerConveyInfo = new()
{
ConveyNo = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 0)), // 输送机编号
TaskType = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 2)), // 任务类型
StackerForbidden = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 4)), // 堆垛机动作禁止
PlcId = Convert.ToInt32(CommonTool.Siemens.Trans<int>(data, 6)), // 当前任务号
ControlModel = (StackerConveyModelEnum)Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 10)), // 控制方式
StackerConveyStatus = (StackerConveyStatusEnum)Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 12)), // 库前输送机状态
VehicleSize = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 14)), // 载具尺寸
Weight = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 16)), // 重量
SensorStatus = Convert.ToUInt32(CommonTool.Siemens.Trans<int>(data, 18)), // 传感器状态
ApplyCode = Convert.ToInt32(CommonTool.Siemens.Trans<int>(data, 22)), // 申请条码
RunningCode = Convert.ToInt32(CommonTool.Siemens.Trans<int>(data, 26)), // 正在运行的条码
ErrCode = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 30)), // 错误信息
Remark = Convert.ToInt32(CommonTool.Siemens.Trans<int>(data, 32)), // 备注
};
return (string.Empty, stackerConveyInfo);
}
/// <summary>
/// 获取提升机信息 ---- 上汽项目专用
/// </summary>
/// <param name="stackerConveyName"></param>
/// <returns></returns>
public (string errText, short model, short allowAction, short errCode, string code) GetLiftInfo(string stackerConveyName)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
return ("设备尚未连接", 0, 0, 999, ""); // 未连接PLC
}
var readResult = CommonTool.Siemens.ReadByteWithName($"提升机信息{stackerConveyName}", 26);
if (!readResult.Success || readResult.Value == default)
{
return (readResult.Message ?? "读取失败", 0, 0, 999, ""); // 读取失败
}
var data = readResult.Value;
short model = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 0));
short allowAction = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 2));
short errCode = Convert.ToInt16(CommonTool.Siemens.Trans<short>(data, 4));
var str = Encoding.ASCII.GetString(data, 6, 20);
string code = Regex.Replace(str, "\\W", "");
return (string.Empty, model, allowAction, errCode, code);
}
/// <summary>
/// 通知PLC放货完成
/// </summary>
/// <param name="location"></param>
/// <returns></returns>
public string? SetVehicelComplete(string location)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
return "设备尚未连接"; // 未连接PLC
}
(var writeResult, byte[]? data) = CommonTool.Siemens.WritePlcWhithName($"站台放货完成{location}", (short)1);
if (writeResult.Success)
{
return string.Empty;
}
return writeResult.Message;
}
/// <summary>
/// 获取输送机任务号
/// </summary>
/// <param name="conveyNo">输送机编号</param>
/// <returns></returns>
public int GetConveyPlcId(string? conveyNo)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return -1;
}
var readResult = CommonTool.Siemens.ReadInt32WithName($"输送机当前任务号{conveyNo}");
if (readResult.Success)
{
return readResult.Value;
}
return -1;
}
/// <summary>
/// 读取输送机条码
/// </summary>
/// <param name="conveyNo"></param>
/// <returns></returns>
public string? ReadConveyCode(string? conveyNo)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return default;
}
var readResult = CommonTool.Siemens.ReadInt32WithName($"输送机读码{conveyNo}");
if (readResult.Success)
{
return readResult.Value.ToString();
}
return default;
}
/// <summary>
/// 输送机卸货完成
/// </summary>
/// <param name="conveyNo"></param>
/// <returns></returns>
public string? SetConveyUnloadSuccess(string? conveyNo)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return "PLC尚未连接";
}
var writeResult = CommonTool.Siemens.WriteBoolWhithName($"输送机卸货完成{conveyNo}", true);
if (writeResult.Success)
{
return string.Empty;
}
return writeResult.Message;
}
/// <summary>
/// 获取输送机是否空闲
/// </summary>
/// <param name="conveyNo">输送机编号</param>
/// <returns></returns>
public int GetConveyIsEasy(string? conveyNo)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return -1;
}
var readResult = CommonTool.Siemens.ReadInt16WithName($"输送机空闲{conveyNo}");
if (readResult.Success)
{
return readResult.Value;
}
return -1;
}
/// <summary>
/// 获取输送机是否有货
/// </summary>
/// <param name="conveyNo">输送机编号</param>
/// <returns></returns>
public int GetConveyIsHaveGoods(string? conveyNo)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
// 未连接PLC
return -1;
}
var readResult = CommonTool.Siemens.ReadInt32WithName($"输送机是否有货{conveyNo}");
if (readResult.Success)
{
return readResult.Value;
}
return -1;
}
/// <summary>
/// 写入输送机任务,返回错误信息
/// </summary>
/// <param name="task"></param>
/// <returns></returns>
public string WriteTask(StackerConveyPlcTask task)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
return "PLC尚未连接。"; // 未连接PLC
}
var (writeResult, _) = CommonTool.Siemens.WritePlcWhithName($"输送机写任务{task.StackerConveyId}",
task.PlcId!,
task.TaskType!,
task.GetStand!,
task.InTunnelId!,
task.OutTunnelId!,
task.SetStand!,
task.GetQueue!,
task.GetLine!,
task.GetLayer!,
task.SetQueue!,
task.SetLine!,
task.SetLayer!,
task.GetDeep!,
task.SetDeep!,
task.Size!,
task.Weight!,
task.Code
);
return writeResult.Success ? string.Empty : "写入失败";
}
/// <summary>
/// 获取过账数据 ---- 地面站控制
/// </summary>
/// <param name="dataNum"></param>
/// <returns></returns>
public List<TaskFeedBackEntity>? GetConveyTaskFeedBackData(int dataNum)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
return default; // 未连接PLC
}
List<TaskFeedBackEntity> taskFeedBackEntities = [];
var readResult = CommonTool.Siemens.ReadByteWithName($"过账区", (ushort)(6 * dataNum));
if (!readResult.Success || readResult.Value == default) { return default; }
var readData = readResult.Value;
for (var i = 0; i < dataNum; i++)
{
TaskFeedBackEntity taskFeedBackEntity = new()
{
PlcId = Convert.ToInt32(CommonTool.Siemens.Trans<int>(readData, 0 + i * 6)),
FeedBackType = Convert.ToInt16(CommonTool.Siemens.Trans<short>(readData, 4 + i * 6)),
CountNo = i
};
taskFeedBackEntities.Add(taskFeedBackEntity);
}
return taskFeedBackEntities;
}
/// <summary>
/// 重置过账缓冲区 --- 写反馈区间接清除 ---- 地面站控制
/// </summary>
/// <param name="taskFeedBackData"></param>
public string? ResetFeedBackData(TaskFeedBackEntity taskFeedBackData)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
return "PLC 未连接"; // 未连接PLC
}
var dbAddress = CommonTool.Siemens.GetAddressWithNameAndBit($"过账区反馈区", 4 * taskFeedBackData.CountNo);
if (string.IsNullOrEmpty(dbAddress))
{
return "无法计算偏移量";
}
var writeResult = CommonTool.Siemens.WritePlcWhithAddress(1, dbAddress, taskFeedBackData.PlcId);
return writeResult.Success ? default : $"清除过账失败,异常信息:{writeResult.Message}";
}
/// <summary>
/// 重置过账缓冲区 --- 直接清除 ---- 地面站控制
/// </summary>
/// <param name="taskFeedBackData"></param>
public string? ClearFeedBackData(TaskFeedBackEntity taskFeedBackData)
{
if (!CommonData.IsConnectPlc || CommonTool.Siemens == default)
{
return "PLC 未连接"; // 未连接PLC
}
var dbAddress = CommonTool.Siemens.GetAddressWithNameAndBit($"过账区", 6 * taskFeedBackData.CountNo);
if (string.IsNullOrEmpty(dbAddress))
{
return "无法计算偏移量";
}
var writeResult = CommonTool.Siemens.WritePlcWhithAddress(1, dbAddress, 0);
return !writeResult.Success ? $"清除过账失败,异常信息:{writeResult.Message}" : default;
}
}