IDataTransfer 接口 |
命名空间: HslCommunication
IDataTransfer 类型公开以下成员。
名称 | 说明 | |
---|---|---|
ParseSource |
从字节数组进行解析实际的对象
| |
ToSource |
将对象生成字符源,写入PLC中
|
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 = melsec.ReadCustomer<DataMy>( "M100" ); if (read.IsSuccess) { // success DataMy data = read.Content; } else { // failed Console.WriteLine( "读取失败:" + read.Message ); }