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.
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 可能要读取M块
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="plcAddress"></param>
|
|
|
|
|
/// <param name="dbType"></param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|