serialtool.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef SERIALTOOL_H
  2. #define SERIALTOOL_H
  3. #include <QObject>
  4. #include <QSerialPort>
  5. #include <QPushButton>
  6. #include <QLineEdit>
  7. #include <QTextEdit>
  8. #include <QByteArray>
  9. #include <QMutex>
  10. class SerialTool : public QObject
  11. {
  12. Q_OBJECT
  13. public:
  14. static SerialTool* getInstance(QObject *parent = nullptr, bool* success = nullptr);
  15. SerialTool(QObject *parent = nullptr);
  16. ~SerialTool();
  17. static SerialTool* instance;
  18. void setupSerialPort();
  19. void openSerialPort();
  20. void closeSerialPort();
  21. void readData();
  22. bool sendData(const QByteArray &data);
  23. void releaseInstance();
  24. bool isInUse = false;
  25. bool getIsInUse();
  26. signals:
  27. void openCloseButtonTextChanged(const QString& text);
  28. void openError();
  29. void dataReceived(const QByteArray& data);
  30. void serialPortOpened(); // 新增信号
  31. // 新增三个信号
  32. void disableButtonReceived();
  33. void buttonPressedReceived();
  34. void enableButtonReceived();
  35. public slots:
  36. void handleSendDataReques(const QByteArray &data);
  37. private:
  38. QByteArray buffer; // 确保这里声明了 buffer 变量
  39. QSerialPort serialPort;
  40. static QMutex globalMutex;
  41. };
  42. #endif // SERIALTOOL_H