mserial.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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,QString speed);
  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. void autoFocusDone();
  75. void autoFocusStart();
  76. void signal_movy();
  77. void timeout_mov_stop();
  78. public slots:
  79. void sendData(const QByteArray &data);
  80. void mqtttoserial(const QByteArray &data);
  81. void getmodemresult(int res);
  82. void doWork(QVideoFrame dframe);
  83. private slots:
  84. //void readBoardData();
  85. private:
  86. void processJsonData(QByteArray &jsonData);
  87. QTimer m_dat_f;
  88. QTimer* timeout_mov = nullptr;
  89. QSerialPort m_serialPort;
  90. SqliteOperator *sqlConfig;
  91. bool m_running;
  92. bool data_upload;
  93. int connect_board; //主板串口连接状态
  94. JsonFunc m_json;
  95. QString qst_dirPath;
  96. int int_fileCount;
  97. qint64 int_totalSize;
  98. void deleteAllFilesInDirectory(const QString &directoryPath)
  99. {
  100. QDir directory(directoryPath);
  101. if (!directory.exists()) {
  102. qDebug() << "Directory does not exist.";
  103. return;
  104. }
  105. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  106. foreach (const QFileInfo &fileInfo, fileInfoList) {
  107. if (fileInfo.isDir()) {
  108. // 递归删除子目录中的文件
  109. deleteAllFilesInDirectory(fileInfo.filePath());
  110. } else {
  111. // 删除文件
  112. QFile::remove(fileInfo.filePath());
  113. }
  114. }
  115. getDirectoryStats(directoryPath);
  116. }
  117. void getDirectoryStats(const QString &directoryPath)
  118. {
  119. QDir directory(directoryPath);
  120. if (!directory.exists()) {
  121. qDebug() << "Directory does not exist.";
  122. }
  123. qint64 totalSize = 0;
  124. int fileCount = 0;
  125. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  126. foreach (const QFileInfo &fileInfo, fileInfoList) {
  127. if (fileInfo.isDir()) {
  128. // 递归获取子目录的统计信息
  129. getDirectoryStats(fileInfo.filePath());
  130. } else {
  131. totalSize += fileInfo.size();
  132. fileCount++;
  133. }
  134. }
  135. qst_dirPath = directoryPath;
  136. int_fileCount = fileCount;
  137. int_totalSize = totalSize;
  138. qDebug() << "Directory: " << directoryPath;
  139. qDebug() << "File count: " << fileCount;
  140. qDebug() << "Total size: " << totalSize << " bytes";
  141. }
  142. };
  143. class Serial_AT : public QSerialPort
  144. {
  145. Q_OBJECT
  146. public:
  147. explicit Serial_AT(QObject *parent = 0);
  148. QTimer *m_5s;
  149. // 发送字节数
  150. int txByte = 0;
  151. // 接收字节数
  152. int rxByte = 0;
  153. // 数据缓存区
  154. QByteArray dataBuf;
  155. // 串口描述内容
  156. QStringList serialDescription;
  157. // 串口扫描函数
  158. QStringList serialScan(void);
  159. bool serialOpen(QString serialName, int baudRate);
  160. void sendData(QByteArray sendData);
  161. void serialClose();
  162. private slots:
  163. void ReadData();
  164. private:
  165. QTextCodec *tc;
  166. QString at_str;
  167. QTimer* timeout_recv = nullptr;
  168. signals:
  169. void readSignal(int recByte);
  170. void sendSuccess(int sendByte);
  171. void imeiDone();
  172. void iccidDone();
  173. };
  174. #endif // MSEIAL_H