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
|
|
|
|
|
|
|
|
|
|
this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1")
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|