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