Files
sy_hx_pbl_backend/Infrastructure/PLC/PLCTool.cs

65 lines
1.6 KiB
C#
Raw Normal View History

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;
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();
public static SiemensS7Net siemensTcpNet = null;
2024-11-07 21:27:56 +08:00
public static bool ConnectPLC()
2024-11-07 20:17:39 +08:00
{
2024-11-07 21:49:54 +08:00
siemensTcpNet= new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1")
2024-11-07 20:17:39 +08:00
{
ConnectTimeOut = 5000
};
OperateResult connect = siemensTcpNet.ConnectServer();
if (connect.IsSuccess)
{
// 连接成功
Console.WriteLine("connect success");
return true;
}
else
{
// 连接失败,输出原因
Console.WriteLine("connect failed:" + connect.Message);
return false;
}
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>
public static bool WriteBit(string addr,bool value)
2024-11-07 21:27:56 +08:00
{
2024-11-08 08:42:33 +08:00
OperateResult write = siemensTcpNet.Write(addr, value);
return write.IsSuccess;
2024-11-07 20:17:39 +08:00
}
2024-11-07 21:27:56 +08:00
public static void ConnectClose()
2024-11-07 20:17:39 +08:00
{
siemensTcpNet.ConnectClose();
}
}
}