/// <summary> /// Acquisition thread /// Awaits for trigger; /// After configured period send appropriate command to MdbSim for each point type /// </summary> private void Acquisition_DoWork() { while (true) { try { acquisitionTrigger.WaitOne(); DO_REG_sekunde++; HR_INT_sekunde++; if (DO_REG_sekunde == ConfigReader.Instance.GetAcquisitionInterval("DigOut")) { ModbusReadCommandParameters p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, ConfigReader.Instance.GetStartAddress("DigOut"), ConfigReader.Instance.GetNumberOfRegisters("DigOut"));; ModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.commandExecutor.EnqueueCommand(fn); DO_REG_sekunde = 0; } if (HR_INT_sekunde == ConfigReader.Instance.GetAcquisitionInterval("AnaOut")) { ModbusReadCommandParameters p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, ConfigReader.Instance.GetStartAddress("AnaOut"), ConfigReader.Instance.GetNumberOfRegisters("AnaOut"));; ModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.commandExecutor.EnqueueCommand(fn); HR_INT_sekunde = 0; } } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; stateUpdater.LogMessage(message); } } }
/// <summary> /// Executes a digital write command. /// </summary> /// <param name="configItem">The configuration item.</param> /// <param name="transactionId">The transaction identifier.</param> /// <param name="remoteUnitAddress">The remote unit address.</param> /// <param name="pointAddress">The point address.</param> /// <param name="value">The value.</param> private void ExecuteDigitalCommand(IConfigItem configItem, ushort transactionId, byte remoteUnitAddress, ushort pointAddress, int value) { ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, pointAddress, (ushort)value, transactionId, remoteUnitAddress); IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.functionExecutor.EnqueueCommand(fn); }
private void WriteCommand_Execute(object obj) { try { ModbusWriteCommandParameters p = null; switch (type) { case PointType.DO_REG: p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, address, RawValue); break; case PointType.HR_INT: p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, address, RawValue); break; default: break; } // TODO implement ModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.commandExecutor.ExecuteCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <summary> /// Method that is executed when read button is clicked on control window; /// Method should create read command parameters and provide it to FunctionFactory /// </summary> /// <param name="obj">Not used</param> private void ReadCommand_Execute(object obj) { try { ConfigReader reader = ConfigReader.Instance; ModbusReadCommandParameters mdb = null; if (Type == PointType.ANALOG_INPUT) { mdb = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, Address, 1); } if (Type == PointType.DIGITAL_OUTPUT) { mdb = new ModbusReadCommandParameters(6, (byte)Type, Address, 1); } if (Type == PointType.DIGITAL_INPUT) { mdb = new ModbusReadCommandParameters(6, (byte)Type, Address, 1); } if (Type == PointType.ANALOG_OUTPUT) { mdb = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, Address, 1); } ModbusFunction fn = FunctionFactory.CreateModbusFunction(mdb); this.commandExecutor.EnqueueCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
private void ReadCommand_Execute(object obj) { try { // TODO implement ModbusReadCommandParameters p = null; switch (type) { case PointType.DO_REG: p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, address, 1); break; case PointType.DI_REG: p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, address, 1); break; case PointType.IN_REG: p = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, address, 1); break; } ModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.commandExecutor.ExecuteCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <inheritdoc /> public void ExecuteReadCommand(IConfigItem configItem, ushort transactionId, byte remoteUnitAddress, ushort startAddress, ushort numberOfPoints) { ModbusReadCommandParameters p = new ModbusReadCommandParameters(6, (byte)GetReadFunctionCode(configItem.RegistryType), startAddress, numberOfPoints, transactionId, remoteUnitAddress); IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.functionExecutor.EnqueueCommand(fn); }
/// <summary> /// Method that is executed when write button is clicked on control window; /// Method should create write command parameters and provide it to FunctionFactory /// </summary> /// <param name="obj">Not used</param> private void WriteCommand_Execute(object obj) { try { ModbusWriteCommandParameters para = null; switch (type) { case PointType.DIGITAL_OUTPUT: para = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, address, commandedValue); break; case PointType.ANALOG_OUTPUT: para = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, address, commandedValue); break; default: break; } ModbusFunction fn = FunctionFactory.CreateModbusFunction(para); this.commandExecutor.EnqueueCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <summary> /// Executes an analog write command. /// </summary> /// <param name="configItem">The configuration item.</param> /// <param name="transactionId">The transaction identifier.</param> /// <param name="remoteUnitAddress">The remote unit address.</param> /// <param name="pointAddress">The point address.</param> /// <param name="value">The value.</param> private void ExecuteAnalogCommand(IConfigItem configItem, ushort transactionId, byte remoteUnitAddress, ushort pointAddress, int value) { ushort raw = eguConverter.ConvertToRaw(configItem.ScaleFactor, configItem.Deviation, value); ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, pointAddress, (ushort)raw, transactionId, remoteUnitAddress); IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.functionExecutor.EnqueueCommand(fn); }
/// <summary> /// Acquisition thread /// Awaits for trigger; /// After configured period send appropriate command to MdbSim for each point type /// /// Kao uslov za while petlju korititi acquisitionStopSignal da bi se akvizicioni thread ugasio kada se aplikacija ugasi /// </summary> private void Acquisition_DoWork() { ConfigReader cr = ConfigReader.Instance; int cnt = 0; int DigOut = cr.GetAcquisitionInterval("DigOut"); int DigIn = cr.GetAcquisitionInterval("DigIn"); int AnaOut = cr.GetAcquisitionInterval("AnaOut"); int AnaIn = cr.GetAcquisitionInterval("AnaIn"); ModbusReadCommandParameters mrcp = null; ModbusFunction fn = null; while (true) { if (cnt % DigOut == 0) { mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddress("DigOut"), cr.GetNumberOfRegisters("DigOut")); fn = FunctionFactory.CreateModbusFunction(mrcp); this.commandExecutor.EnqueueCommand(fn); } if (cnt % DigIn == 0) { mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, cr.GetStartAddress("DigIn"), cr.GetNumberOfRegisters("DigIn")); fn = FunctionFactory.CreateModbusFunction(mrcp); this.commandExecutor.EnqueueCommand(fn); } if (cnt % AnaOut == 0) { mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddress("AnaOut"), cr.GetNumberOfRegisters("AnaOut")); fn = FunctionFactory.CreateModbusFunction(mrcp); this.commandExecutor.EnqueueCommand(fn); } if (cnt % AnaIn == 0) { mrcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, cr.GetStartAddress("AnaIn"), cr.GetNumberOfRegisters("AnaIn")); fn = FunctionFactory.CreateModbusFunction(mrcp); this.commandExecutor.EnqueueCommand(fn); } try { acquisitionTrigger.WaitOne(); cnt++; } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; stateUpdater.LogMessage(message); } } }
private void Acquisition_DoWork() { int cnt = 0; ConfigReader cr = ConfigReader.Instance; int DOCnt = cr.GetAcquisitionIntervalForPointType(PointType.DIGITAL_OUTPUT); int DICnt = cr.GetAcquisitionIntervalForPointType(PointType.DIGITAL_INPUT); int AICnt = cr.GetAcquisitionIntervalForPointType(PointType.ANALOG_INPUT); int AOCnt = cr.GetAcquisitionIntervalForPointType(PointType.ANALOG_OUTPUT); while (true) { //// TODO implement if (cnt % DOCnt == 0) { // TODO implement ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddressForPointType(PointType.DIGITAL_OUTPUT), cr.GetNumberOfRegistersForPointType(PointType.DIGITAL_OUTPUT)); ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(fn); } if (cnt % DICnt == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, cr.GetStartAddressForPointType(PointType.DIGITAL_INPUT), cr.GetNumberOfRegistersForPointType(PointType.DIGITAL_INPUT)); ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(fn); } if (cnt % AOCnt == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddressForPointType(PointType.ANALOG_OUTPUT), cr.GetNumberOfRegistersForPointType(PointType.ANALOG_OUTPUT)); ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(fn); } if (cnt % AICnt == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, cr.GetStartAddressForPointType(PointType.ANALOG_INPUT), cr.GetNumberOfRegistersForPointType(PointType.ANALOG_INPUT)); ModbusFunction fn = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(fn); } try { acquisitionTrigger.WaitOne(); cnt++; } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; stateUpdater.LogMessage(message); } } }
private void WriteCommand_Execute(object obj) { try { ModbusFunction fn = FunctionFactory.CreateModbusFunction(new ModbusWriteCommandParameters(6, (Byte)ModbusFunctionCode.WRITE_SINGLE_COIL, address, rawValue)); this.commandExecutor.ExecuteCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <summary> /// Method that is executed when read button is clicked on control window; /// Method should create read command parameters and provide it to FunctionFactory /// </summary> /// <param name="obj">Not used</param> private void ReadCommand_Execute(object obj) { try { ModbusFunction fn = FunctionFactory.CreateModbusFunction(null); this.commandExecutor.EnqueueCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
private void ReadCommand_Execute(object obj) { try { ModbusReadCommandParameters parameters = new ModbusReadCommandParameters(6, (byte)Type, this.Address, 1); ModbusFunction fn = FunctionFactory.CreateModbusFunction(this.Type, CommandType.READ, parameters); this.commandExecutor.ExecuteCommand(fn); } catch (Exception ex) { this.stateUpdater.LogMessage(ex.Message); } }
private void WriteCommand_Execute(object obj) { try { // TODO implement ModbusFunction fn = FunctionFactory.CreateModbusFunction(this.Type, CommandType.WRITE, null); this.commandExecutor.ExecuteCommand(fn); } catch (Exception ex) { this.stateUpdater.LogMessage(ex.Message); } }
protected override void WriteCommand_Execute(object obj) { try { ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)GetWriteFunctionCode(type), address, (ushort)CommandedValue, configuration); IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.commandExecutor.EnqueueCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <summary> /// Acquisition thread /// Awaits for trigger; /// After configured period send appropriate command to MdbSim for each point type /// </summary> private void Acquisition_DoWork() { int cnt = 0; ConfigReader cr = ConfigReader.Instance; int DOCnt = cr.GetAcquisitionInterval("DigOut"); int AOCnt = cr.GetAcquisitionInterval("AnaOut"); while (true) { if (cnt % DOCnt == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddress("DigOut"), cr.GetNumberOfRegisters("DigOut")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % AOCnt == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddress("AnaOut"), cr.GetNumberOfRegisters("AnaOut")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } //ModbusWriteCommandParameters mwcp1 = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, cr.GetStartAddress("DigOut"), cr.GetNumberOfRegisters("DigOut")); //ModbusFunction mf1 = FunctionFactory.CreateModbusFunction(mwcp1); //this.commandExecutor.EnqueueCommand(mf1); //ModbusWriteCommandParameters mwcp2 = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, cr.GetStartAddress("AnaOut"), cr.GetNumberOfRegisters("AnaOut")); //ModbusFunction mf2 = FunctionFactory.CreateModbusFunction(mwcp2); //this.commandExecutor.EnqueueCommand(mf2); try { acquisitionTrigger.WaitOne(); cnt++; } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; stateUpdater.LogMessage(message); } } }
protected override void WriteCommand_Execute(object obj) { try { // TODO implement ushort raw = 0; raw = EGUConverter.ConvertToRaw(configItem.ScaleFactor, configItem.Deviation, CommandedValue); ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)GetWriteFunctionCode(type), address, raw, configuration); Common.IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); this.commandExecutor.EnqueueCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <summary> /// Method that is executed when read button is clicked on control window; /// Method should create read command parameters and provide it to FunctionFactory /// </summary> /// <param name="obj">Not used</param> private void ReadCommand_Execute(object obj) { try { ModbusReadCommandParameters para = null; switch (type) { case PointType.DIGITAL_OUTPUT: para = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, address, 1); break; case PointType.DIGITAL_INPUT: para = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, address, 1); break; case PointType.ANALOG_INPUT: para = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, address, 1); break; case PointType.ANALOG_OUTPUT: para = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, address, 1); break; default: break; } ModbusFunction fn = FunctionFactory.CreateModbusFunction(para); this.commandExecutor.EnqueueCommand(fn); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <summary> /// Method that is executed when write button is clicked on control window; /// Method should create write command parameters and provide it to FunctionFactory /// </summary> /// <param name="obj">Not used</param> private void WriteCommand_Execute(object obj) { try { ModbusWriteCommandParameters mwp = null; if (type == PointType.DIGITAL_OUTPUT) { mwp = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, address, rawValue); ModbusFunction fn = FunctionFactory.CreateModbusFunction(mwp); this.commandExecutor.EnqueueCommand(fn); } else if (type == PointType.ANALOG_OUTPUT) { mwp = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, address, rawValue); ModbusFunction fn = FunctionFactory.CreateModbusFunction(mwp); this.commandExecutor.EnqueueCommand(fn); } } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; this.stateUpdater.LogMessage(message); } }
/// <summary> /// Acquisition thread /// Awaits for trigger; /// After configured period send appropriate command to MdbSim for each point type /// /// Kao uslov za while petlju korititi acquisitionStopSignal da bi se akvizicioni thread ugasio kada se aplikacija ugasi /// </summary> private void Acquisition_DoWork() { try { ConfigReader reader = ConfigReader.Instance; int AO, AO1, AI, AI1, DO, DO1, DI, DI1; AO = AO1 = reader.GetAcquisitionInterval("AnaOut"); AI = AI1 = reader.GetAcquisitionInterval("AnaIn"); DO = DO1 = reader.GetAcquisitionInterval("DigOut"); DI = DI1 = reader.GetAcquisitionInterval("DigIn"); int cnt = 0; ModbusReadCommandParameters mbParams = null; ModbusFunction fn = null; while (acquisitionStopSignal) { if (cnt % AO == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, reader.GetStartAddress("AnaOut"), reader.GetNumberOfRegisters("AnaOut")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } if (cnt % AO1 == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, reader.GetStartAddress("AnaOut1"), reader.GetNumberOfRegisters("AnaOut1")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } if (cnt % AI == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, reader.GetStartAddress("AnaIn"), reader.GetNumberOfRegisters("AnaIn")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } if (cnt % AI1 == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, reader.GetStartAddress("AnaIn1"), reader.GetNumberOfRegisters("AnaIn1")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } if (cnt % DO == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, reader.GetStartAddress("DigOut"), reader.GetNumberOfRegisters("DigOut")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } if (cnt % DO1 == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, reader.GetStartAddress("DigOut1"), reader.GetNumberOfRegisters("DigOut1")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } if (cnt % DI == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, reader.GetStartAddress("DigIn"), reader.GetNumberOfRegisters("DigIn")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } if (cnt % DI1 == 0) { mbParams = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, reader.GetStartAddress("DigIn1"), reader.GetNumberOfRegisters("DigIn1")); fn = FunctionFactory.CreateModbusFunction(mbParams); commandExecutor.EnqueueCommand(fn); } try { ++cnt; acquisitionTrigger.WaitOne(); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; stateUpdater.LogMessage(message); } } // end while } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; stateUpdater.LogMessage(message); } }
public async Task <bool> GetWeatherForecastAsyncSimulate() { //MOCK_Start bool isFileFull = true; Dictionary <long, List <HourDataPoint> > keyValuePairs = new Dictionary <long, List <HourDataPoint> >(); //Dictionary<long, List<HourDataPoint>> keyValuePairs = cache.ReadFromFileDataPoint(); if (keyValuePairs.Count == 0) { isFileFull = false; } //MOCK_End foreach (KeyValuePair <long, IdentifiedObject> kvp in analogniStari) { List <HourDataPoint> hourDataPoints; //MOCK_Start if (!isFileFull) { //MOCK_End Forecast result = await darkSkyProxy.GetTimeMachineWeatherAsync(((Analog)kvp.Value).Longitude, ((Analog)kvp.Value).Latitude, DateTime.Now, Unit.Auto); hourDataPoints = result.Hourly.Hours.ToList(); //MOCK_Start keyValuePairs.Add(((Analog)kvp.Value).GlobalId, hourDataPoints); } else { hourDataPoints = keyValuePairs[((Analog)kvp.Value).GlobalId]; DateTimeOffset timeOffset = DateTimeOffset.Parse("00:00 AM"); foreach (HourDataPoint dataPoint in hourDataPoints) { dataPoint.Time = timeOffset; timeOffset = timeOffset.AddHours(1); } } //MOCK_End DERMSCommon.WeatherForecast.WeatherForecast weatherForecast = new DERMSCommon.WeatherForecast.WeatherForecast(1001, 1, 1, 1, 1, DateTime.Now, ""); foreach (HourDataPoint hdr in hourDataPoints) { if (hdr.Time.Hour.Equals(DateTime.Now.Hour)) { hourDataPoint = hdr; } } float vrednost = 0; vrednost = CalculateHourAhead(((Analog)kvp.Value).Name, ((Analog)kvp.Value).NormalValue, ((Analog)kvp.Value).Latitude, ((Analog)kvp.Value).Longitude); foreach (KeyValuePair <List <long>, ushort> gidoviNaAdresu in GidoviNaAdresu) { if (gidoviNaAdresu.Key[1] == (((Analog)kvp.Value).GlobalId) && ((Analog)kvp.Value).Description == "Simulation") { try { ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, gidoviNaAdresu.Value, (ushort)vrednost, configuration); Common.IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); commandExecutor.EnqueueCommand(fn); ((Analog)kvp.Value).NormalValue = vrednost; ModbusWriteCommandParameters p1 = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, (ushort)(gidoviNaAdresu.Value - 1), (ushort)vrednost, configuration); Common.IModbusFunction fn1 = FunctionFactory.CreateModbusFunction(p1); commandExecutor.EnqueueCommand(fn1); ModbusWriteCommandParameters p12 = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, (ushort)(gidoviNaAdresu.Value - 2), 0, configuration); Common.IModbusFunction fn12 = FunctionFactory.CreateModbusFunction(p12); commandExecutor.EnqueueCommand(fn12); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; return(false); } } } } //MOCK_Start if (!isFileFull) { cache.WriteToFile(keyValuePairs); } //MOCK_End //foreach (KeyValuePair<long, IdentifiedObject> kvp in digitalniStari) //{ // float vrednost = 0; // List<Forecast> fo = new List<Forecast>(); // GetWeatherForecastAsync(((Discrete)kvp.Value).Latitude, ((Discrete)kvp.Value).Longitude); // vrednost = CalculateHourAhead(((Discrete)kvp.Value).Name, ((Discrete)kvp.Value).NormalValue, ((Discrete)kvp.Value).Latitude, ((Discrete)kvp.Value).Latitude); // foreach (KeyValuePair<List<long>, ushort> gidoviNaAdresu in GidoviNaAdresu) // { // if (gidoviNaAdresu.Key.Contains(((Discrete)kvp.Value).GlobalId)) // { // ushort raw = 0; // raw = EGUConverter.ConvertToRaw(2, 5, vrednost); // ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, gidoviNaAdresu.Value, raw, configuration); // IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); // commandExecutor.EnqueueCommand(fn); // } // } //} if (analogniStari.Count.Equals(0)) { return(false); } return(true); }
/// <summary> /// Acquisition thread /// Awaits for trigger; /// After configured period send appropriate command to MdbSim for each point type /// </summary> private void Acquisition_DoWork() { int cnt = 0; ConfigReader cr = ConfigReader.Instance; int DOcnt1 = cr.GetAcquisitionInterval("DigOut1"); int DOcnt2 = cr.GetAcquisitionInterval("DigOut2"); int DIcnt1 = cr.GetAcquisitionInterval("DigIn1"); int DIcnt2 = cr.GetAcquisitionInterval("DigIn2"); int AOcnt1 = cr.GetAcquisitionInterval("AnaOut1"); int AOcnt2 = cr.GetAcquisitionInterval("AnaOut2"); int AIcnt1 = cr.GetAcquisitionInterval("AnaIn1"); int AIcnt2 = cr.GetAcquisitionInterval("AnaIn2"); while (true) { if (cnt % DOcnt1 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddress("DigOut1"), cr.GetNumberOfRegisters("DigOut1")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % DOcnt2 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_COILS, cr.GetStartAddress("DigOut2"), cr.GetNumberOfRegisters("DigOut2")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % DIcnt1 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, cr.GetStartAddress("DigIn1"), cr.GetNumberOfRegisters("DigIn1")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % DIcnt2 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS, cr.GetStartAddress("DigIn2"), cr.GetNumberOfRegisters("DigIn2")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % AOcnt1 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddress("AnaOut1"), cr.GetNumberOfRegisters("AnaOut1")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % AOcnt2 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS, cr.GetStartAddress("AnaOut2"), cr.GetNumberOfRegisters("AnaOut2")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % AIcnt1 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, cr.GetStartAddress("AnaIn1"), cr.GetNumberOfRegisters("AnaIn1")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } if (cnt % AIcnt2 == 0) { ModbusReadCommandParameters mcp = new ModbusReadCommandParameters(6, (byte)ModbusFunctionCode.READ_INPUT_REGISTERS, cr.GetStartAddress("AnaIn2"), cr.GetNumberOfRegisters("AnaIn2")); ModbusFunction mf = FunctionFactory.CreateModbusFunction(mcp); this.commandExecutor.EnqueueCommand(mf); } try { cnt++; // TODO implement acquisitionTrigger.WaitOne(); } catch (Exception ex) { string message = $"{ex.TargetSite.ReflectedType.Name}.{ex.TargetSite.Name}: {ex.Message}"; stateUpdater.LogMessage(message); } } }
public void SendListOfGenerators(Dictionary <long, double> generators) { foreach (KeyValuePair <long, double> generator in generators) { //if (analogniStari.ContainsKey(generator.Key)) //{ foreach (KeyValuePair <List <long>, ushort> gidoviNaAdresu in GidoviNaAdresu) { if (generator.Key == gidoviNaAdresu.Key[0]) { if (analogniStari.Keys.Contains(gidoviNaAdresu.Key[1])) { if (analogniStari[gidoviNaAdresu.Key[1]].Description == "Commanding") { { KeyValuePair <long, IdentifiedObject> a = analogniStari.ElementAt(gidoviNaAdresu.Value - 3000 - 1); float zbir = ((Analog)a.Value).NormalValue + (float)generator.Value * ((Analog)a.Value).NormalValue / 100; ((Analog)a.Value).NormalValue = zbir; zbir = (float)Math.Round(zbir); double vred = (generator.Value * ((Analog)a.Value).NormalValue / 100); vred = (double)Math.Round(vred); if (vred < 0) { vred = vred * (-1); } ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, gidoviNaAdresu.Value, (ushort)vred, configuration); Common.IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); commandExecutor.EnqueueCommand(fn); ModbusWriteCommandParameters p1 = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_REGISTER, (ushort)(gidoviNaAdresu.Value - 2), (ushort)zbir, configuration); Common.IModbusFunction fn1 = FunctionFactory.CreateModbusFunction(p1); commandExecutor.EnqueueCommand(fn1); } } } else if (digitalniStari.Keys.Contains(gidoviNaAdresu.Key[1])) { if (digitalniStari[gidoviNaAdresu.Key[1]].Description == "Commanding") { { ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, gidoviNaAdresu.Value, (ushort)generator.Value, configuration); Common.IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); commandExecutor.EnqueueCommand(fn); } } } } //else if (digitalniStari.ContainsKey(generator.Key)) //{ // foreach (KeyValuePair<List<long>, ushort> gidoviNaAdresu in GidoviNaAdresu) // { // if (generator.Key == gidoviNaAdresu.Key[0] && gidoviNaAdresu.Key[2] == 1) // { // ushort raw = 0; // raw = EGUConverter.ConvertToRaw(2, 5, generator.Value); // ModbusWriteCommandParameters p = new ModbusWriteCommandParameters(6, (byte)ModbusFunctionCode.WRITE_SINGLE_COIL, gidoviNaAdresu.Value, raw, configuration); // Common.IModbusFunction fn = FunctionFactory.CreateModbusFunction(p); // commandExecutor.EnqueueCommand(fn); // } // } //} } // LISTA GENERATORA KOJI SU PROMENILI FLEXIBILITY } }