综合平台编辑器
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

42 Zeilen
1.0 KiB

  1. #ifndef MODBUSMANAGER_H
  2. #define MODBUSMANAGER_H
  3. #include <QObject>
  4. #include <qmodbusrtuserialclient.h>
  5. #include <QTimer>
  6. #include <QMap>
  7. #include <QSerialPort>
  8. class ModbusManager : public QObject
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit ModbusManager(QObject *parent = nullptr);
  13. ~ModbusManager();
  14. bool startSimulation(const QString &serialPortName,
  15. int baudRate = 9600,
  16. int parity = QSerialPort::NoParity,
  17. int dataBits = QSerialPort::Data8,
  18. int stopBits = QSerialPort::OneStop);
  19. void stopSimulation();
  20. void writeCoil(int address, bool value);
  21. bool isRunning() const;
  22. signals:
  23. void coilUpdated(int address, bool value); // 从站数据更新
  24. void logMessage(const QString &msg); // 日志
  25. private slots:
  26. void pollModbusCoils();
  27. private:
  28. QModbusClient *m_modbusDevice = nullptr;
  29. QTimer *m_pollingTimer = nullptr;
  30. bool m_running = false;
  31. QMap<int, bool> m_coilValues;
  32. };
  33. #endif // MODBUSMANAGER_H