DeviceCommunicationReadCustomerAsyncT 方法 (String) |
命名空间: HslCommunication.Core.Device
public Task<OperateResult<T>> ReadCustomerAsync<T>( string address ) where T : new(), IDataTransfer
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; } }
MelsecMcNet melsec = new MelsecMcNet( "192.168.0.100", 6000 ); OperateResult<DataMy> read = await melsec.ReadCustomerAsync<DataMy>( "M100" ); if (read.IsSuccess) { // success DataMy data = read.Content; } else { // failed Console.WriteLine( "读取失败:" + read.Message ); }