You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/DataBlockHelper/DbWrite/PlcWriteUtil.cs

110 lines
2.6 KiB
C#

using System;
using System.Text;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
namespace DataBlockHelper.DbWrite
{
public class PlcWriteUtil
{
private static SiemensS7Net _siemensS7Net => PlcConnect.Instance;
public PlcWriteUtil()
{
}
public OperateResult Write(PlcDbEnum dbEnum, string address, PlcDbType dbType, object value)
{
OperateResult operateResult = new OperateResult();
string plcAddress = $"{dbEnum.ToString()}.{address}";
switch (dbType)
{
case PlcDbType.PlcInt:
operateResult=_siemensS7Net.Write(plcAddress, Convert.ToInt32(value));
break;
case PlcDbType.PlcUint:
operateResult=_siemensS7Net.Write(plcAddress, Convert.ToInt16(value));
break;
case PlcDbType.PlcBoolean:
operateResult= _siemensS7Net.Write(plcAddress, Convert.ToBoolean(value));
break;
case PlcDbType.PlcFloat:
float number = Convert.ToSingle(value);
operateResult=_siemensS7Net.Write(plcAddress,number);
break;
}
return operateResult;
}
/// <summary>
/// 可能要读取M块
/// </summary>
/// <param name="plcAddress"></param>
/// <param name="dbType"></param>
/// <param name="value"></param>
public void Write(string plcAddress, PlcDbType dbType, object value)
{
OperateResult operateResult = new OperateResult();
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
}
}