using System;
using HslCommunication.Profinet.Siemens;
namespace DataBlockHelper.DbWrite
{
public class PlcWriteUtil
{
private static SiemensS7Net _siemensS7Net => PlcConnect.Instance;
public PlcWriteUtil()
{
}
public void Write(PlcDbEnum dbEnum, string address, PlcDbType dbType, object value)
{
string plcAddress = $"{dbEnum.ToString()}.{address}";
switch (dbType)
{
case PlcDbType.PlcInt:
_siemensS7Net.Write(plcAddress, Convert.ToInt32(value));
break;
case PlcDbType.PlcUint:
_siemensS7Net.Write(plcAddress, Convert.ToInt16(value));
break;
case PlcDbType.PlcBoolean:
_siemensS7Net.Write(plcAddress, Convert.ToBoolean(value));
break;
case PlcDbType.PlcFloat:
_siemensS7Net.Write(plcAddress, Convert.ToSingle(value));
break;
}
}
///
/// 可能要读取M块
///
///
///
///
public void Write(string plcAddress, PlcDbType dbType, object value)
{
switch (dbType)
{
case PlcDbType.PlcInt:
_siemensS7Net.Write(plcAddress, Convert.ToInt32(value));
break;
case PlcDbType.PlcUint:
_siemensS7Net.Write(plcAddress, Convert.ToInt16(value));
break;
case PlcDbType.PlcBoolean:
_siemensS7Net.Write(plcAddress, Convert.ToBoolean(value));
break;
case PlcDbType.PlcFloat:
_siemensS7Net.Write(plcAddress, Convert.ToSingle(value));
break;
}
}
}
public enum PlcDbEnum
{
Db2102 = 2102,
Db2103 = 2103,
Db2104 = 2104,
Db2105 = 2105,
Db2106 = 2106,
Db2107 = 2107,
}
public enum PlcDbType
{
PlcUint,
PlcInt,
PlcFloat,
PlcBoolean
}
}