|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Khd.Core.Plc
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Plc帮助类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class StaticPlcHelper
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 进行高低位转换后,写入Plc地址
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="plc"> plc </param>
|
|
|
|
|
|
/// <param name="address"> 写入地址 </param>
|
|
|
|
|
|
/// <param name="valueString"> 值 </param>
|
|
|
|
|
|
/// <param name="len"> 长度 </param>
|
|
|
|
|
|
public static void WriteToPoint(this Plc.S7.Plc plc, string address, string? valueString, string? len)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (len == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("StaticPlcHelper Ex len is null");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (valueString == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("StaticPlcHelper Ex valueString is null");
|
|
|
|
|
|
}
|
|
|
|
|
|
var value = GetValue(len, valueString);
|
|
|
|
|
|
plc.Write(address, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 读取RFID
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="plc"> plc </param>
|
|
|
|
|
|
/// <param name="address"> 读取地址 </param>
|
|
|
|
|
|
/// <param name="len"> 长度 </param>
|
|
|
|
|
|
/// <returns> 读取到的RFID </returns>
|
|
|
|
|
|
public static string? ReadRFID(this Plc.S7.Plc plc, string address, int len = 36)
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] adds = address.Split(".");
|
|
|
|
|
|
int db = int.Parse(adds[0].Replace("DB", ""));
|
|
|
|
|
|
int startByteAdr = int.Parse(adds[1].Replace("DBX", ""));
|
|
|
|
|
|
var result = (byte[]?)plc.Read(S7.DataType.DataBlock, db, startByteAdr, S7.VarType.Byte, len);
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
string res = Encoding.UTF8.GetString(result).Replace("\0", "").Replace("$", "").Replace("\\u", "").Trim();
|
|
|
|
|
|
|
|
|
|
|
|
if (res.Contains("JYHB"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return res[res.IndexOf('J')..];
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(res.Contains("C"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return res[res.IndexOf('C')..];
|
|
|
|
|
|
}else
|
|
|
|
|
|
{
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 电气写入点位高低位转换
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="len">点位地址位长度</param>
|
|
|
|
|
|
/// <param name="value">写入数值</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static object GetValue(string? len, string? value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (len == "2")
|
|
|
|
|
|
{
|
|
|
|
|
|
return Convert.ToInt16(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (len == "4")
|
|
|
|
|
|
{
|
|
|
|
|
|
return Convert.ToInt32(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (len == "6")
|
|
|
|
|
|
{
|
|
|
|
|
|
return Convert.ToByte(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (len == "8")
|
|
|
|
|
|
{
|
|
|
|
|
|
return Convert.ToString(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (len == "10")
|
|
|
|
|
|
{
|
|
|
|
|
|
return Convert.ToBoolean(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|