基础类库积累--SiemensS7NetClient类
前言
这个类是针对S7协议的类,如果你是做上位机,和下位机(PLC)交互,可能会给你带来帮助。
代码
使用NuGet引入第三方库(HslCommunication)
HslCommunication
HslCommunication的简介:
HSL专注于解决工业网络间的复杂的数据通信问题,解决多个独立程序之间的数据通信,甚至是不同的操作系统,平台的网络通信问题, 可以用来构建各种各样的应用程序,下至通信小软件,上位机软件,CS架构的聊天系统,上至ERP系统,历史追述系统,生产管理系统,MES系统, 无论你的开发环境是Visual Studio, Visual Studio Code, IntelliJ IDEA, Eclipse, Labview, Android Studio等等
HslCommunication的使用:
/// <summary>
/// 西门子S7协议的PLC客户端操作类
/// </summary>
public class SiemensS7NetClient
{
/// <summary>
/// 获取西门子S7协议的客户端
/// </summary>
/// <param name="ip">下位机IP地址</param>
/// <returns>SiemensS7Net实例</returns>
public static SiemensS7Net GetInstance(string ip)
{
return new SiemensS7Net(SiemensPLCS.S1200, ip)
{
ConnectTimeOut = 200
};
}
/// <summary>
/// 获取下位机单点数据
/// </summary>
/// <param name="client">西门子S7协议的客户端实例</param>
/// <param name="dataType">数据类型</param>
/// <param name="block">数据块</param>
/// <param name="offset">偏移量</param>
/// <param name="value">输出参数:点位值(成功);</param>
/// <param name="errorMessage">输出参数:空字符(成功);错误信息(失败)</param>
/// <returns>返回是否获取成功</returns>
public static bool GetPlcSingleData(SiemensS7Net client, string dataType, string block, decimal offset, ref object value, ref string errorMessage)
{
bool result = false;
if (client != null)
{
if (dataType.ToLower() == "int")
{
var operateResult = client.ReadInt16(block + "." + offset);
if (operateResult.IsSuccess)
{
value = operateResult.Content;
errorMessage = "";
result = true;
}
else
{
errorMessage = operateResult.Message;
}
}
else if (dataType.ToLower() == "bool")
{
var operateResult = client.ReadBool(block + "." + offset);
if (operateResult.IsSuccess)
{
value = operateResult.Content;
result = true;
}
else
{
value = operateResult.Message;
}
}
else if (dataType.ToLower() == "usint")
{
var operateResult = client.ReadUInt16(block + "." + offset);
if (operateResult.IsSuccess)
{
value = operateResult.Content;
result = true;
}
else
{
value = operateResult.Message;
}
}
else if (dataType.ToLower() == "word")
{
var operateResult = client.Read(block + "." + offset, 2);
if (operateResult.IsSuccess)
{
value = operateResult.Content;
result = true;
}
else
{
value = operateResult.Message;
}
}
else if (dataType.ToLower() == "wstring")
{
var operateResult = client.Read(block + "." + offset, 254);
if (operateResult.IsSuccess)
{
value = operateResult.Content;
result = true;
}
else
{
value = operateResult.Message;
}
}
}
else
{
errorMessage = "连接下位机的客户端引擎未初始化";
}
return result;
}
/// <summary>
/// 设置下位机单点数据
/// </summary>
/// <param name="client">西门子S7协议的客户端实例</param>
/// <param name="dataType">数据类型</param>
/// <param name="block">数据块</param>
/// <param name="offset">偏移量</param>
/// <param name="value">写入的值</param>
/// <param name="errorMessage">输出参数:空字符(成功);错误信息(失败)</param>
/// <returns>返回是否设置成功</returns>
public static bool SetPlcSingleData(SiemensS7Net client, string dataType, string block, decimal offset, object value, ref string errorMessage)
{
bool result = false;
if (client != null)
{
if (dataType.ToLower() == "int")
{
var operateResult = client.Write(block + "." + offset, Convert.ToInt16(value));
if (operateResult.IsSuccess)
{
errorMessage = "";
result = true;
}
else
{
errorMessage = operateResult.Message;
}
}
else if (dataType.ToLower() == "bool")
{
var operateResult = client.Write(block + "." + offset, Convert.ToBoolean(value));
if (operateResult.IsSuccess)
{
result = true;
}
else
{
errorMessage = operateResult.Message;
}
}
else if (dataType.ToLower() == "usint")
{
var operateResult = client.Write(block + "." + offset, Convert.ToByte(value));
if (operateResult.IsSuccess)
{
errorMessage = "";
result = true;
}
else
{
errorMessage = operateResult.Message;
}
}
}
else
{
errorMessage = "连接下位机的客户端引擎未初始化";
}
return result;
}
/// <summary>
/// 获取下位机批量数据
/// </summary>
/// <param name="client">西门子S7协议的客户端实例</param>
/// <param name="startAddress">读取的起始地址(例如:DB1.46.0)</param>
/// <param name="length">读取数据的个数(1个数据2个字节)</param>
/// <param name="values">输出参数:点位值字节数组(成功);</param>
/// <param name="errorMessage">输出参数:空字符(成功);错误信息(失败)</param>
/// <returns></returns>
public static bool GetPlcBatchData(SiemensS7Net client, string startAddress, ushort length, ref byte[] values, ref string errorMessage)
{
bool result = false;
if (client != null)
{
OperateResult<byte[]> read = client.Read(startAddress, length);
{
if (read.IsSuccess)
{
values = read.Content;
errorMessage = "";
result = true;
}
else
{
errorMessage = read.Message;
}
}
}
else
{
errorMessage = "连接下位机的客户端引擎未初始化";
}
return result;
}
}
总结
其实我也是第一次使用到这个,可能做上位机的经常会使用到,它是一种协议,让下位机和上位机之间建立某一种契约,上位机能给下位机发送命令,下位机执行命令,下位机返回信息给上位机,上位机处理。上位机监听下位机的点位信息。