using HslCommunication; using HslCommunication.Profinet.Beckhoff; using MaterialTraceability.Common; using MaterialTraceability.Plc; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MaterialTraceability.Plc.Impl { public class BeckhoffPlc: IPlc { BeckhoffAdsNet beckhoffAdsNet; public bool IsConnected { get; set; } public bool Connect(string IP,int port) { //string ip = o.ToString(); beckhoffAdsNet = new BeckhoffAdsNet(IP,48898); System.Net.IPAddress address; if (!System.Net.IPAddress.TryParse(IP, out address)) { return false; } try { OperateResult connect = beckhoffAdsNet.ConnectServer(); if (connect.IsSuccess) { this.IsConnected = true; return true; } else { this.IsConnected = false; return false; } } catch (Exception ex) { this.IsConnected = false; return false; } } public int readInt32ByAddress(string address) { int returnflag = 0; try { OperateResult read = beckhoffAdsNet.ReadInt32(address); if (read.IsSuccess) { returnflag = read.Content; } } catch (Exception ex) { } return returnflag; } public bool writeInt32ByAddress(string address,int value) { LogHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, value)); try { OperateResult write = beckhoffAdsNet.Write(address, (Int16)value); if (write.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}成功", address, value)); return true; } LogHelper.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { LogHelper.Error(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex); this.IsConnected = false; return false; } } public bool DisConnect() { return beckhoffAdsNet.ConnectClose().IsSuccess; } public byte[] readValueByAddress(int len, string address) { try { OperateResult read = beckhoffAdsNet.Read(address, (ushort)(len)); if (read.IsSuccess) { byte[] result = StringChange.ConvertFloatToINt(read.Content); LogHelper.PlcLog(String.Format("通过地址和长度读取PLC数据成功:{0}", StringChange.bytesToHexStr(result, result.Length))); return result; } else { LogHelper.PlcLog("通过地址和长度读取PLC数据失败!!!"); this.IsConnected = false; return new byte[0]; } } catch (Exception ex) { LogHelper.Error("通过地址和长度读取PLC数据异常", ex); this.IsConnected = false; return new byte[0]; } } public bool writeInt16ByAddress(string address, int value) { throw new NotImplementedException(); } public bool writeValueByAddress(int value, string address) { try { OperateResult operateResult = beckhoffAdsNet.Write(address, Convert.ToInt32(value)); if (operateResult.IsSuccess) { LogHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value)); return true; } LogHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { LogHelper.Error("通过PLC地址写入int类型数据", ex); this.IsConnected = false; return false; } } public bool resetByAddress(string address, int len) { try { byte[] write = new byte[len * 2]; for (int i = 0; i < len * 2; i++) { write[i] = 0; } OperateResult operateResult = beckhoffAdsNet.Write(address, write); if (operateResult.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}清零数据成功", address)); return true; } LogHelper.PlcLog(String.Format("通过PLC地址{0}清零数据失败!!!", address)); return false; } catch (Exception ex) { LogHelper.Error(String.Format("通过PLC地址{0}清零数据异常", address), ex); return false; } } public string readEaByAddress(string address) { try { OperateResult read = beckhoffAdsNet.Read(address, (ushort)(8)); if (read.IsSuccess && read.Content != null) { string result = Convert.ToString(read.Content); LogHelper.PlcLog(String.Format("通过PLC地址{0}读取EA值成功:{1}", address, result)); return result; } else { LogHelper.PlcLog(String.Format("通过PLC地址{0}读取EA值失败!!!", address)); this.IsConnected = false; return ""; } } catch (Exception ex) { LogHelper.Error("通过PLC地址读取EA值异常", ex); this.IsConnected = false; return ""; } } public int readInteractiveSignal(string address) { try { OperateResult read = beckhoffAdsNet.ReadInt16(address); if (read.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content)); return read.Content; } LogHelper.PlcLog(String.Format("通过PLC地址{0}读取交互信号失败!!!", address)); this.IsConnected = false; return 0; } catch (Exception ex) { LogHelper.Error("通过PLC地址读取交互信号异常", ex); this.IsConnected = false; return 0; } } public string readStringByAddress(string address, ushort length) { try { OperateResult read = beckhoffAdsNet.ReadString(address, length); if (read.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}读取string类型数据成功:{1}", address, read.Content)); return read.Content; } LogHelper.PlcLog(String.Format("通过PLC地址{0}读取string类型数据失败!!!", address)); this.IsConnected = false; return ""; } catch (Exception ex) { LogHelper.Error("通过PLC地址读取int32类型数据异常", ex); return ""; } } public bool writeStringByAddress(string address, string value) { try { OperateResult operateResult = beckhoffAdsNet.Write(address, value); if (operateResult.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value)); return true; } LogHelper.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value)); //this.IsConnected = false; return false; } catch (Exception ex) { LogHelper.Error(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex); //this.IsConnected = false; return false; } } public bool readBoolByAddress(string address) { try { OperateResult read = beckhoffAdsNet.ReadBool(address); if (read.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据成功:{1}", address, read.Content)); return read.Content; } LogHelper.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据失败!!!", address)); this.IsConnected = false; return false; } catch (Exception ex) { LogHelper.Error("通过PLC地址读取int32类型数据异常", ex); this.IsConnected = false; return false; } } public bool writeBoolByAddress(string address, bool value) { try { OperateResult write = beckhoffAdsNet.Write(address, short.Parse(StringChange.ParseToInt(value ? "1" : "0").ToString())); if (write.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value)); return true; } LogHelper.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { LogHelper.Error(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex); this.IsConnected = false; return false; } } public bool writeDoubleByAddress(string address, int value) { try { OperateResult write = beckhoffAdsNet.Write(address, Convert.ToDouble(value)); if (write.IsSuccess) { LogHelper.PlcLog(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value)); return true; } LogHelper.PlcLog(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value)); return false; } catch (Exception ex) { LogHelper.Error(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex); return false; } } } }