//********************************** //* Extract a header from a packet //********************************** public List <byte> Extract(byte[] packet, int length) { List <byte> m_EncapsulatedData = new List <byte>(); string txtValue = string.Empty; string Value3 = string.Empty; byte[] Data = null; // Read Response if (packet[20] == 0x55) { switch (packet[22]) { case 0: // Bit X for (int i = 32; i < packet.Length; i++) { m_EncapsulatedData.Add(packet[i]); } break; case 1: //Byte B txtValue = XGTFENETMessage.GetValStr(packet, 20 + 12, 1); Data = (Conversion.HexToBytes(txtValue)); break; case 2: //Word txtValue = XGTFENETMessage.GetValStr(packet, 20 + 12, 2); Data = (Conversion.HexToBytes(txtValue)); break; case 3: //DWord txtValue = XGTFENETMessage.GetValStr(packet, 20 + 12, 4); Data = (Conversion.HexToBytes(txtValue)); break; case 4: //LWord txtValue = XGTFENETMessage.GetValStr(packet, 20 + 12, 8); Data = (Conversion.HexToBytes(txtValue)); break; case 20: Data = XGTFENETMessage.ToStringArray(packet, packet.Length); break; } for (int i = 0; i < Data.Length; i++) { m_EncapsulatedData.Add(Data[i]); } } return(m_EncapsulatedData); }
public unsafe ItemData <float> ReadFloat(DeviceAddress address) { byte[] data = WriteSyncData(XGTFENETMessage.ReadHoldingRegistersMessage((byte)address.Area, Convert.ToString(address.Start), (ushort)address.DBNumber)); if (Extract(data, data.Length).ToArray() == null) { return(new ItemData <float>(0.0f, 0, QUALITIES.QUALITY_BAD)); } else { return(new ItemData <float>(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(Extract(data, data.Length).ToArray(), 0)), 0, QUALITIES.QUALITY_GOOD)); } }
public ItemData <string> ReadString(DeviceAddress address, ushort size) { byte[] data = WriteSyncData(XGTFENETMessage.ReadHoldingRegistersMessage((byte)address.Area, Convert.ToString(address.Start), (ushort)address.DBNumber)); if (Extract(data, data.Length).ToArray() == null) { return(new ItemData <string>(string.Empty, 0, QUALITIES.QUALITY_BAD)); } else { return(new ItemData <string>(Encoding.ASCII.GetString(Extract(data, data.Length).ToArray(), 0, data.Length), 0, QUALITIES.QUALITY_GOOD));//是否考虑字节序问题? } }
public ItemData <byte> ReadByte(DeviceAddress address) { byte[] data = WriteSyncData(XGTFENETMessage.ReadHoldingRegistersMessage((byte)address.Area, Convert.ToString(address.Start), (ushort)address.DBNumber)); if (Extract(data, data.Length).ToArray() == null) { return(new ItemData <byte>(0, 0, QUALITIES.QUALITY_BAD)); } else { return(new ItemData <byte>(Extract(data, data.Length).ToArray()[0], 0, QUALITIES.QUALITY_GOOD)); } }
public ItemData <bool> ReadBit(DeviceAddress address) { byte[] data = address.DBNumber > 2 ? WriteSyncData(XGTFENETMessage.ReadCoilStatusMessage((byte)address.Area, Convert.ToString(address.Start), (ushort)address.DBNumber)) : WriteSyncData(XGTFENETMessage.ReadCoilStatusMessage((byte)address.Area, Convert.ToString(address.Start * 16 + address.Bit), (ushort)address.DBNumber)); if (Extract(data, data.Length).ToArray() == null) { return(new ItemData <bool>(false, 0, QUALITIES.QUALITY_BAD)); } if (data.Length == 1) { return(new ItemData <bool>(Extract(data, data.Length).ToArray()[0] > 0, 0, QUALITIES.QUALITY_GOOD)); } unsafe { fixed(byte *p = data) { short *p1 = (short *)p; return(new ItemData <bool>((*p1 & (1 << address.Bit.BitSwap())) != 0, 0, QUALITIES.QUALITY_GOOD)); } } }
public byte[] WriteSingleRegister(int id, int startAddress, byte[] values) { byte[] frame = XGTFENETMessage.WriteSingleRegisterMessage(Convert.ToByte(id), Convert.ToString(startAddress), values); return(WriteSyncData(frame)); }
public byte[] WriteSingleCoils(int id, int startAddress, bool OnOff) { byte[] frame = XGTFENETMessage.WriteSingleCoilMessage(Convert.ToByte(id), Convert.ToString(startAddress), OnOff); return(WriteSyncData(frame)); }