2024-11-07 20:17:39 +08:00
|
|
|
|
using HslCommunication;
|
2024-11-07 21:27:56 +08:00
|
|
|
|
using HslCommunication.Profinet.Inovance;
|
2024-11-07 20:17:39 +08:00
|
|
|
|
using HslCommunication.Profinet.Siemens;
|
|
|
|
|
|
using Infrastructure;
|
2024-11-13 15:53:15 +08:00
|
|
|
|
using JinianNet.JNTemplate;
|
2024-11-07 20:17:39 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DOAN.Infrastructure.PLC
|
|
|
|
|
|
{
|
2024-11-08 08:42:33 +08:00
|
|
|
|
public class PLCTool
|
2024-11-07 20:17:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
2024-11-13 15:53:15 +08:00
|
|
|
|
public SiemensS7Net siemensTcpNet = null;
|
|
|
|
|
|
public PLCTool()
|
2024-11-07 20:17:39 +08:00
|
|
|
|
{
|
2024-11-13 15:53:15 +08:00
|
|
|
|
|
2025-01-20 11:38:01 +08:00
|
|
|
|
this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "10.72.82.242")
|
2024-11-07 20:17:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
ConnectTimeOut = 5000
|
2024-11-13 15:53:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool ConnectPLC()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2024-11-07 20:17:39 +08:00
|
|
|
|
{
|
2024-11-13 15:53:15 +08:00
|
|
|
|
OperateResult connect = siemensTcpNet.ConnectServer();
|
|
|
|
|
|
if (connect.IsSuccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 连接成功
|
|
|
|
|
|
Console.WriteLine("PLC连接成功");
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 连接失败,输出原因
|
|
|
|
|
|
Console.WriteLine("PLC连接失败:" + connect.Message);
|
|
|
|
|
|
throw new CustomException("connect failed:" + connect.Message);
|
|
|
|
|
|
}
|
2024-11-07 20:17:39 +08:00
|
|
|
|
}
|
2024-11-13 15:53:15 +08:00
|
|
|
|
catch (Exception e)
|
2024-11-07 20:17:39 +08:00
|
|
|
|
{
|
2024-11-13 15:53:15 +08:00
|
|
|
|
Console.WriteLine("PLC连接失败:" + e.Message);
|
2024-11-07 20:17:39 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-11-13 15:53:15 +08:00
|
|
|
|
|
2024-11-07 20:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-07 21:27:56 +08:00
|
|
|
|
}
|
2024-11-08 08:42:33 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 写入bit
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="addr"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-11-13 15:53:15 +08:00
|
|
|
|
public bool WriteBit(string addr, bool value)
|
2024-11-07 21:27:56 +08:00
|
|
|
|
{
|
2024-11-08 10:10:59 +08:00
|
|
|
|
|
2024-11-08 08:42:33 +08:00
|
|
|
|
OperateResult write = siemensTcpNet.Write(addr, value);
|
|
|
|
|
|
return write.IsSuccess;
|
2024-11-08 10:10:59 +08:00
|
|
|
|
|
2024-11-07 20:17:39 +08:00
|
|
|
|
}
|
2024-11-08 10:10:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 读取bit
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="addr"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-11-13 15:53:15 +08:00
|
|
|
|
public bool ReadBit(string addr)
|
2024-11-08 10:10:59 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
bool M100_7 = siemensTcpNet.ReadBool(addr).Content;
|
|
|
|
|
|
return M100_7;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 10:13:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 读多个连续的二进制数据 转为字节数组
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="addr"></param>
|
|
|
|
|
|
/// <param name="length"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
//public byte[,] ReadAllValue(string addr = "VB100", ushort length = 11)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// byte[,] data = new byte[length, 8];
|
|
|
|
|
|
// //需要自行解析,length为地址个数
|
|
|
|
|
|
// siemensTcpNet.ReadByte(addr);
|
|
|
|
|
|
// OperateResult<byte[]> result = siemensTcpNet.Read(addr, length);
|
|
|
|
|
|
// if (result.IsSuccess)
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
|
|
// if (result.Content.Length > 0)
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
|
|
// for (int i = 0; i < result.Content.Length; i++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
|
|
// int row = i / 8;
|
|
|
|
|
|
// int col = i % 8;
|
|
|
|
|
|
// data[row, col] = result.Content[i];
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Console.WriteLine($"PLC IO 取值失败,地址为{addr},地址个数为{length}");
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 读多个连续的二进制数据 转为字节数组
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="addr"></param>
|
|
|
|
|
|
/// <param name="length"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public byte[] ReadAllValue(string addr = "VB100", ushort length = 12)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
//需要自行解析,length为地址个数
|
|
|
|
|
|
siemensTcpNet.ReadByte(addr);
|
|
|
|
|
|
OperateResult<byte[]> result = siemensTcpNet.Read(addr, length);
|
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return result.Content;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"PLC IO 取值失败,地址为{addr},地址个数为{length}");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-08 10:10:59 +08:00
|
|
|
|
|
2024-11-07 20:17:39 +08:00
|
|
|
|
|
2024-11-13 15:53:15 +08:00
|
|
|
|
public void ConnectClose()
|
2024-11-07 20:17:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
siemensTcpNet.ConnectClose();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|