jy-plc/Command/Extension/PlcHelper.cs

213 lines
8.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 写数据
/// </summary>
/// <param name="ip"></param>
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;
}
/// <summary>
/// SetVal
/// </summary>
/// <param name="ip"></param>
/// <param name="key"></param>
/// <param name="val"></param>
/// <returns></returns>
public async Task<bool> 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;
}
/// <summary>
/// GetVal
/// </summary>
/// <param name="ip"></param>
/// <param name="key"></param>
/// <param name="val"></param>
/// <returns></returns>
public async Task<bool> 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<string> 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 "";
}
/// <summary>
/// 俯仰角度
/// </summary>
/// <param name="ip"></param>
/// <param name="key"></param>
/// <returns></returns>
public async Task<string> 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<object> 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;
}
}
}