点击或拖拽改变大小

SiemensFetchWriteNetWriteAsync 方法 (String, Byte)

将数据写入到PLC数据,地址格式为I100,Q100,DB20.100,M100,以字节为单位
Writes data to the PLC data, in the address format i100,q100,db20.100,m100, in bytes

命名空间:  HslCommunication.Profinet.Siemens
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.1.2.0 (12.1.2.0)
语法
public override Task<OperateResult> WriteAsync(
	string address,
	byte[] value
)

参数

address
类型:SystemString
起始地址,格式为M100,I100,Q100,DB1.100 -> Starting address, formatted as M100,I100,Q100,DB1.100
value
类型:SystemByte
要写入的实际数据 -> The actual data to write

返回值

类型:TaskOperateResult
是否写入成功的结果对象 -> Whether to write a successful result object

实现

IReadWriteNetWriteAsync(String, Byte)
示例
假设起始地址为M100,M100,M101存储了温度,100.6℃值为1006,M102,M103存储了压力,1.23Mpa值为123,M104-M107存储了产量计数,写入如下:
Write示例
SiemensFetchWriteNet siemens = new SiemensFetchWriteNet( "192.168.1.110", 2000 );

// 拼凑数据,这样的话,一次通讯就完成数据的全部写入
byte[] buffer = new byte[8];
siemens.ByteTransform.TransByte( (short)1234 ).CopyTo( buffer, 0 );
siemens.ByteTransform.TransByte( (short)2100 ).CopyTo( buffer, 2 );
siemens.ByteTransform.TransByte( 12353423 ).CopyTo( buffer, 4 );

OperateResult write = siemens.Write( "M100", buffer );
if (write.IsSuccess)
{
    // success
}
else
{
    // failed
}

// 上面的功能等同于三个数据分别写入,下面的性能更差点,因为进行了三次通讯,而且每次还要判断是否写入成功
//siemens.Write( "M100", (short)1234 );
//siemens.Write( "M100", (short)2100 );
//siemens.Write( "M100", 12353423 );
以下是写入不同类型数据的示例
Write示例
SiemensFetchWriteNet siemens = new SiemensFetchWriteNet( "192.168.1.110", 2000 );

// 此处以M100寄存器作为示例
siemens.Write( "M100", (short)1234 );                // 写入M100  short值  ,W3C0,R3C0 效果是一样的
siemens.Write( "M100", (ushort)45678 );              // 写入M100  ushort值
siemens.Write( "M100", 1234566 );                    // 写入M100  int值
siemens.Write( "M100", (uint)1234566 );               // 写入M100  uint值
siemens.Write( "M100", 123.456f );                    // 写入M100  float值
siemens.Write( "M100", 123.456d );                    // 写入M100  double值
siemens.Write( "M100", 123456661235123534L );          // 写入M100  long值
siemens.Write( "M100", 523456661235123534UL );          // 写入M100  ulong值
siemens.Write( "M100", "K123456789" );                // 写入M100  string值

// 读取数组
siemens.Write( "M100", new short[] { 123, 3566, -123 } );                // 写入M100  short值  ,W3C0,R3C0 效果是一样的
siemens.Write( "M100", new ushort[] { 12242, 42321, 12323 } );              // 写入M100  ushort值
siemens.Write( "M100", new int[] { 1234312312, 12312312, -1237213 } );                    // 写入M100  int值
siemens.Write( "M100", new uint[] { 523123212, 213, 13123 } );               // 写入M100  uint值
siemens.Write( "M100", new float[] { 123.456f, 35.3f, -675.2f } );                    // 写入M100  float值
siemens.Write( "M100", new double[] { 12343.542312d, 213123.123d, -231232.53432d } );                    // 写入M100  double值
siemens.Write( "M100", new long[] { 1231231242312, 34312312323214, -1283862312631823 } );          // 写入M100  long值
siemens.Write( "M100", new ulong[] { 1231231242312, 34312312323214, 9731283862312631823 } );          // 写入M100  ulong值
参见