using HslCommunication; using HslCommunication.Profinet; using HslCommunication.Profinet.Melsec; using log4net; using System; using System.Text; using System.Threading.Tasks; namespace Aucma.Core.PLc { /// /// 三菱PLC /// public class MelsecPlc : IPlc { private readonly log4net.ILog log = LogManager.GetLogger(typeof(MelsecPlc)); private MelsecMcNet melsecMcNet = null; #region 构造函数 //public MelsecPlc() //{ // if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41")) // { // //log.Info("HslCommunication激活失败"); // return; // } // Console.WriteLine("HslCommunication激活成功!"); // melsecMcNet = new MelsecMcNet(); // melsecMcNet.ConnectTimeOut = 2000; //} public MelsecPlc(string iP, int port) { if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41")) { //log.Info("HslCommunication激活失败"); return; } Console.WriteLine("HslCommunication激活成功!"); melsecMcNet = new MelsecMcNet(); melsecMcNet.ConnectTimeOut = 2000; Connect(iP, port);//建立连接 } #endregion #region 注册 public bool Registed() { if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41")) { log.Info("HslCommunication激活失败"); return false; } Console.WriteLine("HslCommunication激活成功!"); melsecMcNet = new MelsecMcNet(); melsecMcNet.ConnectTimeOut = 2000; return true; } #endregion #region 是否连接 /// /// 是否连接 /// public bool IsConnected { get; set; } #endregion #region 建立连接 /// /// 建立连接 /// /// /// /// public bool Connect(string iP, int port) { melsecMcNet.IpAddress = iP;//正式环境开启 melsecMcNet.Port = port; // 如果网络不太理想,配置了两个端口,一个有问题,立即切换另一个的话,可以配置如下的代码 //melsecMcNet.GetPipeSocket().SetMultiPorts(new int[] { 6000, 6001 }); try { // 先关闭再重连,防止多次重连 melsecMcNet.ConnectClose(); OperateResult connect = melsecMcNet.ConnectServer(); if (connect.IsSuccess) { IsConnected = true; return true; } else { melsecMcNet.RemoteStopAsync(); melsecMcNet.ConnectCloseAsync(); IsConnected = false; Console.WriteLine("连接失败!"); return false; } } catch (Exception ex) { log.Error(ex.Message); IsConnected = false; return false; } } #endregion #region 断开连接 /// /// 断开连接 /// /// public bool DisConnect() { return melsecMcNet.ConnectClose().IsSuccess; } #endregion #region 读取byte数据 /// /// 读取byte数据 /// /// /// public byte[] ReadBytes(string address) { byte[] bytes = null; try { OperateResult read = melsecMcNet.Read(address, 26); 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 = melsecMcNet.ReadBool(address); 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 = melsecMcNet.ReadInt16(address); 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 = melsecMcNet.ReadInt32(address); 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 = melsecMcNet.ReadString(address, 10); if (read.IsSuccess) { returnflag = read.Content; } } catch (Exception ex) { log.Error("ReadString方法异常" + ex.ToString()); } return returnflag; } #endregion #region 读取Float /// /// 读取Float /// /// /// public float ReadFloat(string address) { float flag = 0; try { OperateResult read = melsecMcNet.ReadFloat(address); if (read.IsSuccess) { flag = read.Content; } } catch (Exception ex) { log.Error("ReadFloat方法异常" + ex.ToString()); } return flag; } #endregion #region 读取Double /// /// 读取Double /// /// /// public double ReadDouble(string address) { double flag = 0; try { OperateResult read = melsecMcNet.ReadDouble(address); if (read.IsSuccess) { flag = read.Content; } } catch (Exception ex) { log.Error("ReadDouble方法异常" + ex.ToString()); } return flag; } #endregion #region 写入int16 /// /// 写入int16 /// /// /// /// public bool WriteInt16(string address, string value) { bool iflag = false; try { OperateResult write = melsecMcNet.Write(address, short.Parse(value)); //Task> operateResult = melsecMcNet.Wait(address, short.Parse(value)); 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 = melsecMcNet.Write(address, value); 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 = melsecMcNet.Write(address, value); 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 = melsecMcNet.Write(address, bytes); 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 = melsecMcNet.Write(address, value); 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 { melsecMcNet.ReceiveTimeOut = 2000; OperateResult read = await melsecMcNet.ReadBoolAsync(address); 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 /// /// 读取byte /// /// 地址 /// 长度 /// public byte[] Read(string address, ushort len) { OperateResult readbyte = melsecMcNet.Read(address, len); if (readbyte.IsSuccess) { IsConnected = true; return readbyte.Content; } else { IsConnected = false; return null; } } } }