综合平台编程器项目的远程存储
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 rivejä
3.6 KiB

  1. #pragma once
  2. #include "services/plc_communication_gateway.h"
  3. #include "domain/register_repository.h"
  4. #include <QModbusDevice>
  5. #include <QObject>
  6. #include <QTimer>
  7. #include <cstdint>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. class PlcRegisterRepository;
  12. class QModbusReply;
  13. class QModbusRtuSerialMaster;
  14. struct PlcCommunicationFailure;
  15. class PlcCommunicationService final : public QObject, public PlcCommunicationGateway
  16. {
  17. Q_OBJECT
  18. public:
  19. explicit PlcCommunicationService(
  20. PlcRegisterRepository &repository,
  21. QObject *parent = nullptr);
  22. ~PlcCommunicationService() override;
  23. PlcCommunicationResult connectDevice(
  24. const PlcSerialConfiguration &configuration) override;
  25. void disconnectDevice() override;
  26. PlcCommunicationResult setPollAddresses(
  27. const std::vector<RegisterAddress> &addresses) override;
  28. PlcConnectionState state() const override;
  29. bool initialReadCompleted() const override;
  30. PlcCommunicationError lastErrorType() const override;
  31. const std::string &lastError() const override;
  32. const PlcSerialConfiguration &configuration() const;
  33. void setCallbacks(
  34. std::function<void()> state_changed,
  35. std::function<void(bool)> initial_read_changed,
  36. std::function<void()> cache_updated,
  37. std::function<void(const std::string &)> error_reported) override;
  38. signals:
  39. void stateChanged();
  40. void initialReadCompletedChanged(bool completed);
  41. void cacheUpdated();
  42. void communicationError(const QString &message);
  43. private:
  44. struct PollBlock
  45. {
  46. RegisterArea area = RegisterArea::M;
  47. int startAddress = 0;
  48. int count = 1;
  49. };
  50. void rebuildPollBlocks();
  51. void applyPollAddresses(const std::vector<RegisterAddress> &addresses);
  52. void pollNextBlock();
  53. void probeRecovery();
  54. void handleRecoveryProbeFailure(QModbusDevice::Error error);
  55. void restoreCommunication();
  56. void handleReadFinished(QModbusReply *reply, PollBlock block);
  57. RegisterWriteResult sendBitWrite(const RegisterAddress &address, bool value);
  58. RegisterWriteResult sendWordWrite(
  59. const RegisterAddress &address, std::int16_t value);
  60. void updateInitialReadCompleted(bool completed, bool force_notification = false);
  61. void handleUnexpectedDisconnect();
  62. void handleModbusError(QModbusDevice::Error error);
  63. void closeSerialSession();
  64. void setState(PlcConnectionState state);
  65. void setError(const PlcCommunicationFailure &failure);
  66. PlcRegisterRepository &repository_;
  67. std::unique_ptr<QModbusRtuSerialMaster> master_;
  68. QTimer poll_timer_;
  69. QTimer recovery_timer_;
  70. PlcSerialConfiguration configuration_;
  71. std::vector<RegisterAddress> poll_addresses_;
  72. std::vector<RegisterAddress> pending_poll_addresses_;
  73. std::vector<PollBlock> poll_blocks_;
  74. std::size_t next_poll_block_ = 0;
  75. QModbusReply *pending_reply_ = nullptr;
  76. QModbusReply *pending_write_reply_ = nullptr;
  77. bool poll_update_pending_ = false;
  78. bool disconnecting_ = false;
  79. bool serial_session_opened_ = false;
  80. bool received_valid_response_ = false;
  81. std::uint64_t connection_generation_ = 0;
  82. PlcConnectionState state_ = PlcConnectionState::Disconnected;
  83. bool initial_read_completed_ = false;
  84. std::vector<bool> initial_blocks_read_;
  85. PlcCommunicationError last_error_type_ = PlcCommunicationError::None;
  86. std::string last_error_;
  87. std::function<void()> state_changed_callback_;
  88. std::function<void(bool)> initial_read_changed_callback_;
  89. std::function<void()> cache_updated_callback_;
  90. std::function<void(const std::string &)> error_reported_callback_;
  91. };