protected void ProcessResponse(ModbusSlaveConfig config, ReadOperation x) { int count = 0; int step_size = 0; int start_digit = 0; List <ModbusOutValue> value_list = new List <ModbusOutValue>(); switch (x.Response[m_dataBodyOffset])//function code { case (byte)ModbusConstants.FunctionCodeType.ReadCoils: case (byte)ModbusConstants.FunctionCodeType.ReadInputs: { count = x.Response[m_dataBodyOffset + 1] * 8; count = (count > x.Count) ? x.Count : count; step_size = 1; start_digit = x.Response[m_dataBodyOffset] - 1; break; } case (byte)ModbusConstants.FunctionCodeType.ReadHoldingRegisters: case (byte)ModbusConstants.FunctionCodeType.ReadInputRegisters: { count = x.Response[m_dataBodyOffset + 1]; step_size = 2; start_digit = (x.Response[m_dataBodyOffset] == 3) ? 4 : 3; break; } } for (int i = 0; i < count; i += step_size) { string res = ""; string cell = ""; string val = ""; if (step_size == 1) { cell = string.Format(x.OutFormat, (char)x.EntityType, x.Address + i + 1); val = string.Format("{0}", (x.Response[m_dataBodyOffset + 2 + (i / 8)] >> (i % 8)) & 0b1); } else if (step_size == 2) { cell = string.Format(x.OutFormat, (char)x.EntityType, x.Address + (i / 2) + 1); val = string.Format("{0,00000}", ((x.Response[m_dataBodyOffset + 2 + i]) * 0x100 + x.Response[m_dataBodyOffset + 3 + i])); } res = cell + ": " + val + "\n"; Console.WriteLine(res); ModbusOutValue value = new ModbusOutValue() { DisplayName = x.DisplayName, Address = cell, Value = val }; value_list.Add(value); } if (value_list.Count > 0) { PrepareOutMessage(config.HwId, x.CorrelationId, value_list); } }
protected void ProcessResponse(ModbusSlaveConfig config, ReadOperation x) { int count = 0; int step_size = 0; int start_digit = 0; List <ModbusOutValue> value_list = new List <ModbusOutValue>(); switch (x.Response[m_dataBodyOffset])//function code { case (byte)ModbusConstants.FunctionCodeType.ReadCoils: case (byte)ModbusConstants.FunctionCodeType.ReadInputs: { count = x.Response[m_dataBodyOffset + 1] * 8; count = (count > x.Count) ? x.Count : count; step_size = 1; start_digit = x.Response[m_dataBodyOffset] - 1; break; } case (byte)ModbusConstants.FunctionCodeType.ReadHoldingRegisters: case (byte)ModbusConstants.FunctionCodeType.ReadInputRegisters: { count = x.Response[m_dataBodyOffset + 1]; step_size = 2; start_digit = (x.Response[m_dataBodyOffset] == 3) ? 4 : 3; break; } } byte[] tValue = new byte[4]; string res = ""; string cell = ""; string val = ""; for (int i = 0; i < count; i += step_size) { res = ""; cell = ""; val = ""; if (step_size == 1) { cell = string.Format(x.OutFormat, (char)x.EntityType, x.Address + i + 1); val = string.Format("{0}", (x.Response[m_dataBodyOffset + 2 + (i / 8)] >> (i % 8)) & 0b1); } else if (step_size == 2) { cell = string.Format(x.OutFormat, (char)x.EntityType, x.Address + (i / 2) + 1); val = string.Format("{0,00000}", ((x.Response[m_dataBodyOffset + 2 + i]) * 0x100 + x.Response[m_dataBodyOffset + 3 + i])); //Console.WriteLine(">> " + i.ToString() + ":" + (x.Response[m_dataBodyOffset + 2 + i]).ToString()); //Console.WriteLine(">> " + i.ToString() + ":" + (x.Response[m_dataBodyOffset + 3 + i]).ToString()); // Little Endian tValue[3 - i] = (x.Response[m_dataBodyOffset + 2 + i]); tValue[3 - (i + 1)] = (x.Response[m_dataBodyOffset + 3 + i]); } } //Console.WriteLine(ByteArrayToString(tValue)); if (x.Signed) { val = (((Double)(BitConverter.ToInt32(tValue))) / ((double)x.Divisor)).ToString(x.NumberFormat); } else { val = (((Double)(BitConverter.ToUInt32(tValue))) / ((double)x.Divisor)).ToString(x.NumberFormat); } res = cell + ": " + val + "\n"; if (x.Debug) { Console.WriteLine(res); } ModbusOutValue value = new ModbusOutValue() { DisplayName = x.DisplayName, Address = cell, Value = val }; value_list.Add(value); if (value_list.Count > 0) { PrepareOutMessage(config.HwId, x.CorrelationId, value_list); } }