mserial.h 6.0 KB

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