serialtool.cpp 3.2 KB

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