|
|
using HybirdFrameworkDriver.ModbusTcpMaster;
|
|
|
using HybirdFrameworkServices.Plc;
|
|
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
|
|
using Service.Cloud.Msg.Cloud.Req;
|
|
|
using Service.Init.Entity;
|
|
|
|
|
|
namespace Service.Execute.Api;
|
|
|
|
|
|
public class PlcApi
|
|
|
{
|
|
|
public static HostToPlc hostToPlc = new HostToPlc();
|
|
|
public static PlcToHost plcToHost = new PlcToHost();
|
|
|
public static PlcFault plcFault = new PlcFault();
|
|
|
public static ModbusTcpMaster master;
|
|
|
|
|
|
/// <summary>
|
|
|
/// PLC是否连接
|
|
|
/// </summary>
|
|
|
public static bool con;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 数据有效时间。单位秒
|
|
|
/// </summary>
|
|
|
static int DataTimeSeconds = 2;
|
|
|
/// <summary>
|
|
|
/// 数据刷新时间
|
|
|
/// </summary>
|
|
|
public static DateTime DataValidityTime = DateTime.Now;
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 是否远程模式
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static bool IsRemote()
|
|
|
{
|
|
|
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
|
|
|
{
|
|
|
return plcToHost.RemoteLocalControlState.Value == 1010 ? true : false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 入口雷达检测
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static bool EntranceRadar()
|
|
|
{
|
|
|
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
|
|
|
{
|
|
|
return plcToHost.RadarStatesIn.Value == 1000 ? true : false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 出口雷达检测
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static bool ExitRadar()
|
|
|
{
|
|
|
if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds)
|
|
|
{
|
|
|
return plcToHost.RadarStatesOut.Value == 1000 ? true : false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 写入口灯
|
|
|
/// 红灯:1020
|
|
|
/// 绿灯:1000
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
/// <returns></returns>
|
|
|
public static bool WriteEntranceLamp(int data)
|
|
|
{
|
|
|
if (master != null)
|
|
|
{
|
|
|
HostToPlc writeHostToPlc = new HostToPlc();
|
|
|
writeHostToPlc.LightIn.Value = (ushort)data;
|
|
|
return master.WriteValue(writeHostToPlc.LightIn);
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 写出口灯
|
|
|
/// 红灯:1020
|
|
|
/// 绿灯:1000
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
/// <returns></returns>
|
|
|
public static bool WriteExistLamp(int data)
|
|
|
{
|
|
|
if (master != null)
|
|
|
{
|
|
|
HostToPlc writeHostToPlc = new HostToPlc();
|
|
|
writeHostToPlc.LightOut.Value = (ushort)data;
|
|
|
return master.WriteValue(writeHostToPlc.LightIn);
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 下发选包
|
|
|
/// </summary>
|
|
|
/// <param name="inBinNo">入仓位,仓位号</param>
|
|
|
/// <param name="outBinNo">出仓位,仓位号</param>
|
|
|
/// <returns></returns>
|
|
|
public static bool DistributeSelectPack(string inBinNo, string outBinNo)
|
|
|
{
|
|
|
if (master != null)
|
|
|
{
|
|
|
HostToPlc writeHostToPlc = new HostToPlc();
|
|
|
writeHostToPlc.EntrySelection.Value = Convert.ToUInt16(inBinNo);
|
|
|
writeHostToPlc.ExitSelection.Value = Convert.ToUInt16(outBinNo);
|
|
|
bool write1 = master.WriteValue(writeHostToPlc.EntrySelection);
|
|
|
bool write2 = master.WriteValue(writeHostToPlc.ExitSelection);
|
|
|
return write1 && write2;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 下发启动换电
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static bool StartSwapping()
|
|
|
{
|
|
|
if (master != null)
|
|
|
{
|
|
|
if (plcToHost.TaskType.Value == 0)//是否允许换电
|
|
|
return false;
|
|
|
|
|
|
HostToPlc writeHostToPlc = new HostToPlc();
|
|
|
writeHostToPlc.TaskType.Value = (ushort)1;
|
|
|
writeHostToPlc.TaskEnablement.Value = (ushort)1000;
|
|
|
bool write1 = master.WriteValue(writeHostToPlc.TaskType);
|
|
|
bool write2 = master.WriteValue(writeHostToPlc.TaskEnablement);
|
|
|
return write1 && write2;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查看拍照状态
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static byte ChannelStatus()
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 读plc任务状态
|
|
|
/// </summary>
|
|
|
/// <returns>6未连接</returns>
|
|
|
public static int ReadPlcTaskStatus()
|
|
|
{
|
|
|
if (master != null)
|
|
|
{
|
|
|
return plcToHost.TaskType.Value;
|
|
|
}
|
|
|
return (-1);
|
|
|
}
|
|
|
|
|
|
} |