mserial.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. Q_INVOKABLE void recoverMcuParam();
  136. private slots:
  137. //void readBoardData();
  138. QString selectSerialPortName();
  139. void feedJsonParser(const QByteArray& data)
  140. {
  141. for (uint8_t b : data) {
  142. lwjsonr_t ret = lwjson_stream_parse(&lwparser, b);
  143. // 只在解析进行中或完成时跟踪字节,避免累积无关数据
  144. if (ret == lwjsonSTREAMINPROG || ret == lwjsonSTREAMDONE) {
  145. jsonBuf.append(static_cast<char>(b));
  146. }
  147. switch (ret){
  148. case lwjsonERRJSON: // 1, 无效的字符
  149. jsonBuf.clear();
  150. break;
  151. case lwjsonSTREAMWAITFIRSTCHAR: // 2, 等待首字符
  152. break;
  153. case lwjsonSTREAMINPROG: // 3, 正在解析中
  154. break;
  155. case lwjsonSTREAMDONE: // 4, 解析已完成
  156. LOG("lwjsonSTREAMDONE:{}",jsonBuf.constData());
  157. emit lwjsonStreamDONE(jsonBuf);
  158. jsonBuf.clear();
  159. lwjson_stream_reset(&lwparser);
  160. break;
  161. case lwjsonERRMEM: // 5, 嵌套的太深("LWJSON_CFG_STREAM_STACK_SIZE")
  162. ERROR("lwjsonERRMEM!");
  163. jsonBuf.clear();
  164. lwjson_stream_reset(&lwparser);
  165. break;
  166. default: // 流式解析时, 其它的返回值, 都不应该出现
  167. ERROR("未知错误:{:d}",static_cast<int>(ret));
  168. break;
  169. }
  170. }
  171. }
  172. private:
  173. bool m_upgrading = false;
  174. void processJsonData(const QByteArray &jsonData);
  175. void saveMcuParam(const QJsonObject &params);
  176. QTimer m_dat_f;
  177. QTimer* timeout_mov = nullptr;
  178. QSerialPort m_serialPort;
  179. YMODEM *m_ymodem; // YMODEM对象
  180. SqliteOperator *sqlConfig;
  181. mutable QMutex m_writeMutex;
  182. bool m_running;
  183. bool data_upload;
  184. int connect_board; //主板串口连接状态
  185. JsonFunc m_json;
  186. QString qst_dirPath;
  187. int int_fileCount;
  188. qint64 int_totalSize;
  189. // 用于存储目录统计结果的结构体
  190. struct DirectoryStats {
  191. qint64 totalSize = 0;
  192. int fileCount = 0;
  193. };
  194. lwjson_stream_parser_t lwparser;
  195. QByteArray jsonBuf;
  196. void deleteAllFilesInDirectory(const QString &directoryPath)
  197. {
  198. QDir directory(directoryPath);
  199. if (!directory.exists()) {
  200. qDebug() << "Directory does not exist.";
  201. return;
  202. }
  203. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  204. foreach (const QFileInfo &fileInfo, fileInfoList) {
  205. if (fileInfo.isDir()) {
  206. // 递归删除子目录中的文件
  207. deleteAllFilesInDirectory(fileInfo.filePath());
  208. } else {
  209. // 删除文件
  210. QFile::remove(fileInfo.filePath());
  211. }
  212. }
  213. analyzeDirectory(directoryPath);
  214. }
  215. // 递归获取目录统计信息
  216. DirectoryStats getDirectoryStats(const QString &directoryPath)
  217. {
  218. QDir directory(directoryPath);
  219. DirectoryStats stats;
  220. if (!directory.exists()) {
  221. qDebug() << "Directory does not exist.";
  222. }
  223. qint64 totalSize = 0;
  224. int fileCount = 0;
  225. QFileInfoList fileInfoList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
  226. foreach (const QFileInfo &fileInfo, fileInfoList) {
  227. if (fileInfo.isDir()) {
  228. // 递归获取子目录的统计信息
  229. DirectoryStats subStats = getDirectoryStats(fileInfo.filePath());
  230. stats.fileCount += subStats.fileCount;
  231. stats.totalSize += subStats.totalSize;
  232. } else {
  233. stats.fileCount++;
  234. stats.totalSize += fileInfo.size();
  235. }
  236. }
  237. return stats;
  238. qst_dirPath = directoryPath;
  239. int_fileCount = fileCount;
  240. int_totalSize = totalSize;
  241. LOG("Directory:{}",directoryPath.toStdString());
  242. LOG("File count:{}",fileCount);
  243. LOG("Total size:{} bytes",totalSize);
  244. }
  245. // 使用示例
  246. void analyzeDirectory(const QString &directoryPath) {
  247. qDebug() << "Analyzing directory:" << directoryPath;
  248. DirectoryStats stats = getDirectoryStats(directoryPath);
  249. qst_dirPath = directoryPath;
  250. int_fileCount = stats.fileCount;
  251. int_totalSize = stats.totalSize;
  252. LOG("Total files:{}",int_totalSize);
  253. LOG("Total size:{} MB",stats.totalSize / (1024.0 * 1024.0));
  254. }
  255. };
  256. class Serial_AT : public QSerialPort
  257. {
  258. Q_OBJECT
  259. public:
  260. explicit Serial_AT(QObject *parent = 0);
  261. QTimer *m_5s;
  262. // 发送字节数
  263. int txByte = 0;
  264. // 接收字节数
  265. int rxByte = 0;
  266. // 数据缓存区
  267. QByteArray dataBuf;
  268. // 串口描述内容
  269. QStringList serialDescription;
  270. // 串口扫描函数
  271. QStringList serialScan(void);
  272. bool serialOpen(QString serialName, int baudRate);
  273. void sendData(QByteArray sendData);
  274. void serialClose();
  275. private slots:
  276. void ReadData();
  277. private:
  278. QTextCodec *tc;
  279. QString at_str;
  280. QTimer* timeout_recv = nullptr;
  281. signals:
  282. void readSignal(int recByte);
  283. void sendSuccess(int sendByte);
  284. void imeiDone();
  285. void iccidDone();
  286. };
  287. #endif // MSEIAL_H