using HslCommunication; using HslCommunication.Profinet.Melsec; using HslCommunication.Profinet.Siemens; using log4net; using System; using System.Text; using System.Threading.Tasks; namespace Admin.Core.PlcServer { /// /// 西门子PLC /// public class SiemensPlc : IPlc { private readonly log4net.ILog log = LogManager.GetLogger(typeof(SiemensPlc)); private SiemensS7Net siemensS7Net; private SiemensPLCS siemensPLCS = SiemensPLCS.S1500; #region 构造函数 //public MelsecPlc() //{ // if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41")) // { // //log.Info("HslCommunication激活失败"); // return; // } // Console.WriteLine("HslCommunication激活成功!"); // siemensS7Net = new SiemensS7Net(); // siemensS7Net.ConnectTimeOut = 2000; //} public SiemensPlc(string iP, int port) { if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41")) { log.Error("HslCommunication激活失败"); return; } Console.WriteLine("HslCommunication激活成功!"); siemensS7Net = new SiemensS7Net(siemensPLCS); siemensS7Net.ConnectTimeOut = 2000; Connect(iP, port);//建立连接 } #endregion #region 注册 public bool Registed() { if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41")) { log.Error("HslCommunication激活失败"); return false; } log.Info("HslCommunication激活成功!"); siemensS7Net = new SiemensS7Net(siemensPLCS); siemensS7Net.ConnectTimeOut = 2000; return true; } #endregion #region 是否连接 /// /// 是否连接 /// public bool IsConnected { get; set; } #endregion #region 建立连接 /// /// 建立连接 /// /// /// /// public bool Connect(string iP, int port) { siemensS7Net.IpAddress = iP;//正式环境开启 siemensS7Net.Port = port; try { OperateResult connect = siemensS7Net.ConnectServer(); if (connect.IsSuccess) { IsConnected = true; return true; } else { siemensS7Net.StopAsync(); IsConnected = false; return false; } } catch (Exception ex) { log.Error($"PLCl连接失败!{ex.Message}"); IsConnected = false; return false; } } #endregion #region 断开连接 /// /// 断开连接 /// /// public bool DisConnect() { return siemensS7Net.ConnectClose().IsSuccess; } #endregion #region 读取byte数据 /// /// 读取byte数据 /// /// /// public byte[] ReadBytes(string address) { byte[] bytes = null; try { OperateResult read = siemensS7Net.Read(address, 26); log.Info($"ReadBytes方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { byte[] code = new byte[read.Content.Length - 2]; Array.Copy(read.Content, 2, code, 0, 24); string scode = Encoding.ASCII.GetString(code, 0, code.Length); bytes = code; } } catch (Exception ex) { log.Error("ReadBytes方法异常" + ex.ToString()); } return bytes; } #endregion #region 读取bool /// /// 读取bool /// /// /// public bool ReadBool(string address) { bool iflag = false; try { OperateResult read = siemensS7Net.ReadBool(address); log.Info($"ReadBool方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { iflag = read.Content; } return iflag; } catch (Exception ex) { log.Error("ReadBool方法异常" + ex.ToString()); } return iflag; } public async Task ReadBoolAsync(string address) { bool iflag = false; try { OperateResult read =await siemensS7Net.ReadBoolAsync(address); log.Info($"ReadBool方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { iflag = read.Content; } return iflag; } catch (Exception ex) { log.Error("ReadBool方法异常" + ex.ToString()); } return iflag; } #endregion #region 读取int16 /// /// 读取int16 /// /// /// public int ReadInt16(string address) { int returnflag = 0; try { OperateResult read = siemensS7Net.ReadInt16(address); log.Info($"ReadInt16方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { returnflag = read.Content; } } catch (Exception ex) { log.Error("ReadInt16方法异常" + ex.ToString()); } return returnflag; } #endregion #region 读取int32 /// /// 读取int32 /// /// /// public int ReadInt32(string address) { int returnflag = 0; try { OperateResult read = siemensS7Net.ReadInt32(address); log.Info($"ReadInt32方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { returnflag = read.Content; } } catch (Exception ex) { log.Error("ReadInt32方法异常" + ex.ToString()); } return returnflag; } #endregion #region 读取string /// /// 读取string /// /// /// public string ReadString(string address) { string returnflag = ""; try { OperateResult read = siemensS7Net.ReadString(address, 10); log.Info($"ReadString方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { returnflag = read.Content; } } catch (Exception ex) { log.Error("ReadString方法异常" + ex.ToString()); } return returnflag; } #endregion #region 读取Float /// /// 读取string /// /// /// public float ReadFloat(string address) { float flag = 0; try { OperateResult read = siemensS7Net.ReadFloat(address); log.Info($"ReadFloat方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { flag = read.Content; } } catch (Exception ex) { log.Error("ReadString方法异常" + ex.ToString()); } return flag; } #endregion #region 写入int16 /// /// 写入int16 /// /// /// /// public bool WriteInt16(string address, string value) { bool iflag = false; try { OperateResult write = siemensS7Net.Write(address, short.Parse(value)); //Task> operateResult = siemensS7Net.Wait(address, short.Parse(value)); log.Info($"WriteInt16方法是否成功:{write.IsSuccess};反馈:{write.Message}"); if (write.IsSuccess) { iflag = true; } else { iflag = false; } return iflag; } catch (Exception ex) { log.Error("WriteInt16方法异常" + ex.ToString()); return iflag; } } #endregion #region 写入int32 /// /// 写入int32 /// /// /// /// public bool WriteInt32(string address, int value) { bool iflag = false; try { OperateResult write = siemensS7Net.Write(address, value); log.Info($"WriteInt32方法是否成功:{write.IsSuccess};反馈:{write.Message}"); if (write.IsSuccess) { iflag = true; } else { iflag = false; } return iflag; } catch (Exception ex) { log.Error("WriteInt32方法异常" + ex.ToString()); return iflag; } } #endregion #region 写入string /// /// 写入string /// /// /// /// public bool WriteString(string address, string value) { bool iflag = false; try { OperateResult write = siemensS7Net.Write(address, value); log.Info($"WriteString方法是否成功:{write.IsSuccess};反馈:{write.Message}"); if (write.IsSuccess) { iflag = true; } else { iflag = false; } } catch (Exception ex) { log.Error("WriteString方法异常" + ex.ToString()); iflag = false; } return iflag; } #endregion #region 写入byte /// /// 写入byte /// /// /// /// public bool WriteByte(string address, byte[] bytes) { bool iflag = false; try { OperateResult write = siemensS7Net.Write(address, bytes); log.Info($"WriteByte方法是否成功:{write.IsSuccess};反馈:{write.Message}"); if (write.IsSuccess) { iflag = true; } else { iflag = false; } } catch (Exception ex) { log.Error("WriteByte方法异常" + ex.ToString()); iflag = false; } return iflag; } #endregion #region 写入Float /// /// 写入byte /// /// /// /// public bool WriteFloat(string address, float value) { bool iflag = false; try { OperateResult write = siemensS7Net.Write(address, value); log.Info($"WriteFloat方法是否成功:{write.IsSuccess};反馈:{write.Message}"); if (write.IsSuccess) { iflag = true; } else { iflag = false; } } catch (Exception ex) { log.Error("WriteByte方法异常" + ex.ToString()); iflag = false; } return iflag; } #endregion #region 写入double /// /// 写入byte /// /// /// /// public bool Writedouble(string address, double value) { bool iflag = false; try { OperateResult write = siemensS7Net.Write(address, value); log.Info($"Writedouble方法是否成功:{write.IsSuccess};反馈:{write.Message}"); if (write.IsSuccess) { iflag = true; } else { iflag = false; } } catch (Exception ex) { log.Error("WriteByte方法异常" + ex.ToString()); iflag = false; } return iflag; } #endregion #region 心跳使用 /// /// 心跳使用 /// /// /// public async Task Read(string address) { try { siemensS7Net.ReceiveTimeOut = 2000; OperateResult read = await siemensS7Net.ReadBoolAsync(address); //log.Info($"心跳方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { IsConnected = true; return true; } else { var k = read.ErrorCode < 0 ? false : true; if (k) { IsConnected = true; return true; } else { IsConnected = false; return false; } } } catch (Exception ex) { log.Error("ReadInt32方法异常" + ex.ToString()); } return false; } #endregion } }