点击或拖拽改变大小

MelsecFxSerialWrite 方法 (String, Byte)

根据指定的地址向PLC写入数据,数据格式为原始的字节类型
Write data to the PLC according to the specified address, the data format is the original byte type

命名空间:  HslCommunication.Profinet.Melsec
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.0.0.0 (12.0.0.0)
语法
public override OperateResult Write(
	string address,
	byte[] value
)

参数

address
类型:SystemString
初始地址,支持的类型参考文档说明
value
类型:SystemByte
原始的字节数据

返回值

类型:OperateResult
是否写入成功的结果对象

实现

IReadWriteNetWrite(String, Byte)
IReadWriteNetWrite(String, Byte)
示例
假设起始地址为D100,D100存储了温度,100.6℃值为1006,D101存储了压力,1.23Mpa值为123,D102,D103存储了产量计数,写入如下:
Write示例
MelsecFxSerial melsecFx = new MelsecFxSerial( );
melsecFx.SerialPortInni( sp =>
{
    sp.PortName = "COM1";
    sp.BaudRate = 9600;
    sp.DataBits = 7;
    sp.StopBits = System.IO.Ports.StopBits.One;
    sp.Parity = System.IO.Ports.Parity.Even;
} );
melsecFx.Open( );

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

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

// 上面的功能等同于三个数据分别写入,下面的性能更差点,因为进行了三次通讯,而且每次还要判断是否写入成功
//melsec_net.Write( "D100", (short)1234 );
//melsec_net.Write( "D100", (short)2100 );
//melsec_net.Write( "D100", 12353423 );
以下是读取不同类型数据的示例
Write示例
MelsecFxSerial melsecFx = new MelsecFxSerial( );
melsecFx.SerialPortInni( sp =>
{
    sp.PortName = "COM1";
    sp.BaudRate = 9600;
    sp.DataBits = 7;
    sp.StopBits = System.IO.Ports.StopBits.One;
    sp.Parity = System.IO.Ports.Parity.Even;
} );
melsecFx.Open( );

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

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