using System; using HighWayIot.Log4net; using HslCommunication; using HslCommunication.Profinet.Melsec; namespace HighWayIot.Plc { public class PlcConnect { private static readonly Lazy lazy = new Lazy(() => new PlcConnect()); public static PlcConnect Instance { get { return lazy.Value; } } private static LogHelper logHelper = LogHelper.Instance; /// /// 静态懒加载MelsecMcNet1 /// private static readonly MelsecMcNet MelsecInstance1 = new PlcConnect().CreateAb("192.168.0.7"); /// /// 静态懒加载MelsecMcNet2 /// private static readonly MelsecMcNet MelsecInstance2 = new PlcConnect().CreateAb("192.168.0.8"); private PlcConnect() { } /// /// 初始化三菱的服务器 /// /// private MelsecMcNet CreateAb(string ip) { //string Ip = ; MelsecMcNet plc = new MelsecMcNet(); try { plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(ip, 2001) { ConnectTimeOut = 3000, // 连接超时时间,单位毫秒 SleepTime = 0, SocketKeepAliveTime = -1, IsPersistentConnection = true, }; var reslt = plc.ConnectServer(); logHelper.Info($"Plc连接 信息:[{reslt.Message}] 是否成功:[{reslt.IsSuccess.ToString()}] 错误代码:[{reslt.ErrorCode}]"); if (!reslt.IsSuccess) { logHelper.Info("链接失败:"+reslt.Message); } } catch (Exception ex) { logHelper.Error("初始化PLC服务器发生错误!", ex); } return plc; } /// /// plc1 是不是保持链接 /// public static bool IsConnect1 { get { if (MelsecInstance1 == null) return false; var result = MelsecInstance1.IpAddress; logHelper.Info($"PLCIP:[{result}]"); return !string.IsNullOrEmpty(result); } } /// /// plc2 是不是保持链接 /// public static bool IsConnect2 { get { if (MelsecInstance2 == null) return false; var result = MelsecInstance2.IpAddress; logHelper.Info($"PLCIP:[{result}]"); return !string.IsNullOrEmpty(result); } } /// /// PLC1写入数据 /// /// 地址 /// 值 /// 数据类型 /// public static OperateResult PlcWrite1(string address, object value, DataTypeEnum type) { var result = new OperateResult() { IsSuccess = false }; try { switch (type) { case DataTypeEnum.Bool: result = MelsecInstance1.Write(address, Convert.ToBoolean(value)); break; //case DataTypeEnum.Byte: // result = Instance.Write(address, Convert.ToByte(value)); // break; case DataTypeEnum.Int16: result = MelsecInstance1.Write(address, Convert.ToInt16(value)); break; case DataTypeEnum.UInt16: result = MelsecInstance1.Write(address, Convert.ToUInt16(value)); break; case DataTypeEnum.Int32: result = MelsecInstance1.Write(address, Convert.ToInt32(value)); break; case DataTypeEnum.UInt32: result = MelsecInstance1.Write(address, Convert.ToUInt32(value)); break; case DataTypeEnum.Int64: result = MelsecInstance1.Write(address, Convert.ToInt64(value)); break; case DataTypeEnum.UInt64: result = MelsecInstance1.Write(address, Convert.ToUInt64(value)); break; case DataTypeEnum.Float: result = MelsecInstance1.Write(address, Convert.ToSingle(value)); break; case DataTypeEnum.Double: result = MelsecInstance1.Write(address, Convert.ToDouble(value)); break; default: throw new ArgumentException($"Unsupported data type: {type}"); } logHelper.PlcLog($"Read address:[{address}] value:[{value}] type:[{type.ToString()}] result:[{result.IsSuccess}]"); } catch (Exception ex) { logHelper.Error("PLC写入数据发生错误!", ex); } return result; } /// /// PLC2写入数据 /// /// 地址 /// 值 /// 数据类型 /// public static OperateResult PlcWrite2(string address, object value, DataTypeEnum type) { var result = new OperateResult() { IsSuccess = false }; try { switch (type) { case DataTypeEnum.Bool: result = MelsecInstance2.Write(address, Convert.ToBoolean(value)); break; //case DataTypeEnum.Byte: // result = Instance.Write(address, Convert.ToByte(value)); // break; case DataTypeEnum.Int16: result = MelsecInstance2.Write(address, Convert.ToInt16(value)); break; case DataTypeEnum.UInt16: result = MelsecInstance2.Write(address, Convert.ToUInt16(value)); break; case DataTypeEnum.Int32: result = MelsecInstance2.Write(address, Convert.ToInt32(value)); break; case DataTypeEnum.UInt32: result = MelsecInstance2.Write(address, Convert.ToUInt32(value)); break; case DataTypeEnum.Int64: result = MelsecInstance2.Write(address, Convert.ToInt64(value)); break; case DataTypeEnum.UInt64: result = MelsecInstance2.Write(address, Convert.ToUInt64(value)); break; case DataTypeEnum.Float: result = MelsecInstance2.Write(address, Convert.ToSingle(value)); break; case DataTypeEnum.Double: result = MelsecInstance2.Write(address, Convert.ToDouble(value)); break; default: throw new ArgumentException($"Unsupported data type: {type}"); } logHelper.PlcLog($"Read address:[{address}] value:[{value}] type:[{type.ToString()}] result:[{result.IsSuccess}]"); } catch (Exception ex) { logHelper.Error("PLC写入数据发生错误!", ex); } return result; } /// /// 读取bool /// /// /// public static bool ReadBool1(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadBool(address); } catch (Exception ex) { logHelper.Error($"PLC读取Bool发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Int16 /// /// /// public static short ReadInt161(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadInt16(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取UInt16 /// /// /// public static ushort ReadUInt161(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadUInt16(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Int32 /// /// /// public static int ReadInt321(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadInt32(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取UInt32 /// /// /// public static uint ReadUInt321(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadUInt32(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Int64 /// /// /// public static long ReadInt641(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadInt64(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取UInt64 /// /// /// public static ulong ReadUInt641(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadUInt64(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Float /// /// /// public static float ReadFloat1(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadFloat(address); } catch (Exception ex) { logHelper.Error($"PLC读取Float发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Double /// /// /// public static double ReadDouble1(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance1.ReadDouble(address); } catch (Exception ex) { logHelper.Error($"PLC读取Double发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取bool /// /// /// public static bool ReadBool2(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadBool(address); } catch (Exception ex) { logHelper.Error($"PLC读取Bool发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Int16 /// /// /// public static short ReadInt162(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadInt16(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取UInt16 /// /// /// public static ushort ReadUInt162(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadUInt16(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Int32 /// /// /// public static int ReadInt322(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadInt32(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取UInt32 /// /// /// public static uint ReadUInt322(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadUInt32(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Int64 /// /// /// public static long ReadInt642(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadInt64(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取UInt64 /// /// /// public static ulong ReadUInt642(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadUInt64(address); } catch (Exception ex) { logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Float /// /// /// public static float ReadFloat2(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadFloat(address); } catch (Exception ex) { logHelper.Error($"PLC读取Float发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } /// /// 读取Double /// /// /// public static double ReadDouble2(string address) { OperateResult result = new OperateResult(); try { result = MelsecInstance2.ReadDouble(address); } catch (Exception ex) { logHelper.Error($"PLC读取Double发生错误!address:[{address}]", ex); return default; } if (!result.IsSuccess) { return default; } return result.Content; } } }