/// <summary> /// Reads the Device Information by using the function code (0x2b / 0x0e). The methods sends as many messages as needed to read the whole data. /// </summary> /// <param name="deviceAddress">Address of the modbus device.</param> /// <param name="deviceIdCode">Device id code to read.</param> /// <param name="timeout">Timeout for response in milli seconds.</param> /// <returns>Returns an array with all device information.</returns> /// <remarks> /// Look at http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf for more details. /// </remarks> public virtual DeviceIdentification[] ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, int timeout = 2000) { var moreFollows = true; var objectId = ModbusObjectId.VendorName; var values = new ArrayList(); while (moreFollows) { var v = this.ReadDeviceIdentification(deviceAddress, deviceIdCode, ref objectId, out moreFollows, timeout); for (var i = 0; i < v.Length; i++) { values.Add(v[i]); } } return((DeviceIdentification[])values.ToArray(typeof(DeviceIdentification))); }
/// <summary> /// Reads the Device Information by using the function code (0x2b / 0x0e) /// </summary> /// <param name="deviceAddress">Address of the modbus device.</param> /// <param name="deviceIdCode">Device id code to read.</param> /// <param name="objectId">Object id to start at. Receives the next object id to read from if moreFollows is set to true.</param> /// <param name="moreFollows">Receives true if there is more information to read.</param> /// <param name="timeout">Timeout for response in milli seconds.</param> /// <returns>Returns an array with all device information.</returns> /// <remarks> /// Look at http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf for more details. /// </remarks> public virtual DeviceIdentification[] ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, ref ModbusObjectId objectId, out bool moreFollows, int timeout = 2000) { object telegramContext = null; [email protected](deviceAddress, (byte)ModbusFunctionCode.ReadDeviceIdentification, 3, this.buffer, out var telegramLength, out var dataPos, false, ref telegramContext); this.buffer[dataPos] = 0x0e; this.buffer[dataPos + 1] = (byte)deviceIdCode; this.buffer[dataPos + 2] = (byte)objectId; this.SendReceive(deviceAddress, ModbusFunctionCode.ReadDeviceIdentification, timeout, telegramLength, -1, telegramContext, ref dataPos); if (deviceAddress == ModbusConst.BroadcastAddress) { moreFollows = false; return(null); } moreFollows = this.buffer[dataPos + 3] != 0; objectId = (ModbusObjectId)this.buffer[dataPos + 4]; var cnt = this.buffer[dataPos + 5]; var values = new DeviceIdentification[cnt]; dataPos += 6; for (var i = 0; i < cnt; i++) { var v = new char[this.buffer[dataPos + 1]]; for (var n = 0; n < v.Length; ++n) { v[n] = (char)this.buffer[dataPos + 2 + n]; } values[i] = new DeviceIdentification { ObjectId = (ModbusObjectId)this.buffer[dataPos], Value = new string(v) }; dataPos += (short)(2 + v.Length); } return(values); }
/// <summary> /// Reads the Device Information by using the function code (0x2b / 0x0e) /// </summary> /// <param name="deviceAddress">Address of the modbus device.</param> /// <param name="deviceIdCode">Device id code to read.</param> /// <param name="objectId">Object id to start at. Receives the next object id to read from if moreFollows is set to true.</param> /// <param name="moreFollows">Receives true if there is more information to read.</param> /// <param name="timeout">Timeout for response in milli seconds.</param> /// <returns>Returns an array with all device information.</returns> /// <remarks> /// Look at http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf for more details. /// </remarks> public virtual DeviceIdentification[] ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, ref ModbusObjectId objectId, out bool moreFollows, int timeout = 2000) { short telegramLength; short dataPos; object telegramContext = null; _Interface.CreateTelegram(deviceAddress, (byte)ModbusFunctionCode.ReadDeviceIdentification, 3, _Buffer, out telegramLength, out dataPos, false, ref telegramContext); _Buffer[dataPos] = 0x0e; _Buffer[dataPos + 1] = (byte)deviceIdCode; _Buffer[dataPos + 2] = (byte)objectId; SendReceive(deviceAddress, ModbusFunctionCode.ReadDeviceIdentification, timeout, telegramLength, -1, telegramContext, ref dataPos); if (deviceAddress == ModbusConst.BroadcastAddress) { moreFollows = false; return null; } moreFollows = _Buffer[dataPos + 3] != 0; objectId = (ModbusObjectId) _Buffer[dataPos + 4]; byte cnt = _Buffer[dataPos + 5]; var values = new DeviceIdentification[cnt]; dataPos += 6; for (int i = 0; i < cnt; i++) { var v = new char[_Buffer[dataPos + 1]]; for (int n = 0; n < v.Length; ++n) { v[n] = (char) _Buffer[dataPos + 2 + n]; } values[i] = new DeviceIdentification { ObjectId = (ModbusObjectId) _Buffer[dataPos], Value = new string(v) }; dataPos += (short)(2 + v.Length); } return values; }
/// <summary> /// Reads the Device Information by using the function code (0x2b / 0x0e). The methods sends as many messages as needed to read the whole data. /// </summary> /// <param name="deviceAddress">Address of the modbus device.</param> /// <param name="deviceIdCode">Device id code to read.</param> /// <param name="timeout">Timeout for response in milli seconds.</param> /// <returns>Returns an array with all device information.</returns> /// <remarks> /// Look at http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf for more details. /// </remarks> public virtual DeviceIdentification[] ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, int timeout = 2000) { bool moreFollows = true; var objectId = ModbusObjectId.VendorName; var values = new ArrayList(); while (moreFollows) { var v = ReadDeviceIdentification(deviceAddress, deviceIdCode, ref objectId, out moreFollows, timeout); for (int i = 0; i < v.Length; i++) { values.Add(v[i]); } } return (DeviceIdentification[]) values.ToArray(typeof (DeviceIdentification)); }