serialport.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "serialport.h"
  2. #include <QSerialPortInfo>
  3. #include "logger.h"
  4. #include "appcontext.h"
  5. #define SERIAL_PORT_DEVICE "/dev/ttyS9"
  6. SerialPort::SerialPort(QObject *parent)
  7. : QObject{parent}
  8. {
  9. QList<QSerialPortInfo> serialPorts = QSerialPortInfo::availablePorts();
  10. for (const QSerialPortInfo &info : serialPorts) {
  11. LOG_DEBUG("Name : {}", info.portName().toUtf8().data());
  12. LOG_DEBUG("Description : {}", info.description().toUtf8().data());
  13. LOG_DEBUG("Manufacturer : {}", info.manufacturer().toUtf8().data());
  14. LOG_DEBUG("Serial Number: {}", info.serialNumber().toUtf8().data());
  15. LOG_DEBUG("Location : {}", info.systemLocation().toUtf8().data());
  16. LOG_DEBUG("Vendor ID : {}", info.vendorIdentifier());
  17. LOG_DEBUG("Product ID : {}", info.productIdentifier());
  18. LOG_DEBUG("-----------------------------------");
  19. }
  20. m_serialPort.setPortName(SERIAL_PORT_DEVICE);
  21. m_serialPort.setBaudRate(QSerialPort::Baud115200);
  22. m_serialPort.setDataBits(QSerialPort::Data8);
  23. m_serialPort.setParity(QSerialPort::NoParity);
  24. m_serialPort.setStopBits(QSerialPort::OneStop);
  25. m_serialPort.setFlowControl(QSerialPort::NoFlowControl);
  26. if (!m_serialPort.open(QIODevice::ReadOnly)) {
  27. LOG_ERROR("failed to open serial port:", m_serialPort.errorString().toUtf8().data());
  28. return;
  29. }
  30. LOG_INFO("open serial port successfully: {}", SERIAL_PORT_DEVICE);
  31. ringbuffer_init(&m_ringbuffer, 64);
  32. memset(m_ringbuffer.data, 0 , 64);
  33. connect(&m_serialPort, &QSerialPort::readyRead, [this](){
  34. QByteArray byteArray = m_serialPort.readAll();
  35. LOG_DEBUG("serial received: {}", byteArray.toHex().data()); // 或直接输出 data
  36. ringbuffer_append(&m_ringbuffer, (data_t*)byteArray.data(), byteArray.size());
  37. int ret;
  38. int frameSize;
  39. data_t head[2];
  40. data_t data;
  41. data_t buffer[12];
  42. data_t checkSum;
  43. while(1)
  44. {
  45. if(m_ringbuffer.buffer_size < 12)
  46. break;
  47. if(0 == ringbuffer_data(m_ringbuffer, &head[0], 0) && 0 == ringbuffer_data(m_ringbuffer, &head[1], 1))
  48. {
  49. if(0xAA == head[0] && 0x55 == head[1])
  50. {
  51. ringbuffer_data(m_ringbuffer, &data, 2);
  52. frameSize = data;
  53. if(data != 12)
  54. {
  55. LOG_ERROR("frame size error");
  56. ringbuffer_popup(&m_ringbuffer, 2);
  57. }
  58. else
  59. {
  60. int size = ringbuffer_read(m_ringbuffer, buffer, frameSize);
  61. if(size == frameSize)
  62. {
  63. checkSum = 0x00;
  64. for(int i = 0; i < size - 3; i++)
  65. {
  66. checkSum += buffer[i];
  67. }
  68. if(buffer[3] != 0x11)
  69. {
  70. LOG_ERROR("cmd code error");
  71. ringbuffer_popup(&m_ringbuffer, 2);
  72. continue;
  73. }
  74. if(checkSum != buffer[size - 3])
  75. {
  76. LOG_ERROR("check sum error");
  77. ringbuffer_popup(&m_ringbuffer, 2);
  78. continue;
  79. }
  80. if(0xBB != buffer[size - 2] || 0x66 != buffer[size - 1])
  81. {
  82. LOG_ERROR("tail error");
  83. ringbuffer_popup(&m_ringbuffer, 2);
  84. continue;
  85. }
  86. if(buffer[4] == 0x00) {
  87. emit pageSwitch(-1);
  88. }
  89. else if(buffer[4] == 0x01) {
  90. emit pageSwitch();
  91. }
  92. LOG_DEBUG("key state : {:x}", buffer[4]);
  93. LOG_DEBUG("heating state : {:x}", buffer[5]);
  94. LOG_DEBUG("screen temperature : {}", static_cast<int8_t>(buffer[6]));
  95. LOG_DEBUG("Interface onboard temperature: {}", static_cast<int8_t>(buffer[7]));
  96. ringbuffer_popup(&m_ringbuffer, size);
  97. MCUModule &mcuModule = AppContext::instance().mcuModule();
  98. if(buffer[5]) {
  99. mcuModule.setIsHeatng(true);
  100. } else {
  101. mcuModule.setIsHeatng(false);
  102. }
  103. mcuModule.setScreenTemp(static_cast<int8_t>(buffer[6]));
  104. mcuModule.setInterfaceTemp(static_cast<int8_t>(buffer[7]));
  105. }
  106. }
  107. }
  108. else
  109. {
  110. ringbuffer_popup(&m_ringbuffer, 1);
  111. }
  112. }
  113. }
  114. });
  115. }
  116. SerialPort::~SerialPort()
  117. {
  118. }