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