mserial.h 5.9 KB

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