IReadWriteNetWriteCustomerT 方法 |
命名空间: HslCommunication.Core
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 ); DataMy dataMy = new DataMy( ); dataMy.temperature = 20; dataMy.press = 123.456f; dataMy.others = 1234232132; OperateResult write = melsec.WriteCustomer( "M100" ,dataMy ); if (write.IsSuccess) { // success Console.WriteLine( "写入成功!" ); } else { // failed Console.WriteLine( "读取失败:" + write.Message ); }