158 lines
4.2 KiB
C#
158 lines
4.2 KiB
C#
using HslCommunication;
|
||
using HslCommunication.Profinet.Inovance;
|
||
using HslCommunication.Profinet.Siemens;
|
||
using Infrastructure;
|
||
using JinianNet.JNTemplate;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace DOAN.Infrastructure.PLC
|
||
{
|
||
public class PLCTool
|
||
{
|
||
|
||
|
||
// private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||
|
||
public SiemensS7Net siemensTcpNet = null;
|
||
public PLCTool()
|
||
{
|
||
|
||
this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1")
|
||
{
|
||
ConnectTimeOut = 5000
|
||
};
|
||
}
|
||
|
||
public bool ConnectPLC()
|
||
{
|
||
try
|
||
{
|
||
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);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Console.WriteLine("PLC连接失败:" + e.Message);
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
/// <summary>
|
||
/// 写入bit
|
||
/// </summary>
|
||
/// <param name="addr"></param>
|
||
/// <param name="value"></param>
|
||
/// <returns></returns>
|
||
public bool WriteBit(string addr, bool value)
|
||
{
|
||
|
||
OperateResult write = siemensTcpNet.Write(addr, value);
|
||
return write.IsSuccess;
|
||
|
||
}
|
||
/// <summary>
|
||
/// 读取bit
|
||
/// </summary>
|
||
/// <param name="addr"></param>
|
||
/// <returns></returns>
|
||
public bool ReadBit(string addr)
|
||
{
|
||
|
||
bool M100_7 = siemensTcpNet.ReadBool(addr).Content;
|
||
return M100_7;
|
||
}
|
||
|
||
/// <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;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
public void ConnectClose()
|
||
{
|
||
siemensTcpNet.ConnectClose();
|
||
|
||
}
|
||
|
||
}
|
||
}
|