public async Task InitSession() { await this.ConnectSlave(); foreach (var op_pair in this.config.Operations) { ReadOperation x = op_pair.Value; //Can't read float from coils/inputs if (x.FunctionCode == (byte)FunctionCodeType.ReadCoils || x.FunctionCode == (byte)FunctionCodeType.ReadInputs) { x.ValueType = ModbusValueType.Basic; } //If working with complex value we need to override count if (x.ValueType != ModbusValueType.Basic) { x.Count = ModbusComplexValuesHandler.GetCountForValueType(x.ValueType); } x.RequestLen = this.m_reqSize; x.Request = new byte[m_bufSize]; this.EncodeRead(x); } }
public async Task WriteMessage(WriteOperation operation) { if (operation.ValueType != ModbusValueType.Basic) { //Handling complex value //Obtain value split into several int values var valuesToBeWritten = ModbusComplexValuesHandler.SplitComplexValue(operation.Value, operation.ValueType); //We need to write each part of complex value to separate registry foreach (var v in valuesToBeWritten) { operation.IntValueToWrite = Convert.ToUInt16(operation.Value); await WriteCB(operation); operation.StartAddress = (Convert.ToInt32(operation.StartAddress) + 1).ToString(); } } else { operation.IntValueToWrite = Convert.ToUInt16(operation.Value); await WriteCB(operation); } }
protected List <ModbusOutValue> 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)FunctionCodeType.ReadCoils: case (byte)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)FunctionCodeType.ReadHoldingRegisters: case (byte)FunctionCodeType.ReadInputRegisters: { count = x.Response[m_dataBodyOffset + 1]; step_size = 2; start_digit = (x.Response[m_dataBodyOffset] == 3) ? 4 : 3; break; } } var initialCell = string.Format(x.OutFormat, (char)x.Entity, x.Address + 1); if (x.ValueType == ModbusValueType.Basic) { 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.Entity, 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.Entity, 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); } } //We need to merge complex value else { var bytesA = x.Response.SubArray(m_dataBodyOffset + 2, step_size * x.Count); var val = ModbusComplexValuesHandler.MergeComplexValue(bytesA, x.ValueType, this.config.EndianSwap, this.config.MidEndianSwap); ModbusOutValue value = new ModbusOutValue() { DisplayName = x.DisplayName, Address = initialCell, Value = val }; value_list.Add(value); var res = initialCell + " : " + val + "\n"; Console.WriteLine(res); } if (value_list.Count > 0) { PrepareOutMessage(config.HwId, x.CorrelationId, value_list); } return(value_list); }