mserial.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef MSEIAL_H
  2. #define MSEIAL_H
  3. #include <QObject>
  4. #include <QtSerialPort/QSerialPort>
  5. #include <QtSerialPort/QSerialPortInfo>
  6. #include <QStringList>
  7. #include <QString>
  8. #include <QByteArray>
  9. #include <QTextCodec>
  10. #include <QThread>
  11. #include <m_json.h>
  12. #include <QTimer>
  13. #include <QStandardPaths>
  14. #include <QVideoFrame>
  15. #include <mysql.h>
  16. class SerialThread: public QThread
  17. {
  18. Q_OBJECT
  19. public:
  20. // 数据缓存区
  21. QByteArray dataBuf;
  22. explicit SerialThread(QObject *parent = nullptr);
  23. ~SerialThread();
  24. void setSerialPortSettings(const QString &portName, QSerialPort::BaudRate baudRate,
  25. QSerialPort::DataBits dataBits, QSerialPort::Parity parity,
  26. QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl);
  27. void run() override;
  28. void stop();
  29. void SetLedState(bool sw){
  30. sendData(m_json.setLedState(sw));
  31. }
  32. Q_INVOKABLE void reboot(){
  33. sendData(m_json.CmdReboot());
  34. }
  35. QSerialPort& getSerialPort() {
  36. return m_serialPort;
  37. }
  38. Q_INVOKABLE void readBoardData();
  39. Q_INVOKABLE void update_dat_f_main(QString freq); //更新数据上传间隔
  40. Q_INVOKABLE void sendCmd(QString cmd_type,bool sw,QString setup);
  41. Q_INVOKABLE QStringList pathinfo(){
  42. QStringList pf_pic;
  43. pf_pic.append(qst_dirPath);
  44. pf_pic.append(QString::number(int_fileCount));
  45. pf_pic.append(QString::number(int_totalSize/1048576));
  46. return pf_pic;
  47. }
  48. Q_INVOKABLE void readPathinfo(){
  49. getDirectoryStats(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  50. }
  51. Q_INVOKABLE void removepic(){
  52. deleteAllFilesInDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  53. }
  54. Q_INVOKABLE void setCollt(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
  55. sendData(m_json.CmdParamconf(sh1,eh1,sh2,eh2,sh3,eh3));
  56. }
  57. Q_INVOKABLE void setLightness(int val){
  58. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
  59. if (qtContext.isValid()) {
  60. QJniObject jClass("com/yfkj/cb/Q6iot/Backlight");
  61. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
  62. }
  63. }
  64. signals:
  65. void dataReceived(const QByteArray &data);
  66. void dataUpload(const QByteArray &data);
  67. void updatestart(QString filename);
  68. void auto_takephoto(bool sw);
  69. void takephotodone();
  70. void disconnect_485();
  71. void autotestdone();
  72. void translatedone();
  73. void netMessage(const QByteArray &data);
  74. public slots:
  75. void sendData(const QByteArray &data);
  76. void mqtttoserial(const QByteArray &data);
  77. void getmodemresult(int res);
  78. void doWork(QVideoFrame dframe);
  79. private slots:
  80. //void readBoardData();
  81. private:
  82. void processJsonData(QByteArray &jsonData);
  83. QTimer m_dat_f;
  84. QSerialPort m_serialPort;
  85. SqliteOperator *sqlConfig;
  86. bool m_running;
  87. bool data_upload;
  88. int connect_board; //主板串口连接状态
  89. JsonFunc m_json;
  90. QString qst_dirPath;
  91. int int_fileCount;
  92. qint64 int_totalSize;
  93. void deleteAllFilesInDirectory(const QString &directoryPath)
  94. {
  95. QDir directory(directoryPath);
  96. if (!directory.exists()) {
  97. qDebug() << "Directory does not exist.";
  98. return;
  99. }
  100. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  101. foreach (const QFileInfo &fileInfo, fileInfoList) {
  102. if (fileInfo.isDir()) {
  103. // 递归删除子目录中的文件
  104. deleteAllFilesInDirectory(fileInfo.filePath());
  105. } else {
  106. // 删除文件
  107. QFile::remove(fileInfo.filePath());
  108. }
  109. }
  110. getDirectoryStats(directoryPath);
  111. }
  112. void getDirectoryStats(const QString &directoryPath)
  113. {
  114. QDir directory(directoryPath);
  115. if (!directory.exists()) {
  116. qDebug() << "Directory does not exist.";
  117. }
  118. qint64 totalSize = 0;
  119. int fileCount = 0;
  120. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  121. foreach (const QFileInfo &fileInfo, fileInfoList) {
  122. if (fileInfo.isDir()) {
  123. // 递归获取子目录的统计信息
  124. getDirectoryStats(fileInfo.filePath());
  125. } else {
  126. totalSize += fileInfo.size();
  127. fileCount++;
  128. }
  129. }
  130. qst_dirPath = directoryPath;
  131. int_fileCount = fileCount;
  132. int_totalSize = totalSize;
  133. qDebug() << "Directory: " << directoryPath;
  134. qDebug() << "File count: " << fileCount;
  135. qDebug() << "Total size: " << totalSize << " bytes";
  136. }
  137. };
  138. class Serial : public QSerialPort
  139. {
  140. Q_OBJECT
  141. public:
  142. explicit Serial(QObject *parent = 0);
  143. QTimer *m_5s;
  144. // 发送字节数
  145. int txByte = 0;
  146. // 接收字节数
  147. int rxByte = 0;
  148. // 数据缓存区
  149. QByteArray dataBuf;
  150. // 串口描述内容
  151. QStringList serialDescription;
  152. // 串口扫描函数
  153. QStringList serialScan(void);
  154. bool serialOpen(QString serialName, int baudRate);
  155. void sendData(QByteArray sendData);
  156. void serialClose();
  157. private slots:
  158. void ReadData();
  159. private:
  160. QTextCodec *tc;
  161. signals:
  162. void readSignal(int recByte);
  163. void sendSuccess(int sendByte);
  164. public slots:
  165. };
  166. #endif // MSEIAL_H