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.
142 lines
7.9 KiB
142 lines
7.9 KiB
using HybirdFrameworkCore.Autofac.Attribute;
|
|
using HybirdFrameworkDriver.ModbusTcpMaster;
|
|
using HybirdFrameworkDriver.Session;
|
|
using HybirdFrameworkServices.Plc;
|
|
using Newtonsoft.Json.Linq;
|
|
using Service.Init.Entity;
|
|
|
|
namespace Service
|
|
{
|
|
[Scope("SingleInstance")]
|
|
public class EquipmentInit
|
|
{
|
|
/// <summary>
|
|
/// 连接所有数据
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public void Connect()
|
|
{
|
|
ConnectPlc();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 连接PLC
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public int ConnectPlc()
|
|
{
|
|
Thread thread = new Thread(() => ConnectPlcAsync());
|
|
thread.Start();
|
|
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 连接PLC
|
|
/// </summary>
|
|
private void ConnectPlcAsync()
|
|
{
|
|
bool connected = false;
|
|
|
|
ModbusTcpMaster master = null;
|
|
//PLC连接
|
|
while (!connected)
|
|
{
|
|
master = new ModbusTcpMaster()
|
|
{
|
|
Ip = "172.0.20.66",
|
|
Port = 502,
|
|
};
|
|
master.ReadAction = BatchReadPlc; //启动线程一直读
|
|
master.Duration = 3000;
|
|
connected = master.Connect();
|
|
|
|
if (connected)
|
|
{
|
|
PlcStaticInfo.PlcInfos.TryGetValue(3, out PlcInfo plcInfo);
|
|
plcInfo.Connected = true;
|
|
break;
|
|
}
|
|
|
|
Thread.Sleep(5000);
|
|
}
|
|
|
|
ModbusSession modbusSession = new ModbusSession(master);
|
|
master.connectId = master.Ip + master.Port;
|
|
SessionMgr.RegisterModbusSession(master.connectId, modbusSession);
|
|
SessionMgr.SetAttrModbus(modbusSession, "eqm_sn", 1);
|
|
PlcInfo waterColdInfo = new PlcInfo(master.connectId, 1);
|
|
PlcStaticInfo.PlcInfos.TryAdd(1, waterColdInfo);
|
|
}
|
|
|
|
/**
|
|
* 一直读
|
|
*/
|
|
private static void BatchReadPlc(ModbusTcpMaster master)
|
|
{
|
|
//这里不设置while。
|
|
int sn = 3; //(int)SessionMgr.GetAttrModbus(master.connectId, "eqm_sn");
|
|
|
|
PlcStaticInfo.PlcInfos.TryGetValue(sn, out PlcInfo plcInfo);
|
|
if (plcInfo != null)
|
|
{
|
|
var bytes01 = master.BatchReadHolderRegister(1, 115);
|
|
if (bytes01 != null)
|
|
{
|
|
ModbusDecoder.Decode<HostToPlc>(bytes01, plcInfo.hostToPlc);
|
|
}
|
|
|
|
var bytes02 = master.BatchReadHolderRegister(201, 222);
|
|
if (bytes02 != null)
|
|
{
|
|
ModbusDecoder.Decode<PlcToHost>(bytes02, plcInfo.plcToHost);
|
|
plcInfo.RemoteNot = plcInfo.plcToHost.RemoteLocalControlState.Value == 1010 ? true : false;
|
|
plcInfo.EntranceRadar = plcInfo.plcToHost.RadarStatesOut.Value == 1000 ? true : false;
|
|
plcInfo.ChannelLocationState = plcInfo.plcToHost.ChannelLocationState.Value == 1000 ? true : false;
|
|
|
|
plcInfo.BatteryDisassembly = plcInfo.plcToHost.TaskStates.Value == 1002 ? true : false;
|
|
plcInfo.StorageBatteries = plcInfo.plcToHost.TaskStates.Value == 1003 ? true : false;
|
|
plcInfo.BatteryDelivery = plcInfo.plcToHost.TaskStates.Value == 1004 ? true : false;
|
|
plcInfo.BatteryInstallation = plcInfo.plcToHost.TaskStates.Value == 1005 ? true : false;
|
|
plcInfo.BatteryInstallationComplete = plcInfo.plcToHost.TaskStates.Value == 1006 ? true : false;
|
|
plcInfo.SetExitLightGreen = (plcInfo.plcToHost.LightOut.Value == 1000 || plcInfo.plcToHost.LightOut.Value == 1010) ? true : false;
|
|
plcInfo.ExitRadarSensingTrigger = plcInfo.plcToHost.RadarStatesOut.Value == 1000 ? true : false;
|
|
|
|
|
|
#region 有料检测
|
|
plcInfo.MaterialDetec01 = plcInfo.plcToHost.MaterialDetecSwitchFault01.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec01.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec02 = plcInfo.plcToHost.MaterialDetecSwitchFault02.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec02.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec03 = plcInfo.plcToHost.MaterialDetecSwitchFault03.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec03.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec04 = plcInfo.plcToHost.MaterialDetecSwitchFault04.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec04.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec05 = plcInfo.plcToHost.MaterialDetecSwitchFault05.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec05.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec06 = plcInfo.plcToHost.MaterialDetecSwitchFault06.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec06.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec07 = plcInfo.plcToHost.MaterialDetecSwitchFault07.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec07.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec08 = plcInfo.plcToHost.MaterialDetecSwitchFault08.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec08.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec09 = plcInfo.plcToHost.MaterialDetecSwitchFault09.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec09.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec10 = plcInfo.plcToHost.MaterialDetecSwitchFault10.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec10.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec11 = plcInfo.plcToHost.MaterialDetecSwitchFault11.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec11.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec12 = plcInfo.plcToHost.MaterialDetecSwitchFault12.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec12.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec13 = plcInfo.plcToHost.MaterialDetecSwitchFault13.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec13.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec14 = plcInfo.plcToHost.MaterialDetecSwitchFault14.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec14.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec15 = plcInfo.plcToHost.MaterialDetecSwitchFault15.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec15.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec16 = plcInfo.plcToHost.MaterialDetecSwitchFault16.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec16.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec17 = plcInfo.plcToHost.MaterialDetecSwitchFault17.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec17.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec18 = plcInfo.plcToHost.MaterialDetecSwitchFault18.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec18.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec19 = plcInfo.plcToHost.MaterialDetecSwitchFault19.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec19.Value == true ? 1 : 0);
|
|
plcInfo.MaterialDetec20 = plcInfo.plcToHost.MaterialDetecSwitchFault20.Value == true ? (byte)2 : (byte)(plcInfo.plcToHost.MaterialDetec20.Value == true ? 1 : 0);
|
|
|
|
#endregion
|
|
}
|
|
|
|
var bytes03 = master.BatchReadHolderRegister(701, 10);
|
|
if (bytes03 != null)
|
|
{
|
|
ModbusDecoder.Decode<PlcFault>(bytes03, plcInfo.plcFault);
|
|
}
|
|
//OperateResult<byte[]> result2 = ModbusTcpNet.Read("x=3;201", 222);
|
|
}
|
|
|
|
}
|
|
}
|
|
} |