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.

58 line
1.4 KiB

  1. #ifndef SERIALPORTMANAGER_H
  2. #define SERIALPORTMANAGER_H
  3. #include <QDialog>
  4. #include <QSerialPort>
  5. #include <QComboBox>
  6. #include <QPushButton>
  7. #include <QFormLayout>
  8. #include <QSpinBox>
  9. #include <QLabel>
  10. class SerialPortManager : public QDialog
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit SerialPortManager(QWidget *parent = nullptr);
  15. ~SerialPortManager();
  16. // 获取串口配置
  17. QString portName() const;
  18. int baudRate() const;
  19. QSerialPort::Parity parity() const;
  20. QSerialPort::StopBits stopBits() const;
  21. int stationAddress() const;
  22. // 新增串口操作接口
  23. bool isOpen() const;
  24. void openSerialPort();
  25. void closeSerialPort();
  26. QByteArray readAll();
  27. void write(const QByteArray &data);
  28. signals:
  29. void errorOccurred(const QString &error);
  30. void logMessage(const QString &message);
  31. private slots:
  32. void refreshPorts();
  33. void onConnectClicked();
  34. void handleReadyRead();
  35. void handleError(QSerialPort::SerialPortError error);
  36. private:
  37. void populateBaudRates();
  38. void populateParities();
  39. void updateConnectionState(bool connected);
  40. QSerialPort *m_serial; // 串口对象
  41. QComboBox *m_portComboBox;
  42. QComboBox *m_baudComboBox;
  43. QComboBox *m_parityComboBox;
  44. QSpinBox *m_stationSpinBox;
  45. QPushButton *m_connectBtn; // 连接/断开按钮
  46. QLabel *m_statusLabel; // 状态标签
  47. };
  48. #endif // SERIALPORTMANAGER_H