using S7.Net;
namespace BPASmartClient.S7Net
{
public class SiemensHelper
{
Plc myPlc;
public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;
///
/// 打开连接
///
/// PLC CPU 类型
/// plc ip 地址
/// plc 端口号
/// PLC 机架号
/// PLC 插槽号
public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0)
{
myPlc = new Plc(cpuType, ip, port, rack, solt);
myPlc.Open();
}
///
/// 断开和PLC的连接
///
public void Disconnect()
{
myPlc?.Close();
}
public object Read(string address)
{
if (!IsConnected) return default;
return myPlc?.Read(address);
}
public void Write(string address, object value)
{
myPlc?.Write(address, value);
}
public ReadT ReadStruct(int db, int startAddress = 0)
{
if (!IsConnected) return default;
return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
}
public void WriteStruct(object structValue, int db, int startAddress = 0)
{
myPlc?.WriteStruct(structValue, db, startAddress);
}
public int ReadClass(object sourceClass, int db, int startAddress = 0)
{
if (!IsConnected) return -1;
return myPlc.ReadClass(sourceClass, db, startAddress);
}
public void WriteClass(object sourceClass, int db, int startAddress = 0)
{
myPlc?.WriteClass(sourceClass, db, startAddress);
}
}
}