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.

74 lines
1.9 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #pragma once
  2. #include <fstream>
  3. #include <functional>
  4. #include <iostream>
  5. #include <list>
  6. #include <map>
  7. #include <memory>
  8. #include <mutex>
  9. #include <set>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. //
  14. #include <winsock2.h>
  15. //
  16. #include <Windows.h>
  17. //
  18. #include <QtSerialPort/QSerialPort>
  19. #include <QtSerialPort/QSerialPortInfo>
  20. //
  21. #include "zqui\base\zqthread.hpp"
  22. #define SDK_VERSION 1
  23. namespace iflytop {
  24. using namespace std;
  25. // QT_CHARTS_USE_NAMESPACE
  26. class QTSerialChannel {
  27. function<void(uint8_t *data, size_t len)> m_rxcb;
  28. QSerialPort::DataBits m_dataBits;
  29. QSerialPort::Parity m_parity;
  30. QSerialPort::StopBits m_stopBits;
  31. QSerialPort::FlowControl m_flowControl;
  32. string m_name;
  33. unique_ptr<thread> m_thread;
  34. uint32_t m_baudRate;
  35. HANDLE m_CommHandler;
  36. bool m_isOpen = false;
  37. public:
  38. void init();
  39. virtual bool isOpen();
  40. virtual bool send(const uint8_t *data, size_t len);
  41. virtual void regRxListener(function<void(uint8_t *data, size_t len)> cb);
  42. bool open();
  43. void close();
  44. bool setBaudRate(qint32 baudRate) { m_baudRate = baudRate; }
  45. qint32 baudRate() const { return m_baudRate; }
  46. void setPortName(string name) { m_name = name; }
  47. void setDataBits(QSerialPort::DataBits dataBits) { m_dataBits = dataBits; }
  48. void setParity(QSerialPort::Parity parity) { m_parity = parity; }
  49. void setStopBits(QSerialPort::StopBits stopBits) { m_stopBits = stopBits; }
  50. void setFlowControl(QSerialPort::FlowControl flowControl) { m_flowControl = flowControl; }
  51. QSerialPort::DataBits dataBits() const { return m_dataBits; }
  52. QSerialPort::Parity parity() const { return m_parity; }
  53. QSerialPort::StopBits stopBits() const { return m_stopBits; }
  54. QSerialPort::FlowControl flowControl() const { return m_flowControl; }
  55. private:
  56. int com_receive(uint8_t *rxbuf, int rxbufsize);
  57. };
  58. } // namespace iflytop