Browse Source

Alternate read only Modbus RTU support with the ability to read binary32 values

Neal Wilson 7 years ago
parent
commit
cc79cfe2b0
3 changed files with 2206 additions and 1387 deletions
  1. 20
    0
      config/Windows/productionroaster.xml
  2. 92
    6
      src/modbus.w
  3. 2094
    1381
      src/typica.cpp

+ 20
- 0
config/Windows/productionroaster.xml View File

@@ -378,6 +378,26 @@
378 378
 					tabControls.push(outputControl);
379 379
 				}
380 380
 			}
381
+                        else if(driverReference.driver == "modbusngport")
382
+                        {
383
+                            var device = new ModbusNG(configModel, driverIndex);
384
+                            modbusdevices.push(device);
385
+                            for(var j = 0; j < device.channelCount(); j++) {
386
+                                channels.push(device.getChannel(j));
387
+                                columnNames.push(device.channelColumnName(j));
388
+                                channelType.push("T");
389
+                                if(device.isChannelHidden(j)) {
390
+                                    channelVisibility.push(false);
391
+                                } else {
392
+                                    channelVisibility.push(true);
393
+                                    var indicator = new TemperatureDisplay;
394
+                                    temperatureDisplays.push(indicator);
395
+                                    var decorator = new WidgetDecorator(indicator, device.channelIndicatorText(j), 2);
396
+                                    device.getChannel(j).newData.connect(indicator.setValue);
397
+                                    indicatorPanel.addWidget(decorator);
398
+                                }
399
+                            }
400
+                        }
381 401
 			else if(driverReference.driver == "unsupporteddevice")
382 402
 			{
383 403
 				var device = createDevice(driverIndex);

+ 92
- 6
src/modbus.w View File

@@ -362,10 +362,11 @@ class ModbusNG : public QObject
362 362
     public:
363 363
         ModbusNG(DeviceTreeModel *model, const QModelIndex &index);
364 364
         ~ModbusNG();
365
-        QList<Channel*> channels;
366
-        QList<ModbusScanItem> scanList;
367
-        QList<QString> channelNames;
368
-        QList<bool> hiddenStates;
365
+        Q_INVOKABLE int channelCount();
366
+        Channel* getChannel(int);
367
+        Q_INVOKABLE QString channelColumnName(int);
368
+        Q_INVOKABLE QString channelIndicatorText(int);
369
+        Q_INVOKABLE bool isChannelHidden(int);
369 370
     private slots:
370 371
         void sendNextMessage();
371 372
         void timeout();
@@ -378,6 +379,11 @@ class ModbusNG : public QObject
378 379
         QTimer *commTimeout;
379 380
         int scanPosition;
380 381
         QByteArray responseBuffer;
382
+        QList<Channel*> channels;
383
+        QList<ModbusScanItem> scanList;
384
+        QList<QString> channelNames;
385
+        QList<QString> channelLabels;
386
+        QList<bool> hiddenStates;
381 387
 };
382 388
 
383 389
 @ One of the things that the old Modbus code got right was in allowing the
@@ -475,7 +481,9 @@ ModbusNG::ModbusNG(DeviceTreeModel *model, const QModelIndex &index) :
475 481
         channelNames.append(channelAttributes.value("column").toString());
476 482
         hiddenStates.append(
477 483
             channelAttributes.value("hidden").toString() == "true" ? true : false);
484
+        channelLabels.append(model->data(channelIndex, 0).toString());
478 485
     }
486
+    messageDelayTimer->start();
479 487
 }
480 488
 
481 489
 ModbusNG::~ModbusNG()
@@ -520,7 +528,6 @@ void ModbusNG::dataAvailable()
520 528
     commTimeout->stop();
521 529
     if(calculateCRC(responseBuffer) == 0)
522 530
     {
523
-        qDebug() << responseBuffer;
524 531
         quint16 intresponse;
525 532
         float floatresponse;
526 533
         char *ibytes = (char*)&intresponse;
@@ -596,4 +603,83 @@ quint16 ModbusNG::calculateCRC(QByteArray data)
596 603
         i++;
597 604
     }
598 605
     return retval;
599
-}
606
+}
607
+
608
+int ModbusNG::channelCount()
609
+{
610
+    return channels.size();
611
+}
612
+
613
+Channel* ModbusNG::getChannel(int channel)
614
+{
615
+    return channels.at(channel);
616
+}
617
+
618
+QString ModbusNG::channelColumnName(int channel)
619
+{
620
+    return channelNames.at(channel);
621
+}
622
+
623
+QString ModbusNG::channelIndicatorText(int channel)
624
+{
625
+    return channelLabels.at(channel);
626
+}
627
+
628
+bool ModbusNG::isChannelHidden(int channel)
629
+{
630
+    return hiddenStates.at(channel);
631
+}
632
+
633
+@ This class must be exposed to the host environment.
634
+
635
+@<Function prototypes for scripting@>=
636
+QScriptValue constructModbusNG(QScriptContext *context, QScriptEngine *engine);
637
+void setModbusNGProperties(QScriptValue value, QScriptEngine *engine);
638
+QScriptValue ModbusNG_getChannel(QScriptContext *context, QScriptEngine *engine);
639
+
640
+@ The host environment is informed of the constructor.
641
+
642
+@<Set up the scripting engine@>=
643
+constructor = engine->newFunction(constructModbusNG);
644
+value = engine->newQMetaObject(&ModbusNG::staticMetaObject, constructor);
645
+engine->globalObject().setProperty("ModbusNG", value);
646
+
647
+@ The constructor takes the configuration model and the index to the device as
648
+arguments.
649
+
650
+@<Functions for scripting@>=
651
+QScriptValue constructModbusNG(QScriptContext *context, QScriptEngine *engine)
652
+{
653
+    QScriptValue object;
654
+    if(context->argumentCount() == 2)
655
+    {
656
+        object = engine->newQObject(new ModbusNG(argument<DeviceTreeModel *>(0, context),
657
+                                                 argument<QModelIndex>(1, context)),
658
+                                    QScriptEngine::ScriptOwnership);
659
+        setModbusNGProperties(object, engine);
660
+    }
661
+    else
662
+    {
663
+        context->throwError("Incorrect number of arguments passed to "@|
664
+            "ModbusNG constructor.");
665
+    }
666
+    return object;
667
+}
668
+
669
+void setModbusNGProperties(QScriptValue value, QScriptEngine *engine)
670
+{
671
+    setQObjectProperties(value, engine);
672
+    value.setProperty("getChannel", engine->newFunction(ModbusNG_getChannel));
673
+}
674
+
675
+QScriptValue ModbusNG_getChannel(QScriptContext *context, QScriptEngine *engine)
676
+{
677
+    ModbusNG *self = getself<ModbusNG *>(context);
678
+    QScriptValue object;
679
+    if(self)
680
+    {
681
+        object = engine->newQObject(self->getChannel(argument<int>(0, context)));
682
+        setChannelProperties(object, engine);
683
+    }
684
+    return object;
685
+}

+ 2094
- 1381
src/typica.cpp
File diff suppressed because it is too large
View File


Loading…
Cancel
Save