点击或拖拽改变大小

ReadWriteNetHelperWriteCustomerAsyncT 方法

写入自定义类型的数据,该类型必须继承自IDataTransfer接口
Write data of a custom type, which must inherit from the IDataTransfer interface

命名空间:  HslCommunication.Core.Net
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.1.2.0 (12.1.2.0)
语法
public static Task<OperateResult> WriteCustomerAsync<T>(
	IReadWriteNet readWriteNet,
	string address,
	T data
)
where T : new(), IDataTransfer

参数

readWriteNet
类型:HslCommunication.CoreIReadWriteNet

[缺少 "M:HslCommunication.Core.Net.ReadWriteNetHelper.WriteCustomerAsync``1(HslCommunication.Core.IReadWriteNet,System.String,``0)" 的 <param name="readWriteNet"/> 文档]

address
类型:SystemString
起始地址
data
类型:T

[缺少 "M:HslCommunication.Core.Net.ReadWriteNetHelper.WriteCustomerAsync``1(HslCommunication.Core.IReadWriteNet,System.String,``0)" 的 <param name="data"/> 文档]

类型参数

T
类型对象

返回值

类型:TaskOperateResult
带有成功标识的结果类对象
备注
需要是定义一个类,选择好相对于的IDataTransfer实例,才能调用该方法。
示例
此处演示三菱的读取示例,先定义一个类,实现IDataTransfer接口
DataMy示例
public class DataMy : IDataTransfer
{
    // 根据对应的设备选择对应的实例化
    // 三菱 RegularByteTransform
    // 西门子 ReverseBytesTransform
    // Modbus及欧姆龙 ReverseWordTransform
    private IByteTransform byteTransform = new RegularByteTransform( );

    public ushort ReadCount => 5;


    public short temperature = 0;  // 温度
    public float press = 0f;       // 压力
    public int others = 0;         // 自定义的其他信息



    public void ParseSource( byte[] Content )
    {
        temperature = byteTransform.TransInt16( Content, 0 );
        press = byteTransform.TransSingle( Content, 2 );
        others = byteTransform.TransInt32( Content, 6 );
    }

    public byte[] ToSource( )
    {
        byte[] buffer = new byte[10];
        byteTransform.TransByte( temperature ).CopyTo( buffer, 0 );
        byteTransform.TransByte( press ).CopyTo( buffer, 2 );
        byteTransform.TransByte( others ).CopyTo( buffer, 6 );
        return buffer;
    }
}
接下来就可以实现数据的读取了
WriteCustomerAsync示例
MelsecMcNet melsec = new MelsecMcNet( "192.168.0.100", 6000 );

DataMy dataMy = new DataMy( );
dataMy.temperature = 20;
dataMy.press = 123.456f;
dataMy.others = 1234232132;

OperateResult write = await melsec.WriteCustomerAsync( "M100", dataMy );
if (write.IsSuccess)
{
    // success
    Console.WriteLine( "写入成功!" );
}
else
{
    // failed
    Console.WriteLine( "读取失败:" + write.Message );
}
参见