using HslCommunication.Profinet.Siemens; using Infrastructure; using MiniExcelLibs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DOAN.Infrastructure.PLC { public class PLCTool { // 私有连接对象 private SiemensS7Net siemensTcpNet = null; private readonly string plcAddress; public PLCTool() { plcAddress = AppSettings.GetConfig("PLCConfig:Address"); this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, plcAddress) { ConnectTimeOut = 5000 }; } /// /// 尝试连接到PLC /// /// 连接成功返回true,失败返回false public bool ConnectPLC() { try { var connect = siemensTcpNet.ConnectServer(); if (connect.IsSuccess) { Console.WriteLine($"PLC连接成功,地址{plcAddress}"); return true; } else { Console.WriteLine($"PLC连接失败,地址{plcAddress}: {connect.Message}"); return false; } } catch (Exception e) { Console.WriteLine($"PLC连接失败,地址{plcAddress}: {e.Message}"); return false; } } /// /// 向PLC写入单个bit值 /// /// PLC地址 /// 要写入的bool值 /// 写入成功返回true,失败返回false public bool WriteBit(string addr, bool value) { var write = siemensTcpNet.Write(addr, value); return write.IsSuccess; } /// /// 从PLC读取单个bit值 /// /// PLC地址 /// 读取到的bool值 public bool ReadBit(string addr) { return siemensTcpNet.ReadBool(addr).Content; } /// /// 从PLC读取多个连续的二进制数据并转为字节数组 /// /// 起始PLC地址,默认为"VB100" /// 要读取的字节数,默认为12 /// 读取到的字节数组,如果失败返回null public byte[] ReadAllValue(string addr = "VB100", ushort length = 12) { var result = siemensTcpNet.Read(addr, length); if (result.IsSuccess) { return result.Content; } else { Console.WriteLine($"PLC IO 取值失败,PLC地址{plcAddress},访问地址为{addr},地址个数为{length}"); return null; } } /// /// 关闭与PLC的连接 /// public void ConnectClose() { siemensTcpNet.ConnectClose(); } } }