mserial.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include <m_loger.h>
  17. #include "lwjson/lwjson.h"
  18. #include "ymodem.h"
  19. class SerialThread: public QThread
  20. {
  21. Q_OBJECT
  22. public:
  23. // 数据缓存区
  24. QByteArray dataBuf;
  25. YMODEM* getYmodem(); // 声明 getter
  26. explicit SerialThread(QObject *parent = nullptr);
  27. ~SerialThread();
  28. void setSerialPortSettings(const QString &portName, QSerialPort::BaudRate baudRate,
  29. QSerialPort::DataBits dataBits, QSerialPort::Parity parity,
  30. QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl);
  31. void init();
  32. void run() override;
  33. void stop();
  34. void SetLedState(bool sw){
  35. sendData(m_json.setLedState(sw));
  36. }
  37. Q_INVOKABLE void reboot(){
  38. sendData(m_json.CmdReboot());
  39. }
  40. QSerialPort& getSerialPort() {
  41. return m_serialPort;
  42. }
  43. Q_INVOKABLE void readBoardData();
  44. Q_INVOKABLE void uploadData_Mqtt(){
  45. m_dat_f.setInterval(0);
  46. };
  47. Q_INVOKABLE void update_dat_f_main(QString freq); //更新数据上传间隔
  48. Q_INVOKABLE void sendCmd(QString cmd_type,bool sw,QString setup,QString speed);
  49. Q_INVOKABLE QStringList pathinfo(){
  50. QStringList pf_pic;
  51. pf_pic.append(qst_dirPath);
  52. pf_pic.append(QString::number(int_fileCount));
  53. pf_pic.append(QString::number(int_totalSize/1048576));
  54. return pf_pic;
  55. }
  56. Q_INVOKABLE void readPathinfo(){
  57. analyzeDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  58. }
  59. Q_INVOKABLE void removepic(){
  60. deleteAllFilesInDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
  61. }
  62. Q_INVOKABLE void setCollt(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
  63. sendData(m_json.CmdParamconf(sh1,eh1,sh2,eh2,sh3,eh3));
  64. }
  65. Q_INVOKABLE void setCollt_min(int sh1 ,int eh1,int sh2,int eh2,int sh3,int eh3){
  66. sendData(m_json.CmdParamconf_min(sh1,eh1,sh2,eh2,sh3,eh3));
  67. }
  68. Q_INVOKABLE void setTempconf(int tpl ,int tph){
  69. sendData(m_json.CmdParamconf_temp(tpl,tph));
  70. }
  71. Q_INVOKABLE void setHt_xy_dis(int xdis ,int ydis){
  72. sendData(m_json.CmdParamconf_xy_dis(xdis,ydis));
  73. }
  74. Q_INVOKABLE void setLightness(int val){
  75. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context()); //activity
  76. if (qtContext.isValid()) {
  77. QJniObject jClass("com/yfkj/cb/Q6iot/Backlight");
  78. jClass.callMethod<void>("setScreenLight","(Landroid/content/Context;I)V",qtContext.object(),val);
  79. //jClass.callStaticMethod<void>("turnScreenOff", "()V");
  80. }
  81. #if 0
  82. if(val==-255){
  83. QJniObject::callStaticMethod<void>("GetUsbPermission",
  84. "turnScreenOff",
  85. "()V");
  86. }
  87. #endif
  88. }
  89. Q_INVOKABLE QString getId(){
  90. return param::imei;
  91. }
  92. Q_INVOKABLE QString getIccid(){
  93. return param::iccid;
  94. }
  95. Q_INVOKABLE QString getlat(){
  96. return param::gps_lat;
  97. }
  98. Q_INVOKABLE QString getlng(){
  99. return param::gps_lng;
  100. }
  101. Q_INVOKABLE QString get_app_version(){
  102. return APP_VERSION;
  103. }
  104. signals:
  105. void dataReceived(const QByteArray &data);
  106. void dataUpload(const QByteArray &data);
  107. void updatestart(QString filename);
  108. void auto_takephoto(bool sw);
  109. void takephotodone();
  110. void disconnect_485();
  111. void autotestdone();
  112. void translatedone();
  113. void netMessage(const QByteArray &data);
  114. void autoFocusDone();
  115. void autoFocusStart();
  116. void lwjsonStreamDONE(const QByteArray &data);
  117. void signal_movy(const QString stepcount);
  118. void timeout_mov_stop();
  119. void gpsSuccess();
  120. void ymodemReady(QString mtype);
  121. void ymodemProgress(int val,int tval);
  122. void ymodemFinished(int res);
  123. public slots:
  124. void sendData(const QByteArray &data);
  125. void mqtttoserial(const QByteArray &data);
  126. void getmodemresult(int res);
  127. void startYmodem(const QString path);
  128. void startUpgrade(QString path)
  129. {
  130. if(m_ymodem){
  131. m_upgrading = true;
  132. m_ymodem->start_ymodem(path);
  133. }
  134. }
  135. void recoverMcuParam();
  136. private slots:
  137. //void readBoardData();
  138. QString selectSerialPortName();
  139. void feedJsonParser(const QByteArray& data)
  140. {
  141. dataBuf.append(data);
  142. for (uint8_t b : data) {
  143. lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
  144. switch (ret){
  145. case lwjsonERRJSON: // 1, 无效的字符
  146. case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
  147. break;
  148. case lwjsonSTREAMINPROG: // 3, 正在解析中
  149. break;
  150. case lwjsonSTREAMDONE: // 4, 解析已完成
  151. LOG("lwjsonSTREAMDONE:{}",dataBuf.constData());
  152. emit lwjsonStreamDONE(dataBuf);
  153. dataBuf.clear();
  154. lwjson_stream_reset(&lwparser);
  155. break;
  156. case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
  157. ERROR("lwjsonERRMEM!");
  158. lwjson_stream_reset(&lwparser);
  159. break;
  160. default: // 流式解析时, 其它的返回值, 都不应该出现
  161. ERROR("未知错误:{:d}",static_cast<int>(ret));
  162. break;
  163. }
  164. }
  165. }
  166. private:
  167. bool m_upgrading = false;
  168. void processJsonData(const QByteArray &jsonData);
  169. void saveMcuParam(const QJsonObject &params);
  170. QTimer m_dat_f;
  171. QTimer* timeout_mov = nullptr;
  172. QSerialPort m_serialPort;
  173. YMODEM *m_ymodem; // YMODEM对象
  174. SqliteOperator *sqlConfig;
  175. mutable QMutex m_writeMutex;
  176. bool m_running;
  177. bool data_upload;
  178. int connect_board; //主板串口连接状态
  179. JsonFunc m_json;
  180. QString qst_dirPath;
  181. int int_fileCount;
  182. qint64 int_totalSize;
  183. // 用于存储目录统计结果的结构体
  184. struct DirectoryStats {
  185. qint64 totalSize = 0;
  186. int fileCount = 0;
  187. };
  188. lwjson_stream_parser_t lwparser;
  189. void deleteAllFilesInDirectory(const QString &directoryPath)
  190. {
  191. QDir directory(directoryPath);
  192. if (!directory.exists()) {
  193. qDebug() << "Directory does not exist.";
  194. return;
  195. }
  196. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  197. foreach (const QFileInfo &fileInfo, fileInfoList) {
  198. if (fileInfo.isDir()) {
  199. // 递归删除子目录中的文件
  200. deleteAllFilesInDirectory(fileInfo.filePath());
  201. } else {
  202. // 删除文件
  203. QFile::remove(fileInfo.filePath());
  204. }
  205. }
  206. analyzeDirectory(directoryPath);
  207. }
  208. // 递归获取目录统计信息
  209. DirectoryStats getDirectoryStats(const QString &directoryPath)
  210. {
  211. QDir directory(directoryPath);
  212. DirectoryStats stats;
  213. if (!directory.exists()) {
  214. qDebug() << "Directory does not exist.";
  215. }
  216. qint64 totalSize = 0;
  217. int fileCount = 0;
  218. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  219. foreach (const QFileInfo &fileInfo, fileInfoList) {
  220. if (fileInfo.isDir()) {
  221. // 递归获取子目录的统计信息
  222. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  223. stats.fileCount += subStats.fileCount;
  224. stats.totalSize += subStats.totalSize;
  225. } else {
  226. stats.fileCount++;
  227. stats.totalSize += fileInfo.size();
  228. }
  229. }
  230. return stats;
  231. qst_dirPath = directoryPath;
  232. int_fileCount = fileCount;
  233. int_totalSize = totalSize;
  234. LOG("Directory:{}",directoryPath.toStdString());
  235. LOG("File count:{}",fileCount);
  236. LOG("Total size:{} bytes",totalSize);
  237. }
  238. // 使用示例
  239. void analyzeDirectory(const QString &directoryPath) {
  240. qDebug() << "Analyzing directory:" << directoryPath;
  241. DirectoryStats stats = getDirectoryStats(directoryPath);
  242. qst_dirPath = directoryPath;
  243. int_fileCount = stats.fileCount;
  244. int_totalSize = stats.totalSize;
  245. LOG("Total files:{}",int_totalSize);
  246. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  247. }
  248. };
  249. class Serial_AT : public QSerialPort
  250. {
  251. Q_OBJECT
  252. public:
  253. explicit Serial_AT(QObject *parent = 0);
  254. QTimer *m_5s;
  255. // 发送字节数
  256. int txByte = 0;
  257. // 接收字节数
  258. int rxByte = 0;
  259. // 数据缓存区
  260. QByteArray dataBuf;
  261. // 串口描述内容
  262. QStringList serialDescription;
  263. // 串口扫描函数
  264. QStringList serialScan(void);
  265. bool serialOpen(QString serialName, int baudRate);
  266. void sendData(QByteArray sendData);
  267. void serialClose();
  268. private slots:
  269. void ReadData();
  270. private:
  271. QTextCodec *tc;
  272. QString at_str;
  273. QTimer* timeout_recv = nullptr;
  274. signals:
  275. void readSignal(int recByte);
  276. void sendSuccess(int sendByte);
  277. void imeiDone();
  278. void iccidDone();
  279. };
  280. #endif // MSEIAL_H