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.

77 lines
2.0 KiB

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 "zqthread.hpp"
  22. #include "idatachannel.hpp"
  23. #define SDK_VERSION 1
  24. namespace iflytop {
  25. using namespace std;
  26. // QT_CHARTS_USE_NAMESPACE
  27. typedef function<void(bool connect)> device_state_cb_t;
  28. class QTDataChannel : public IDataChannel {
  29. function<void(uint8_t *data, size_t len)> m_rxcb;
  30. QSerialPort::DataBits m_dataBits;
  31. QSerialPort::Parity m_parity;
  32. QSerialPort::StopBits m_stopBits;
  33. QSerialPort::FlowControl m_flowControl;
  34. string m_name;
  35. unique_ptr<thread> m_thread;
  36. uint32_t m_baudRate;
  37. HANDLE m_CommHandler;
  38. bool m_isOpen = false;
  39. public:
  40. void init();
  41. virtual bool isOpen() override;
  42. virtual bool send(const uint8_t *data, size_t len) override;
  43. virtual void regRxListener(function<void(uint8_t *data, size_t len)> cb) override;
  44. bool open();
  45. void close();
  46. void setBaudRate(qint32 baudRate) { m_baudRate = baudRate; }
  47. qint32 baudRate() const { return m_baudRate; }
  48. void setPortName(string name) { m_name = name; }
  49. void setDataBits(QSerialPort::DataBits dataBits) { m_dataBits = dataBits; }
  50. void setParity(QSerialPort::Parity parity) { m_parity = parity; }
  51. void setStopBits(QSerialPort::StopBits stopBits) { m_stopBits = stopBits; }
  52. void setFlowControl(QSerialPort::FlowControl flowControl) { m_flowControl = flowControl; }
  53. QSerialPort::DataBits dataBits() const { return m_dataBits; }
  54. QSerialPort::Parity parity() const { return m_parity; }
  55. QSerialPort::StopBits stopBits() const { return m_stopBits; }
  56. QSerialPort::FlowControl flowControl() const { return m_flowControl; }
  57. private:
  58. int com_receive(uint8_t *rxbuf, int rxbufsize);
  59. };
  60. } // namespace iflytop