You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.6 KiB

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