78 lines
1.8 KiB
C#
78 lines
1.8 KiB
C#
using HslCommunication;
|
|
using HslCommunication.Profinet.Inovance;
|
|
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 PLCTool
|
|
{
|
|
|
|
|
|
// private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
public static SiemensS7Net siemensTcpNet = null;
|
|
public static bool ConnectPLC()
|
|
{
|
|
siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1")
|
|
{
|
|
ConnectTimeOut = 5000
|
|
};
|
|
OperateResult connect = siemensTcpNet.ConnectServer();
|
|
if (connect.IsSuccess)
|
|
{
|
|
// 连接成功
|
|
Console.WriteLine("connect success");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
// 连接失败,输出原因
|
|
Console.WriteLine("connect failed:" + connect.Message);
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
/// <summary>
|
|
/// 写入bit
|
|
/// </summary>
|
|
/// <param name="addr"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static 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 static bool ReadBit(string addr)
|
|
{
|
|
|
|
bool M100_7 = siemensTcpNet.ReadBool(addr).Content;
|
|
return M100_7;
|
|
}
|
|
|
|
|
|
|
|
public static void ConnectClose()
|
|
{
|
|
siemensTcpNet.ConnectClose();
|
|
|
|
}
|
|
|
|
}
|
|
}
|