serialtool.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "SerialTool.h"
  2. #include <QDebug>
  3. #include "../global.h"
  4. #include "../logger.h"
  5. // 定义静态成员变量并初始化为 nullptr
  6. SerialTool *SerialTool::instance = nullptr;
  7. QMutex SerialTool::globalMutex;
  8. SerialTool::SerialTool(QObject *parent) : QObject(parent) {}
  9. SerialTool::~SerialTool() {
  10. if (serialPort.isOpen()) {
  11. serialPort.close();
  12. }
  13. }
  14. SerialTool *SerialTool::getInstance(QObject *parent, bool *success) {
  15. if (instance == nullptr) {
  16. instance = new SerialTool(parent);
  17. }
  18. if (instance->isInUse) {
  19. if (success) {
  20. *success = false;
  21. }
  22. return nullptr;
  23. }
  24. instance->isInUse = true;
  25. if (success) {
  26. *success = true;
  27. }
  28. // globalMutex.unlock();
  29. return instance;
  30. }
  31. // 打开串口的函数
  32. void SerialTool::openSerialPort() {
  33. const QString portName = firewidgetPort;
  34. const qint32 baudRate = 9600;
  35. if (!serialPort.isOpen()) {
  36. serialPort.setPortName(portName);
  37. serialPort.setBaudRate(baudRate);
  38. serialPort.setDataBits(QSerialPort::Data8);
  39. serialPort.setStopBits(QSerialPort::OneStop);
  40. serialPort.setParity(QSerialPort::NoParity);
  41. if (serialPort.open(QIODevice::ReadWrite)) {
  42. Logger::getInstance().info(QString("串口 %1 打开成功").arg(portName));
  43. emit serialPortOpened(); // 发射新信号
  44. } else {
  45. Logger::getInstance().error(QString("串口 %1 打开错误").arg(portName));
  46. emit openError();
  47. }
  48. }
  49. }
  50. // 关闭串口的函数
  51. void SerialTool::closeSerialPort() {
  52. if (serialPort.isOpen()) {
  53. serialPort.close();
  54. emit openCloseButtonTextChanged("打开串口");
  55. }
  56. }
  57. bool SerialTool::sendData(const QByteArray &data) {
  58. if (serialPort.isOpen()) {
  59. qint64 bytesWritten = serialPort.write(data);
  60. return bytesWritten == data.size();
  61. }
  62. return false;
  63. }
  64. void SerialTool::handleSendDataReques(const QByteArray &data) { sendData(data); }
  65. void SerialTool::readData() {
  66. QByteArray newData = serialPort.readAll();
  67. buffer.append(newData);
  68. int startIndex = buffer.indexOf("\r\n");
  69. while (startIndex != -1) {
  70. int endIndex = buffer.indexOf("\r\n", startIndex + 2);
  71. if (endIndex != -1) {
  72. QByteArray command = buffer.mid(startIndex + 2, endIndex - startIndex - 2);
  73. emit dataReceived(newData);
  74. // 根据 command 的值发射相应的信号
  75. if (command == "BUTTON_DISABLED") {
  76. emit disableButtonReceived();
  77. } else if (command == "BUTTON_PRESSED") {
  78. emit buttonPressedReceived();
  79. } else if (command == "BUTTON_ENABLE") {
  80. emit enableButtonReceived();
  81. }
  82. buffer = buffer.mid(endIndex + 2);
  83. } else {
  84. break;
  85. }
  86. startIndex = buffer.indexOf("\r\n");
  87. }
  88. }
  89. void SerialTool::setupSerialPort() {
  90. openSerialPort();
  91. connect(&serialPort, &QSerialPort::readyRead, this, &SerialTool::readData);
  92. }
  93. void SerialTool::releaseInstance() { isInUse = false; }
  94. bool SerialTool::getIsInUse() { return isInUse; }