public async Task <SessionsHandle> CreateHandleFromConfiguration(ModuleConfig config) { SessionsHandle sessionsHandle = null; foreach (var config_pair in config.SlaveConfigs) { ModbusSlaveConfig slaveConfig = config_pair.Value; switch (slaveConfig.GetConnectionType()) { case ConnectionType.ModbusTCP: { if (sessionsHandle == null) { sessionsHandle = new SessionsHandle(); } ModbusSlaveSession slave = new ModbusTCPSlaveSession(slaveConfig); await slave.InitSession(); sessionsHandle.ModbusSessionList.Add(slave); break; } case ConnectionType.ModbusRTU: { if (sessionsHandle == null) { sessionsHandle = new SessionsHandle(); } ModbusSlaveSession slave = new ModbusRTUSlaveSession(slaveConfig); await slave.InitSession(); sessionsHandle.ModbusSessionList.Add(slave); break; } case ConnectionType.ModbusASCII: { break; } case ConnectionType.Unknown: { break; } } } return(sessionsHandle); }
public ModbusTCPSlaveSession(ModbusSlaveConfig conf) : base(conf) { }
public ModbusSlaveSession(ModbusSlaveConfig conf) { this.config = conf; }
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); }
protected ModbusSlaveSession(ModbusSlaveConfig conf) { this.config = conf; }