using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using HslCommunication; using HslCommunication.Profinet.Siemens; namespace LangGuan.Command.Extension { public class PlcHelper { /// /// 写数据 /// /// public Task WriteVal(string ip, string key) { try { using (var siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, ip) { ConnectTimeOut = 5000 }) { var con = siemensTcpNet.ConnectServer(); if (con.IsSuccess) { var result = siemensTcpNet.Write(key, (ushort)1); Console.WriteLine($"连接plc成功..{ip},写入key:{key},是否写入成功:{result.IsSuccess},msg:{result.Message}code:{result.ErrorCode}"); } } } catch (Exception ex) { Console.WriteLine($"连接plc失败..{ip},msg:{ex.Message}"); } return Task.CompletedTask; } public Task WriteVal(string ip, string key, int val) { try { using (var siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, ip) { ConnectTimeOut = 5000 }) { var con = siemensTcpNet.ConnectServer(); if (con.IsSuccess) { var result = siemensTcpNet.Write(key, (ushort)val); Console.WriteLine($"连接plc成功..{ip},写入key:{key},是否写入成功:{result.IsSuccess},msg:{result.Message}code:{result.ErrorCode}"); } } } catch (Exception ex) { Console.WriteLine($"连接plc失败..{ip},msg:{ex.Message}"); } return Task.CompletedTask; } /// /// SetVal /// /// /// /// /// public async Task SetVal(string ip, string key, bool val) { try { using (var siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, ip) { ConnectTimeOut = 5000 }) { var con = siemensTcpNet.ConnectServer(); if (con.IsSuccess) { var result = await siemensTcpNet.WriteAsync(key, val); Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}" + $"--- 连接plc成功..{ip},写key:{key},value:{val},是否写入成功:{result.IsSuccess}"); await siemensTcpNet.ConnectCloseAsync(); return result.IsSuccess; } } } catch (Exception ex) { Console.WriteLine($"连接plc失败..{ip},msg:{ex.Message}"); } return false; } /// /// GetVal /// /// /// /// /// public async Task GetVal(string ip, string key) { try { using (var siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, ip) { ConnectTimeOut = 5000 }) { var con = siemensTcpNet.ConnectServer(); if (con.IsSuccess) { var result = await siemensTcpNet.ReadBoolAsync(key); Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}" + $"连接plc成功..{ip},GetVal:{key},value:{result.Content}"); await siemensTcpNet.ConnectCloseAsync(); return result.Content; } } } catch (Exception ex) { Console.WriteLine($"连接plc失败..{ip},msg:{ex.Message}"); } return false; } public async Task GetAngleVal(string ip, string key) { try { using (var siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, ip) { ConnectTimeOut = 2000 }) { var con = siemensTcpNet.ConnectServer(); if (con.IsSuccess) { var uint_M100 = await siemensTcpNet.ReadUInt32Async(key); Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}" + $"连接plc成功..{ip},GetVal:{key},value:{uint_M100.Content }"); await siemensTcpNet.ConnectCloseAsync(); return (uint_M100.Content / 100).ToString(); } } } catch (Exception ex) { Console.WriteLine($"连接plc失败..{ip},msg:{ex.Message}"); } return ""; } /// /// 俯仰角度 /// /// /// /// public async Task GetAngleVal1(string ip, string key) { try { using (var siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, ip) { ConnectTimeOut = 2000 }) { string result = string.Empty; var con = siemensTcpNet.ConnectServer(); if (con.IsSuccess) { //var int_16 = await siemensTcpNet.ReadInt16Async(key); //var ushort_M100 = await siemensTcpNet.ReadUInt16Async(key); //var double_M100 = await siemensTcpNet.ReadDoubleAsync(key); //var float_M100 = await siemensTcpNet.ReadFloatAsync(key); if (key == "VD4030") { var uint_M100 = await siemensTcpNet.ReadInt32Async(key); result = (uint_M100.Content / 10.0).ToString(); } if (key == "VW4034") { var ushort_M100 = await siemensTcpNet.ReadUInt16Async(key); result = (ushort_M100.Content / 100.0).ToString(); } Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}" + $"连接plc成功..{ip},GetVal:{key},value:{result }"); await siemensTcpNet.ConnectCloseAsync(); return result; } } } catch (Exception ex) { Console.WriteLine($"连接plc失败..{ip},msg:{ex.Message}"); } return ""; } public async Task read(string ip, string key) { try { using (var siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, ip) { ConnectTimeOut = 5000 }) { var con = siemensTcpNet.ConnectServer(); if (con.IsSuccess) { var result = await siemensTcpNet.ReadUInt16Async(key); Console.WriteLine($"连接plc成功..{ip},读取key:{key},value{result.Content}"); return result.Content; } } } catch (Exception ex) { Console.WriteLine($"连接plc失败..{ip},msg:{ex.Message}"); } return false; } } }