/// <summary> /// 指定寄存器写入操作,阻塞的 /// </summary> /// <param name="dev"></param> /// <param name="positions"></param> /// <returns>单寄存器返回写入成功的值,多寄存器返回写入成功寄存器的个数,失败返回null</returns> public object Write <TResult>(TStorage storage, Expression <Func <TStorage, TResult> > positions) { ready(); object returnValue = null; var prop = positions.GetProperty(); var value = prop.GetValue(storage, null); var attr = prop.GetAttribute <StorageAttribute>(); if (attr == null) { throw new ComException("结构体属性未指定StorageAttribute特性"); } var byteSize = prop.PropertyType.GetByteSize(); var storageSize = (int)Math.Ceiling((float)byteSize / 2); byte[] bySend = null; if (storageSize < 2)//写单个寄存器 { bySend = _iProtocol.GetWrite06Data(DeviceAddress, attr.Addr, value.ToString(), ModBusHelper.convertDataType(prop.PropertyType), ByteOrder, CRCType, ref _tcpFlag); } else { object[][] v = new object[1][]; v[0] = new object[] { ModBusHelper.convertDataType(prop.PropertyType), value }; bySend = _iProtocol.GetWrite10Data(DeviceAddress, attr.Addr, storageSize.ToString(), (byte)byteSize, v, ByteOrder, CRCType, ref _tcpFlag); } _con.Com.Write(bySend); byte[] byRe = _iProtocol.ReadReturnWrite(_con.Com); if (_iProtocol.CheckReceiveData(byRe)) { if (storageSize < 2) { returnValue = _iProtocol.ToWriteReturn(byRe, ByteOrder, prop.PropertyType); } else { returnValue = _iProtocol.ToWriteReturn(byRe, 1, typeof(UInt16)); } } return(returnValue); }
/// <summary> /// 读取地址表所有寄存器,阻塞的 /// </summary> /// <param name="dev"></param> /// <returns></returns> public TStorage Read() { ready(); var type = typeof(TStorage); List <byte> packReceive = new List <byte>(); //以一次读取120个字 var packLength = 120; var packSends = type.SendPackaging(packLength); if (packSends?.Count > 0) { foreach (var s in packSends) { byte[] bySend = _iProtocol.GetRead03Data(DeviceAddress, s.Start, s.Length.ToString(), CRCType, ref _tcpFlag); _con.Com.Write(bySend); byte[] byRead = _iProtocol.ReadReturnRead(_con.Com, s.Length * 2); if (_iProtocol.CheckReceiveData(byRead)) { var byValue = _iProtocol.GetCutReceiveData(byRead); packReceive.AddRange(byValue.ToList()); } Thread.Sleep(5); } if (packReceive.Count == type.GetByteSize()) { packReceive = ModBusHelper.ExchangeOrder(packReceive, 0, ByteOrder, type); byte[] byRe = packReceive.ToArray(); return((TStorage)ConvertHelper.BytesToStuct(byRe, type)); } } throw new ComException("未读取到数据"); }
public object ToWriteReturn(byte[] byData, byte byteOrder, Type type) { return(ConvertHelper.BytesToStuct(ModBusHelper.GetDataByte(byData, byteOrder, 4, 2), type)); }
public bool CheckReceiveData(byte[] data) { return(ModBusHelper.CheckCRC(data)); }
public T ToSingleField <T>(byte[] byRead, byte byteOrder, int byteSize) { return((T)ConvertHelper.BytesToStuct(ModBusHelper.GetDataByte(byRead, byteOrder, 3, byteSize), typeof(T))); }
public byte[] GetWrite10Data(byte addrNo, int beginPos, string elementCount, byte dataLength, object[][] value, byte byteOrder, byte crcType, ref ushort tcpFlag) { return(ModBusHelper.GetWrite10Data(addrNo, beginPos, elementCount, dataLength, value, byteOrder, crcType)); }
public byte[] GetWrite06Data(byte addrNo, int beginPos, string value, ModBusHelper.DataType dataType, byte byteOrder, byte crcType, ref ushort tcpFlag) { return(ModBusHelper.GetWrite06Data(addrNo, beginPos, value, dataType, byteOrder, crcType)); }
public byte[] GetRead03Data(byte addrNo, int beginPos, string elementCount, byte crcType, ref ushort tcpFlag) { return(ModBusHelper.GetRead03Data(addrNo, beginPos, elementCount, crcType)); }
public bool CheckReceiveData(byte[] data) { return(ModBusHelper.CheckTCPLength(data)); }