@@ -17,12 +17,12 @@ QVersionNumber FormModSim::VERSION = QVersionNumber(1, 1);
1717// / \param num
1818// / \param parent
1919// /
20- FormModSim::FormModSim (int id, ModbusMultiServer& server, MainWindow* parent) :
20+ FormModSim::FormModSim (int id, ModbusMultiServer& server, QSharedPointer<DataSimulator> simulator, MainWindow* parent) :
2121 QWidget(parent)
2222 , ui(new Ui::FormModSim)
2323 ,_formId(id)
2424 ,_mbMultiServer(server)
25- ,_dataSimulator(new DataSimulator( this ) )
25+ ,_dataSimulator(simulator )
2626{
2727 Q_ASSERT (parent != nullptr );
2828
@@ -344,29 +344,38 @@ void FormModSim::print(QPrinter* printer)
344344}
345345
346346// /
347- // / \brief FormModSim::resumeSimulations
347+ // / \brief FormModSim::simulationMap
348+ // / \return
348349// /
349- void FormModSim::resumeSimulations ()
350+ ModbusSimulationMap FormModSim::simulationMap () const
350351{
351- _dataSimulator->resumeSimulations ();
352- }
352+ const auto dd = displayDefinition ();
353+ const auto startAddr = dd.PointAddress - 1 ;
354+ const auto endAddr = startAddr + dd.Length ;
353355
354- // /
355- // / \brief FormModSim::pauseSimulations
356- // /
357- void FormModSim::pauseSimulations ()
358- {
359- _dataSimulator->pauseSimulations ();
356+ ModbusSimulationMap result;
357+ const auto simulationMap = _dataSimulator->simulationMap ();
358+ for (auto && key : _dataSimulator->simulationMap ().keys ())
359+ {
360+ if (key.first == dd.PointType &&
361+ key.second >= startAddr && key.second < endAddr)
362+ {
363+ result[key] = simulationMap[key];
364+ }
365+ }
366+
367+ return result;
360368}
361369
362370// /
363- // / \brief FormModSca::restartSimulations
371+ // / \brief FormModSim::startSimulation
372+ // / \param type
373+ // / \param addr
374+ // / \param params
364375// /
365- void FormModSim::restartSimulations ( )
376+ void FormModSim::startSimulation (QModbusDataUnit::RegisterType type, quint16 addr, const ModbusSimulationParams& params )
366377{
367- _dataSimulator->stopSimulations ();
368- for (auto && k : _simulationMap.keys ())
369- _dataSimulator->startSimulation (dataDisplayMode (), k.first , k.second , _simulationMap[k]);
378+ _dataSimulator->startSimulation (dataDisplayMode (), type, addr, params);
370379}
371380
372381// /
@@ -449,12 +458,9 @@ void FormModSim::onDefinitionChanged()
449458// /
450459void FormModSim::on_outputWidget_itemDoubleClicked (quint16 addr, const QVariant& value)
451460{
452- if (!_mbMultiServer.isConnected ())
453- return ;
454-
455461 const auto mode = dataDisplayMode ();
456462 const auto pointType = ui->comboBoxModbusPointType ->currentPointType ();
457- auto & simParams = _simulationMap[{ pointType, addr}] ;
463+ auto simParams = _dataSimulator-> simulationParams ( pointType, addr) ;
458464
459465 switch (pointType)
460466 {
@@ -526,7 +532,6 @@ void FormModSim::on_mbDeviceIdChanged(quint8 deviceId)
526532// /
527533void FormModSim::on_mbConnected (const ConnectionDetails&)
528534{
529- _dataSimulator->resumeSimulations ();
530535 updateStatus ();
531536}
532537
@@ -535,9 +540,6 @@ void FormModSim::on_mbConnected(const ConnectionDetails&)
535540// /
536541void FormModSim::on_mbDisconnected (const ConnectionDetails&)
537542{
538- if (!_mbMultiServer.isConnected ())
539- _dataSimulator->pauseSimulations ();
540-
541543 updateStatus ();
542544}
543545
@@ -579,8 +581,5 @@ void FormModSim::on_mbDataChanged(const QModbusDataUnit&)
579581// /
580582void FormModSim::on_dataSimulated (DataDisplayMode mode, QModbusDataUnit::RegisterType type, quint16 addr, QVariant value)
581583{
582- if (!_mbMultiServer.isConnected ())
583- return ;
584-
585584 _mbMultiServer.writeRegister (type, { addr, value, mode, byteOrder () });
586585}
0 commit comments