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.

117 lines
4.3 KiB

6 months ago
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.ModbusTcpMaster;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkServices.Plc;
using Newtonsoft.Json.Linq;
6 months ago
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;
6 months ago
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);
6 months ago
}
/**
*
*/
private static void BatchReadPlc(ModbusTcpMaster master)
{
//这里不设置while。
int sn = 3; //(int)SessionMgr.GetAttrModbus(master.connectId, "eqm_sn");
6 months ago
PlcStaticInfo.PlcInfos.TryGetValue(sn, out PlcInfo plcInfo);
if (plcInfo != null)
{
var bytes01 = master.BatchRead(1, 115);
if (bytes01 != null)
6 months ago
{
ModbusDecoder.Decode<HostToPlc>(bytes01, plcInfo.hostToPlc);
6 months ago
}
var bytes02 = master.BatchRead(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;
6 months ago
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;
}
6 months ago
var bytes03 = master.BatchRead(701, 10);
if (bytes03 != null)
{
ModbusDecoder.Decode<PlcFault>(bytes03, plcInfo.plcFault);
}
//OperateResult<byte[]> result2 = ModbusTcpNet.Read("x=3;201", 222);
6 months ago
}
6 months ago
}
}
}