Files
sy_hx_pbl_backend/Infrastructure/PLC/PCLTool.cs

69 lines
1.7 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
{
public class PCLTool
{
// 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
}
public static int Write(string addr,int num)
{
2024-11-07 21:56:43 +08:00
OperateResult write = siemensTcpNet.Write(addr, (byte)num);
2024-11-07 21:27:56 +08:00
if (write.IsSuccess)
{
Console.WriteLine("Write [v100] success");
return 1;
}
else
{
Console.WriteLine("Write [v100] failed: " + write.Message);
return 0;
}
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();
}
}
}