check.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  1. #include "board.h"
  2. #include "base_types.h"
  3. #include "check.h"
  4. #include "param.h"
  5. #include "lcd_disp.h"
  6. #include "tdc_tof.h"
  7. #include "prj_com.h"
  8. #include "stat.h"
  9. #include "app_nb.h"
  10. #include "alarm.h"
  11. #include "bill.h"
  12. float RefFlow = 2000;
  13. extern void SaveParam(void);
  14. static volatile check_t Check;
  15. static volatile uint16_t rx_state;
  16. static volatile uint16_t rx_data_len;
  17. static volatile uint8_t cmd;
  18. uint8_t G_STATIC_OT_SETTING_FLAG = 0; // 20260429 正在设置静态OT,不进行飞行时间差滤波
  19. // add by yuewei 20260424, for lcd display "ot-Err"
  20. uint8_t G_OT_ERROR_FLAG_CNT = 0;
  21. // add by yuewei 20260318, for lcd display remain time
  22. uint16_t ot_count_down = 0;
  23. // add by yuewei 20260309,to tell rtc hongwai start receive long data
  24. uint8_t HONGWAI_LONG_RX_FLAG = 0; // defalt:0,long data:1
  25. uint32_t GetCheckState(void)
  26. {
  27. return Check.state;
  28. }
  29. uint32_t GetCheckPulse(void)
  30. {
  31. return Check.total_pulse;
  32. }
  33. void SetCheckFlow(double cum_flow, float spdLvl, float temp)
  34. {
  35. if (Check.state == CHECKED)
  36. {
  37. Check.start_cum_flow = cum_flow; // unit: cm^3
  38. Check.curr_cum_flow = cum_flow; // unit: cm^3
  39. Check.flow_cnt = 0;
  40. Check.total_spd_lvl = 0.0f;
  41. Check.total_water_temp = 0.0f;
  42. return;
  43. }
  44. Param_t *param;
  45. Check.curr_cum_flow = cum_flow; // unit: cm^3
  46. Check.total_spd_lvl += spdLvl;
  47. Check.total_water_temp += temp;
  48. Check.flow_cnt++;
  49. GetParam(&param);
  50. // modified by yuewei 20260316 to read in normal mode, only use check_spdLvl_c[0]
  51. // uint16_t temp_index = CheckTempRangeIndex((float)(Check.total_water_temp / Check.flow_cnt));
  52. uint16_t temp_index = 0;
  53. CheckSpdLvlC_t *p = &(param->check_spdLvl_c[temp_index]);
  54. uint8_t c_idx = CheckCValueIndex(temp_index, (uint16_t)(Check.total_spd_lvl / Check.flow_cnt));
  55. LOG("++++++++++++++++++++++c_idx=%d\n",c_idx);
  56. // Pb = Vs*6400*q/C Vs unit: L 6.4 = 6400 / 1000
  57. // modified by yuewei 20260317 change q_value to q
  58. // Check.total_pulse = (uint32_t)((Check.curr_cum_flow - Check.start_cum_flow) / p->cur_c[c_idx] * 6.4 * param->q_value);
  59. Check.total_pulse = (uint32_t)((Check.curr_cum_flow - Check.start_cum_flow) / p->cur_c[c_idx] * 6.4 * param->q);
  60. }
  61. void check_init(void)
  62. {
  63. Check.state = CHECKED;
  64. Finsh_Uart_Mode |= CHECK_ITF_ON;
  65. Check.rx_idx = 0;
  66. Check.rx_over_time = 0;
  67. Check.rx_err = 0;
  68. Check.start_cum_flow = 0.0f;
  69. Check.curr_cum_flow = 0.0f;
  70. Check.flow_cnt = 0;
  71. Check.flow_rate = 0;
  72. Check.c_point_idx = 0;
  73. Check.total_pulse = 0;
  74. Check.checking_cnt = 0;
  75. Check.start_time = 0;
  76. Check.end_time = 0;
  77. rx_data_len = 0;
  78. rx_state = CHECK_RX_HEAD;
  79. }
  80. void CheckRxErr(void)
  81. {
  82. HONGWAI_LONG_RX_FLAG = 0; // add by yuewei 20260309 to deal hongwai long data
  83. /////////////////////////////
  84. if (Check.rx_idx <= 4) // one byte recieved, it's interference, ignore it wxl2026-6-1
  85. {
  86. rx_data_len = 0;
  87. rx_state = CHECK_RX_HEAD;
  88. return;
  89. }
  90. ////////////////////////////////
  91. // Check.tx_buf[0]='Y';
  92. // Check.tx_buf[1]='0'+Check.rx_idx;
  93. // Check.tx_buf[2]=' ';
  94. // finsh_tx(Check.tx_buf, 3);
  95. Check.rx_err |= CHECK_RX_ERROR;
  96. //////////////////
  97. // LOG("-%x-\r\n",Check.rx_buf[0]);
  98. ////////////////////
  99. Check.rx_idx = 0;
  100. if (Check.state == CHECKED || Check.state == CHECKING) // add by HBR 2026052109 to CHECKING ERR_H
  101. {
  102. lcd_set_timer(2); // modifed by yuewei 30->2, to display Err-H 5 Seconds
  103. }
  104. rx_data_len = 0;
  105. rx_state = CHECK_RX_HEAD;
  106. }
  107. void CheckRxData(volatile uint8_t data)
  108. {
  109. // volatile uint8_t cmd;
  110. uint8_t crc;
  111. uint16_t crc16; // add by yuewei 20260305
  112. if (finsh_txing_flag == 1)
  113. {
  114. return;
  115. }
  116. if (rx_state == CHECK_RX_HEAD)
  117. {
  118. if (data == 0x68)
  119. {
  120. Check.rx_buf[Check.rx_idx++] = data;
  121. rx_state = CHECK_RX_CMD;
  122. }
  123. else if (data == 0xfe)
  124. {
  125. return;
  126. }
  127. else
  128. {
  129. // LOG("----------------------------1\n");
  130. // LOG("Check.rx_idx=%d,data=%x\n",Check.rx_idx,data);
  131. if (Check.rx_idx >= 2)
  132. {
  133. CheckRxErr();
  134. }
  135. return;
  136. }
  137. }
  138. else if (rx_state == CHECK_RX_CMD)
  139. {
  140. if (data >= 0x00)
  141. {
  142. Check.rx_buf[Check.rx_idx++] = data;
  143. // add by yuewei 20260317
  144. if (data == 0xA5)
  145. {
  146. rx_state = CHECK_RX_CRC_MODBUS;
  147. }
  148. else
  149. {
  150. rx_state = CHECK_RX_S_ADDR;
  151. }
  152. }
  153. else
  154. {
  155. CheckRxErr();
  156. // LOG("----------------------------2\n");
  157. return;
  158. }
  159. }
  160. else if (rx_state == CHECK_RX_S_ADDR)
  161. {
  162. Check.rx_buf[Check.rx_idx++] = data;
  163. if (Check.rx_idx >= 5)
  164. {
  165. rx_state = CHECK_RX_D_ADDR;
  166. }
  167. }
  168. else if (rx_state == CHECK_RX_D_ADDR)
  169. {
  170. Check.rx_buf[Check.rx_idx++] = data;
  171. if (Check.rx_idx >= 8)
  172. {
  173. cmd = Check.rx_buf[1];
  174. if (cmd == 0x00 || cmd == 0x10 || cmd == 0x11 || cmd == 0x12 // add by yuewei 20260316
  175. || cmd == 0x13 || cmd == 0x14 || cmd == 0x15 || cmd == 0x16 // add by yuewei 20260331
  176. || cmd == 0x17 || cmd == 0x18 || cmd == 0x24 || cmd == 0x26 // add by yuewei 20260331
  177. || cmd == 0x3D || cmd == 0x65 || cmd == 0x67 || cmd == 0x69 // add by yuewei 20260305
  178. || cmd == 0x53 || cmd == 0x63 || cmd == 0x64 || cmd == 0x6A || cmd == 0xE1 || cmd == 0xE2 || cmd == 0x02 || cmd == 0x03)
  179. { // add by yuewei 20260414 cmd = 0x02 ox03
  180. rx_state = CHECK_RX_LEN;
  181. }
  182. else
  183. {
  184. rx_state = CHECK_RX_CRC;
  185. }
  186. }
  187. }
  188. else if (rx_state == CHECK_RX_LEN)
  189. {
  190. Check.rx_buf[Check.rx_idx++] = data;
  191. // modified by yuewei 20260304 start
  192. /*
  193. if (data < (CHECK_RX_BUF_LEN - 11)) {
  194. rx_data_len = data;
  195. rx_state = CHECK_RX_DATA;
  196. } else {
  197. CheckRxErr();
  198. }
  199. */
  200. if ((data == 0xF0) && (cmd == 0x69))
  201. {
  202. HONGWAI_LONG_RX_FLAG = 1; // add by yuewei 20260309 to deal hongwai long data
  203. rx_data_len = 502; // 设置口径和温度补偿值
  204. }
  205. else if ((data == 0xFF) && (cmd == 0x03))
  206. {
  207. HONGWAI_LONG_RX_FLAG = 1; // add by yuewei 20260309 to deal hongwai long data
  208. rx_data_len = 512 + 4; // 表端获取升级包,特殊处理512+4->256+4
  209. }
  210. else
  211. {
  212. rx_data_len = data;
  213. }
  214. rx_state = CHECK_RX_DATA;
  215. // modified by yuewei 20260304 end
  216. }
  217. else if (rx_state == CHECK_RX_DATA)
  218. {
  219. Check.rx_buf[Check.rx_idx++] = data;
  220. if (Check.rx_idx >= rx_data_len + 9)
  221. {
  222. if ((rx_data_len == 502) && (cmd == 0x69))
  223. {
  224. rx_state = CHECK_RX_CRC_16; // 设置口径和温度补偿值,特殊处理
  225. }
  226. else
  227. {
  228. rx_state = CHECK_RX_CRC;
  229. }
  230. }
  231. }
  232. else if (rx_state == CHECK_RX_CRC_16)
  233. { // 设置口径和温度补偿值,特殊处理
  234. Check.rx_buf[Check.rx_idx++] = data; // add by yuewei 20260305
  235. rx_state = CHECK_RX_CRC; // add by yuewei 20260305
  236. }
  237. else if (rx_state == CHECK_RX_CRC)
  238. {
  239. Check.rx_buf[Check.rx_idx++] = data;
  240. // add by yuewei 20260305 start
  241. // 设置口径和温度补偿值,特殊处理
  242. if ((rx_data_len == 502) && (cmd == 0x69))
  243. {
  244. crc16 = data;
  245. crc16 = crc16 << 8;
  246. crc16 = crc16 | Check.rx_buf[Check.rx_idx - 2];
  247. if (crc16 == check_sum_2_byte(&Check.rx_buf[1], Check.rx_idx - 3))
  248. {
  249. rx_state = CHECK_RX_TAIL;
  250. }
  251. else
  252. {
  253. CheckRxErr();
  254. // LOG("----------------------------4\n");
  255. }
  256. } // add by yuewei 20260305 end
  257. else
  258. {
  259. crc = data;
  260. if (crc == check_sum(&Check.rx_buf[1], Check.rx_idx - 2))
  261. {
  262. rx_state = CHECK_RX_TAIL;
  263. }
  264. else
  265. {
  266. CheckRxErr();
  267. // LOG("----------------------------5\n");
  268. }
  269. }
  270. }
  271. else if (rx_state == CHECK_RX_CRC_MODBUS)
  272. { // 读取ot需要特殊处理
  273. Check.rx_buf[Check.rx_idx++] = data; // add by yuewei 20260317
  274. if (Check.rx_idx >= 20)
  275. {
  276. crc16 = Check.rx_buf[19];
  277. crc16 = crc16 << 8;
  278. crc16 = crc16 | Check.rx_buf[18];
  279. if (crc16 == crc16_modbus(&Check.rx_buf[0], 18))
  280. {
  281. rx_state = CHECK_RX_TAIL;
  282. }
  283. else
  284. {
  285. CheckRxErr();
  286. }
  287. }
  288. }
  289. else if (rx_state == CHECK_RX_TAIL)
  290. {
  291. if (data != 0x16)
  292. {
  293. // LOG("----------------------------6\n");
  294. // LOG("Check.rx_idx=%d,data=%x\n",Check.rx_idx,data);
  295. CheckRxErr();
  296. }
  297. HONGWAI_LONG_RX_FLAG = 0; // add by yuewei 20260309 to deal hongwai long data
  298. Check.rx_buf[Check.rx_idx++] = data;
  299. Check.rx_flag |= CHECK_RX_VLD;
  300. // LOG("\n");
  301. // for(int k = 0;k < Check.rx_idx; k++)
  302. // {
  303. // LOG("%2x ",Check.rx_buf[k]);
  304. // }
  305. // LOG("\n");
  306. }
  307. Check.rx_over_time = 2;
  308. }
  309. void CheckRxTimeout(void)
  310. {
  311. if (Check.rx_over_time >= 2)
  312. {
  313. Check.rx_over_time--;
  314. }
  315. else if (Check.rx_over_time == 1)
  316. {
  317. Check.rx_flag |= CHECK_RX_VLD;
  318. Check.rx_over_time--;
  319. }
  320. }
  321. boolean_t CheckRxValid(void)
  322. {
  323. if (Check.rx_idx < 4)
  324. {
  325. return FALSE;
  326. }
  327. if (Check.rx_buf[0] != 0x68)
  328. {
  329. return FALSE;
  330. }
  331. if (Check.rx_buf[Check.rx_idx - 1] != 0x16)
  332. {
  333. return FALSE;
  334. }
  335. // add by yuewei 20260305 start
  336. // 设置口径和温度补偿值,特殊处理
  337. if ((rx_data_len == 502) && (Check.rx_buf[1] == 0x69))
  338. {
  339. uint16_t crc16 = Check.rx_buf[Check.rx_idx - 2];
  340. crc16 = crc16 << 8;
  341. crc16 = crc16 | Check.rx_buf[Check.rx_idx - 3];
  342. if (crc16 != check_sum_2_byte(&Check.rx_buf[1], Check.rx_idx - 4))
  343. {
  344. return FALSE;
  345. }
  346. } // add by yuewei 20260305 end
  347. else
  348. {
  349. if (Check.rx_buf[1] == 0xa5)
  350. {
  351. // add by yuewei 20260317 to deal read ot
  352. uint16_t crc16 = Check.rx_buf[Check.rx_idx - 2];
  353. crc16 = crc16 << 8;
  354. crc16 = crc16 | Check.rx_buf[Check.rx_idx - 3];
  355. if (crc16 != crc16_modbus(&Check.rx_buf[0], 18))
  356. {
  357. return FALSE;
  358. }
  359. }
  360. else
  361. {
  362. if (check_sum(&Check.rx_buf[1], Check.rx_idx - 3) != Check.rx_buf[Check.rx_idx - 2])
  363. {
  364. return FALSE;
  365. }
  366. }
  367. }
  368. return TRUE;
  369. }
  370. void CheckComHead(uint8_t cmd)
  371. {
  372. Check.tx_buf[0] = 0xfe;
  373. Check.tx_buf[1] = 0x68;
  374. Check.tx_buf[2] = cmd;
  375. Check.tx_buf[3] = 0x11;
  376. Check.tx_buf[4] = 0x11;
  377. Check.tx_buf[5] = 0x11;
  378. Check.tx_buf[6] = 0x22;
  379. Check.tx_buf[7] = 0x22;
  380. Check.tx_buf[8] = 0x22;
  381. }
  382. void CheckComTx(uint8_t cmd)
  383. {
  384. CheckComHead(cmd);
  385. Check.tx_buf[9] = check_sum(&Check.tx_buf[2], 7);
  386. Check.tx_buf[10] = 0x16;
  387. }
  388. // add by yuewei 20260401
  389. void check_com_cmd_response(uint8_t cmd, uint8_t state)
  390. {
  391. CheckComHead(cmd);
  392. Check.tx_buf[9] = 0x01; // L
  393. Check.tx_buf[10] = state; // 1:success, other:fail
  394. Check.tx_buf[11] = check_sum(&Check.tx_buf[2], 9);
  395. Check.tx_buf[12] = 0x16;
  396. }
  397. void CheckReadAddr(void)
  398. {
  399. uint8_t i;
  400. Param_t *param;
  401. GetParam(&param);
  402. // modified by yuewei 20260313 read meter addr do not enter check mode
  403. // Check.state = CHECKING;
  404. // Check.checking_cnt = 4096;
  405. CheckComHead(0x52);
  406. Check.tx_buf[9] = 0x07;
  407. for (i = 0; i < DEV_ID_LEN; i++)
  408. {
  409. Check.tx_buf[10 + i] = param->dev_id[DEV_ID_LEN - 1 - i];
  410. }
  411. Check.tx_buf[16] = 0x00;
  412. Check.tx_buf[17] = check_sum(&Check.tx_buf[2], 15);
  413. Check.tx_buf[18] = 0x16;
  414. finsh_tx(Check.tx_buf, 19);
  415. }
  416. void CheckSetAddr(void)
  417. {
  418. uint16_t i;
  419. Param_t *param;
  420. GetParam(&param);
  421. if (Check.rx_idx < 18)
  422. {
  423. Check.rx_err |= CHECK_RX_ERROR;
  424. return;
  425. }
  426. if (Check.rx_buf[8] != 0x07)
  427. {
  428. Check.rx_err |= CHECK_RX_ERROR;
  429. return;
  430. }
  431. for (i = 0; i < DEV_ID_LEN; i++)
  432. {
  433. param->dev_id[i] = Check.rx_buf[9 + DEV_ID_LEN - 1 - i];
  434. }
  435. // add by yuewei 20260313 save id to eeprom
  436. set_device_addr(param->dev_id);
  437. // SaveParam();
  438. CheckComTx(0x40);
  439. finsh_tx(Check.tx_buf, 11);
  440. }
  441. void CheckMeterStart(void)
  442. {
  443. Check.state = CHECKING;
  444. Check.checking_cnt = 4096;
  445. rt_base_t level = rt_hw_interrupt_disable();
  446. Check.curr_cum_flow = CumFlow;//add by yuewei 20260630 to deal Check.curr_cum_flow = 0 after reset
  447. Check.start_cum_flow = Check.curr_cum_flow;
  448. Check.flow_cnt = 0;
  449. Check.total_spd_lvl = 0.0f;
  450. Check.total_water_temp = 0.0f;
  451. Check.start_time = RTC_CntInt;
  452. Check.end_time = Check.start_time;
  453. uint16_t state = get_tof_state();
  454. if (state & TOF_ST_CALIB)
  455. {
  456. CumFlow = 0;
  457. CalibTime = 0;
  458. CalibSpdLvl = 0;
  459. CalibTemp = 0;
  460. Check.start_cum_flow = 0;
  461. Check.curr_cum_flow = 0;
  462. }
  463. rt_hw_interrupt_enable(level);
  464. CheckComTx(0x40);
  465. finsh_tx(Check.tx_buf, 11);
  466. lcd_enter_check_mode(); //add by HBR 20260622
  467. }
  468. void CheckMeterEnd(void)
  469. {
  470. rt_base_t level;
  471. uint16_t state;
  472. Check.state = CHECKED;
  473. Check.checking_cnt = 0;
  474. state = get_tof_state();
  475. if (state & TOF_ST_CALIB)
  476. {
  477. level = rt_hw_interrupt_disable();
  478. CumFlow = 0.0f;
  479. rt_hw_interrupt_enable(level);
  480. }
  481. CheckComTx(0x40);
  482. finsh_tx(Check.tx_buf, 11);
  483. }
  484. void CheckReadFlowParam(void)
  485. {
  486. uint16_t i;
  487. Param_t *param;
  488. GetParam(&param);
  489. CheckComHead(0x62);
  490. Check.tx_buf[9] = 0x16;
  491. // modified by yuewei 20260316 to read in normal mode, only use check_spdLvl_c[0]
  492. // uint16_t temp_idx = CheckTempRangeIndex((float)(Check.total_water_temp / Check.checking_cnt));
  493. uint16_t temp_idx = 0;
  494. CheckSpdLvlC_t *p = &(param->check_spdLvl_c[temp_idx]);
  495. Check.tx_buf[10] = (uint8_t)Check.total_pulse;
  496. Check.tx_buf[11] = (uint8_t)(Check.total_pulse >> 8);
  497. Check.tx_buf[12] = (uint8_t)(Check.total_pulse >> 16);
  498. Check.tx_buf[13] = (uint8_t)(Check.total_pulse >> 24);
  499. for (i = 0; i < CHECK_POINT_NUM; i++)
  500. {
  501. Check.tx_buf[14 + i * 2] = (uint8_t)(p->cur_c[i]);
  502. Check.tx_buf[15 + i * 2] = (uint8_t)(p->cur_c[i] >> 8);
  503. }
  504. // modified by yuewei 20260316 change q_value to q
  505. // Check.tx_buf[30] = (uint8_t)(param->q_value);
  506. // Check.tx_buf[31] = (uint8_t)(param->q_value >> 8);
  507. Check.tx_buf[30] = (uint8_t)(param->q);
  508. Check.tx_buf[31] = (uint8_t)(param->q >> 8);
  509. Check.tx_buf[32] = check_sum(&Check.tx_buf[2], 30);
  510. Check.tx_buf[33] = 0x16;
  511. finsh_tx(Check.tx_buf, 34);
  512. }
  513. void CheckReadFlowParamInTf(void)
  514. {
  515. uint16_t i;
  516. uint32_t total_pulse;
  517. // add by yuewei 20260519
  518. float rtc_interrupt_time_seconds = get_rtc_interrupt_time();
  519. Check.end_time = get_rtc_cnt_int();
  520. // modified by yuewei 20260519 start
  521. // uint16_t N = (uint16_t)((Check.end_time - Check.start_time) * 4);
  522. uint16_t N = (uint16_t)((Check.end_time - Check.start_time) * 4 * rtc_interrupt_time_seconds);
  523. // modified by yuewei 20260519 end
  524. Param_t *param;
  525. GetParam(&param);
  526. CheckComHead(0x6B);
  527. Check.tx_buf[9] = 0x22;
  528. // modified by yuewei 20260316 to read in normal mode, only use check_spdLvl_c[0]
  529. // uint16_t temp_idx = CheckTempRangeIndex((float)(Check.total_water_temp / Check.checking_cnt));
  530. uint16_t temp_idx = 0;
  531. CheckSpdLvlC_t *p = &(param->check_spdLvl_c[temp_idx]);
  532. total_pulse = Check.total_pulse;
  533. Check.tx_buf[10] = (uint8_t)total_pulse;
  534. Check.tx_buf[11] = (uint8_t)(total_pulse >> 8);
  535. Check.tx_buf[12] = (uint8_t)(total_pulse >> 16);
  536. Check.tx_buf[13] = (uint8_t)(total_pulse >> 24);
  537. for (i = 0; i < CHECK_POINT_NUM; i++)
  538. {
  539. Check.tx_buf[14 + i * 2] = (uint8_t)(p->cur_c[i]);
  540. Check.tx_buf[15 + i * 2] = (uint8_t)(p->cur_c[i] >> 8);
  541. }
  542. // modified by yuewei 20260316 change q_value to q
  543. // Check.tx_buf[30] = (uint8_t)(param->q_value);
  544. // Check.tx_buf[31] = (uint8_t)(param->q_value >> 8);
  545. Check.tx_buf[30] = (uint8_t)(param->q);
  546. Check.tx_buf[31] = (uint8_t)(param->q >> 8);
  547. Check.tx_buf[32] = (uint8_t)N;
  548. Check.tx_buf[33] = (uint8_t)(N >> 8);
  549. uint32_t cum_flow;
  550. cum_flow = (uint32_t)(Check.curr_cum_flow - Check.start_cum_flow);
  551. Check.tx_buf[34] = (uint8_t)cum_flow;
  552. Check.tx_buf[35] = (uint8_t)(cum_flow >> 8);
  553. Check.tx_buf[36] = (uint8_t)(cum_flow >> 16);
  554. Check.tx_buf[37] = (uint8_t)(cum_flow >> 24);
  555. uint32_t spdlvl = (uint32_t)(Check.total_spd_lvl * 10 / Check.flow_cnt);
  556. Check.tx_buf[38] = (uint8_t)spdlvl;
  557. Check.tx_buf[39] = (uint8_t)(spdlvl >> 8);
  558. Check.tx_buf[40] = (uint8_t)(spdlvl >> 16);
  559. Check.tx_buf[41] = (uint8_t)(spdlvl >> 24);
  560. uint16_t temp = (uint16_t)(Check.total_water_temp * 10 / Check.flow_cnt + 0.5);
  561. Check.tx_buf[42] = (uint8_t)temp;
  562. Check.tx_buf[43] = (uint8_t)(temp >> 8);
  563. Check.tx_buf[44] = check_sum(&Check.tx_buf[2], 42);
  564. Check.tx_buf[45] = 0x16;
  565. finsh_tx(Check.tx_buf, 46);
  566. Check.state = CHECKED;
  567. }
  568. void CheckSetFlowParam(void)
  569. {
  570. uint16_t i;
  571. uint16_t state;
  572. Param_t *param;
  573. GetParam(&param);
  574. // modified by yuewei 20260316 to read in normal mode, only use check_spdLvl_c[0]
  575. // uint16_t temp_idx = CheckTempRangeIndex((float)(Check.total_water_temp / Check.checking_cnt));
  576. uint16_t temp_idx = 0;
  577. CheckSpdLvlC_t *p = &(param->check_spdLvl_c[temp_idx]);
  578. // calib param collect not change check c value
  579. state = get_tof_state();
  580. if (state & TOF_ST_METER)
  581. {
  582. // midified by yuewei 20260324
  583. // memcpy(p->base_c, p->cur_c, sizeof(p->cur_c));
  584. for (i = 0; i < CHECK_POINT_NUM; i++)
  585. {
  586. p->cur_c[i] = ((uint16_t)Check.rx_buf[10 + i * 2] << 8) | Check.rx_buf[9 + i * 2];
  587. }
  588. }
  589. CheckComHead(0x63);
  590. Check.tx_buf[9] = 0x10;
  591. for (i = 0; i < CHECK_POINT_NUM; i++)
  592. {
  593. Check.tx_buf[10 + i * 2] = (uint8_t)(p->cur_c[i]);
  594. Check.tx_buf[11 + i * 2] = (uint8_t)(p->cur_c[i] >> 8);
  595. }
  596. Check.tx_buf[26] = check_sum(&Check.tx_buf[2], 24);
  597. Check.tx_buf[27] = 0x16;
  598. finsh_tx(Check.tx_buf, 28);
  599. // add by yuewei 20260313 save flow param to eeprom
  600. FLOW_PARA_DEF flow_para;
  601. flow_para.temp = (Check.total_water_temp / Check.checking_cnt);
  602. memcpy(flow_para.cur_c, p->cur_c, sizeof(p->cur_c));
  603. set_flow_para(&flow_para);
  604. }
  605. void CheckReadDeltaTC(void)
  606. {
  607. uint8_t temp = Check.rx_buf[9];
  608. uint16_t i;
  609. Param_t *param;
  610. GetParam(&param);
  611. CheckComHead(0x64);
  612. Check.tx_buf[9] = 0x21;
  613. // modified by yuewei 20260316 to read in normal mode, only use check_spdLvl_c[0]
  614. // uint16_t temp_idx = CheckTempRangeIndex((float)(Check.total_water_temp / Check.checking_cnt));
  615. uint16_t temp_idx = 0;
  616. CheckSpdLvlC_t *p = &(param->check_spdLvl_c[temp_idx]);
  617. for (i = 0; i < CHECK_POINT_NUM; i++)
  618. {
  619. Check.tx_buf[10 + i * 2] = (uint8_t)(p->cur_c[i]);
  620. Check.tx_buf[11 + i * 2] = (uint8_t)(p->cur_c[i] >> 8);
  621. }
  622. for (i = 0; i < CHECK_POINT_NUM; i++)
  623. {
  624. Check.tx_buf[26 + i * 2] = (uint8_t)(p->spd_lvl[i]);
  625. Check.tx_buf[27 + i * 2] = (uint8_t)(p->spd_lvl[i] >> 8);
  626. }
  627. Check.tx_buf[42] = temp;
  628. Check.tx_buf[43] = check_sum(&Check.tx_buf[2], 41);
  629. Check.tx_buf[44] = 0x16;
  630. finsh_tx(Check.tx_buf, 45);
  631. }
  632. void CheckReadCumFlow(void)
  633. {
  634. uint32_t cum = (uint32_t)(CumFlow * 0.1);
  635. uint8_t i;
  636. Param_t *param;
  637. uint16_t state;
  638. GetParam(&param);
  639. CheckComHead(0x59);
  640. Check.tx_buf[9] = 0x17;
  641. for (i = 0; i < DEV_ID_LEN; i++)
  642. {
  643. Check.tx_buf[10 + i] = param->dev_id[DEV_ID_LEN - 1 - i];
  644. }
  645. Check.tx_buf[16] = 0x00;
  646. state = get_tof_state();
  647. if (state & TOF_ST_CALIB)
  648. {
  649. uint32_t ref_flow = (uint32_t)RefFlow;
  650. Check.tx_buf[17] = (uint8_t)ref_flow;
  651. Check.tx_buf[18] = (uint8_t)(ref_flow >> 8);
  652. Check.tx_buf[19] = (uint8_t)(ref_flow >> 16);
  653. Check.tx_buf[20] = (uint8_t)(ref_flow >> 24);
  654. Check.tx_buf[21] = (uint8_t)cum;
  655. Check.tx_buf[22] = (uint8_t)(cum >> 8);
  656. Check.tx_buf[23] = (uint8_t)(cum >> 16);
  657. Check.tx_buf[24] = (uint8_t)(cum >> 24);
  658. uint32_t spd_lvl = (uint32_t)(CalibSpdLvl * 1000);
  659. Check.tx_buf[25] = (uint8_t)spd_lvl;
  660. Check.tx_buf[26] = (uint8_t)(spd_lvl >> 8);
  661. Check.tx_buf[27] = (uint8_t)(spd_lvl >> 16);
  662. Check.tx_buf[28] = (uint8_t)(spd_lvl >> 24);
  663. uint32_t temp = (uint32_t)(CalibTemp * 100);
  664. Check.tx_buf[29] = (uint8_t)temp;
  665. Check.tx_buf[30] = (uint8_t)(temp >> 8);
  666. Check.tx_buf[31] = (uint8_t)(temp >> 16);
  667. Check.tx_buf[32] = (uint8_t)(temp >> 24);
  668. }
  669. else
  670. {
  671. Check.tx_buf[17] = 0x00;
  672. Check.tx_buf[18] = 0x00;
  673. Check.tx_buf[19] = 0x00;
  674. Check.tx_buf[20] = 0x00;
  675. Check.tx_buf[21] = (uint8_t)cum;
  676. Check.tx_buf[22] = (uint8_t)(cum >> 8);
  677. Check.tx_buf[23] = (uint8_t)(cum >> 16);
  678. Check.tx_buf[24] = (uint8_t)(cum >> 24);
  679. Check.tx_buf[25] = 0x00;
  680. Check.tx_buf[26] = 0x00;
  681. Check.tx_buf[27] = 0x00;
  682. Check.tx_buf[28] = 0x00;
  683. Check.tx_buf[29] = 0x00;
  684. Check.tx_buf[30] = 0x00;
  685. Check.tx_buf[31] = 0x00;
  686. Check.tx_buf[32] = 0x00;
  687. }
  688. Check.tx_buf[33] = check_sum(&Check.tx_buf[2], 31);
  689. Check.tx_buf[34] = 0x16;
  690. finsh_tx(Check.tx_buf, 35);
  691. }
  692. void CheckSwitchIrWorkMode(void)
  693. {
  694. Finsh_Uart_Mode &= ~CHECK_ITF_ON;
  695. CheckComTx(0xE0);
  696. finsh_tx(Check.tx_buf, 11);
  697. }
  698. void CheckSetTofState(void)
  699. {
  700. if (Check.rx_buf[9] == 0)
  701. {
  702. set_tof_state(TOF_ST_METER);
  703. }
  704. else if (Check.rx_buf[9] == 1)
  705. {
  706. set_tof_state(TOF_ST_CALIB);
  707. }
  708. CheckComTx(0xE1);
  709. finsh_tx(Check.tx_buf, 11);
  710. }
  711. void CheckSetRefFlow(void)
  712. {
  713. uint32_t ref_flow = Check.rx_buf[9] | (Check.rx_buf[10] << 8) | (Check.rx_buf[11] << 16) | (Check.rx_buf[12] << 24);
  714. RefFlow = (float)ref_flow;
  715. CheckComTx(0xE2);
  716. finsh_tx(Check.tx_buf, 11);
  717. }
  718. // add by yuewei 20260310
  719. /**
  720. * @brief 通过按键设置表端进入检表模式
  721. * @param 无
  722. * @retval 无
  723. */
  724. void check_enter_check_mode_by_key_press(void)
  725. {
  726. Check.state = CHECKING;
  727. Check.checking_cnt = 4096;
  728. lcd_enter_check_mode(); //add by HBR 20260622
  729. rt_base_t level = rt_hw_interrupt_disable();
  730. Check.start_cum_flow = Check.curr_cum_flow;
  731. Check.flow_cnt = 0;
  732. Check.total_spd_lvl = 0.0f;
  733. Check.total_water_temp = 0.0f;
  734. Check.start_time = RTC_CntInt;
  735. Check.end_time = Check.start_time;
  736. uint16_t state = get_tof_state();
  737. if (state & TOF_ST_CALIB)
  738. {
  739. CumFlow = 0;
  740. CalibTime = 0;
  741. CalibSpdLvl = 0;
  742. CalibTemp = 0;
  743. Check.start_cum_flow = 0;
  744. Check.curr_cum_flow = 0;
  745. }
  746. rt_hw_interrupt_enable(level);
  747. }
  748. // add by yuewei 20260304
  749. /**
  750. * @brief 设置表端进入正常模式(0X31)
  751. * @param 无
  752. * @retval 无
  753. */
  754. void check_enter_normal_mode(void)
  755. {
  756. CheckMeterEnd();
  757. }
  758. // add by yuewei 20260310
  759. /**
  760. * @brief 通过按键设置表端进入正常模式
  761. * @param 无
  762. * @retval 无
  763. */
  764. void check_enter_normal_mode_by_key_press(void)
  765. {
  766. rt_base_t level;
  767. uint16_t state;
  768. Check.state = CHECKED;
  769. Check.checking_cnt = 0;
  770. state = get_tof_state();
  771. if (state & TOF_ST_CALIB)
  772. {
  773. level = rt_hw_interrupt_disable();
  774. CumFlow = 0.0f;
  775. rt_hw_interrupt_enable(level);
  776. }
  777. }
  778. // add by yuewei 20260304
  779. /**
  780. * @brief 清表数据(0X3C),主要清正、反向累计水量
  781. * @param 无
  782. * @retval 无
  783. */
  784. void check_clear_meter(void)
  785. {
  786. CheckComTx(0x40);
  787. finsh_tx(Check.tx_buf, 11);
  788. // clear_bottom_data(); // clear base read
  789. clear_all_record(); // clear year,month,day,hour,5min records
  790. clear_log(); // clear log record
  791. }
  792. // add by yuewei 20260304
  793. /**
  794. * @brief 设置声道数(0X3D)
  795. * @param 无
  796. * @retval 无
  797. */
  798. void check_set_channel_num(void)
  799. {
  800. Param_t *param;
  801. GetParam(&param);
  802. CheckComTx(0x40);
  803. finsh_tx(Check.tx_buf, 11);
  804. /* small water meter only have one channel disable channel set
  805. // Check.rx_buf[9]
  806. param->chan_num = Check.rx_buf[9];
  807. pTdcItf->ch_num = Check.rx_buf[9];
  808. if (pTdcItf->ch_num == 1)
  809. {
  810. TdcItf.ch_bit_map = 1;
  811. }
  812. if (pTdcItf->ch_num == 2)
  813. {
  814. TdcItf.ch_bit_map = 3;
  815. }
  816. if (pTdcItf->ch_num == 4)
  817. {
  818. TdcItf.ch_bit_map = 0x0F;
  819. }
  820. // add by yuewei 20260519
  821. tof_filter_init(pTdcItf->ch_num);
  822. // save chan_num to eeprom
  823. set_channel_numb(&param->chan_num);
  824. */
  825. // LOG("check_set_channel_num:%d\n", Check.rx_buf[9]);
  826. }
  827. // add by yuewei 20260429
  828. /**
  829. * @brief 设置静态ot(0X58)
  830. * @param 无
  831. * @retval 无
  832. */
  833. void check_set_static_ot(void)
  834. {
  835. uint8_t isValid = 1;
  836. uint8_t ch_num;
  837. uint16_t i, j;
  838. float TmpFloat[MUX_CH_NUM] = {0}; // 显式初始化 0
  839. uint32_t Tu32;
  840. uint8_t ch;
  841. uint32_t cnt_int;
  842. // 先回复红外指令
  843. CheckComTx(0x40);
  844. finsh_tx(Check.tx_buf, 11);
  845. // 状态切换 + 稳定延时
  846. set_tof_state(0);
  847. rt_thread_delay(20); // 20ms 系统延时更稳定
  848. set_tof_state(TOF_ST_TEST);
  849. ch_num = GetChNum();
  850. ot_count_down = ch_num * 512; // OT 检测倒计时
  851. SKEW_PARA_DEF skew_para = {0}; // 结构体清 0
  852. // 【关键】标记:OT 校准期间关闭滤波,避免干扰
  853. G_STATIC_OT_SETTING_FLAG = 1;
  854. // 遍历所有通道采集 OT
  855. for (i = 0; i < ch_num; i++)
  856. {
  857. ch = TdcItf.mux_ch_no[i];
  858. set_mux_ch(ch);
  859. us_delay(50);
  860. // rt_thread_delay(1); // 1ms 比 us_delay 更稳定可靠
  861. // 任何通道空管 → OT 无效
  862. if (alarm_status(ERR_CH0) || alarm_status(ERR_CH1) ||
  863. alarm_status(ERR_CH2) || alarm_status(ERR_CH3))
  864. {
  865. isValid = 0;
  866. break;
  867. }
  868. TmpFloat[i] = 0.0f;
  869. // 连续采样 512 次求平均
  870. for (j = 0; j < 512; j++)
  871. {
  872. // RTC 计数步进
  873. cnt_int = get_rtc_cnt_int();
  874. cnt_int++;
  875. set_rtc_cnt_int(cnt_int);
  876. set_tdc_int_step(0x00);
  877. Tu32 = (cnt_int & 1u) ? TDC_CFG_FIRE_DOWN : TDC_CFG_FIRE_UP;
  878. // 启动 TDC 测量
  879. tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG | Tu32);
  880. // 等待 TDC 完成(空循环不占 CPU 优化)
  881. while (get_tdc_int_step() != 0xFFu)
  882. ;
  883. // 累加时差
  884. TmpFloat[i] += get_diff_tof();
  885. // 进度打点
  886. if ((j & 0x3Fu) == 0x00u)
  887. {
  888. LOG(".");
  889. }
  890. ot_count_down--;
  891. // 必须延时,否则 PathSkew 偏小
  892. rt_thread_delay(10); // 10ms 系统延时,稳定不抖动
  893. // us_delay(10000); // must insert delay, otherwise TdcPathSkew a little small
  894. feed_dog(); // wxl2026-5-19
  895. }
  896. // 512 次平均后,绝对值超限 → OT 无效
  897. if (fabsf(TdcItf.path_skew[ch] + TmpFloat[i] * (1.0f / 512)) * 100.00f > Paramx.n)
  898. {
  899. LOG("\nCh%d PathSkew Error! new PathSkew = %.3f ns\n", ch, TmpFloat[i] / 512.0f);
  900. isValid = 0;
  901. break;
  902. }
  903. }
  904. // ==============================================
  905. // 校准有效 → 保存 Skew 到 EEPROM
  906. // ==============================================
  907. if (isValid == 1)
  908. {
  909. for (i = 0; i < ch_num; i++)
  910. {
  911. ch = TdcItf.mux_ch_no[i];
  912. TdcItf.path_skew[ch] += (TmpFloat[i] / 512.0f);
  913. skew_para.skew[ch] = TdcItf.path_skew[ch];
  914. LOG("\nCh%d new PathSkew = %.3f ns\n", ch, TdcItf.path_skew[ch]);
  915. }
  916. set_skew_para(&skew_para); // 保存到 EEPROM
  917. write_alarm(LOG_SET_STATIC_OT, 1, 5555);
  918. }
  919. else
  920. {
  921. LOG("PathSkew Error!\n");
  922. G_OT_ERROR_FLAG_CNT = 5; // 通知 LCD 显示 OT-Err
  923. }
  924. // 恢复平均差值标志
  925. extern float DiffToFAvg128;
  926. DiffToFAvg128 = 0.0f;
  927. DiffToFAvg = 0.0f;
  928. // 恢复正常计量状态
  929. set_tof_state(0);
  930. rt_thread_delay(20);
  931. set_tof_state(TOF_ST_METER);
  932. // 恢复正常滤波
  933. G_STATIC_OT_SETTING_FLAG = 0;
  934. }
  935. // add by yuewei 20260304
  936. /**
  937. * @brief 设置Δt和C(0X65)
  938. * @param 无
  939. * @retval 无
  940. */
  941. void check_set_delta_t_and_c(void)
  942. {
  943. uint16_t i;
  944. Param_t *param;
  945. CheckComTx(0x40);
  946. finsh_tx(Check.tx_buf, 11);
  947. GetParam(&param);
  948. // modified by yuewei 20260316 to read in normal mode, only use check_spdLvl_c[0]
  949. // uint16_t temp_idx = CheckTempRangeIndex((float)(Check.total_water_temp / Check.checking_cnt));
  950. uint16_t temp_idx = 0;
  951. CheckSpdLvlC_t *p = &(param->check_spdLvl_c[temp_idx]);
  952. for (i = 0; i < CHECK_POINT_NUM; i++)
  953. {
  954. p->cur_c[i] = ((uint16_t)Check.rx_buf[10 + i * 2] << 8) | Check.rx_buf[9 + i * 2];
  955. }
  956. for (i = 0; i < CHECK_POINT_NUM; i++)
  957. {
  958. p->spd_lvl[i] = ((uint16_t)Check.rx_buf[26 + i * 2] << 8) | Check.rx_buf[25 + i * 2];
  959. }
  960. DELTA_TC_FLOW_PARA_DEF deltaTC_para;
  961. deltaTC_para.temp = 50; // only use check_spdLvl_c[0]
  962. memcpy(deltaTC_para.cur_c, p->cur_c, sizeof(p->cur_c));
  963. memcpy(deltaTC_para.spd_lvl, p->spd_lvl, sizeof(p->spd_lvl));
  964. // save deltaTC to eeprom
  965. set_deltaTC_flow_para(&deltaTC_para);
  966. }
  967. // add by yuewei 20260316
  968. /**
  969. * @brief 设置bnqr(0X67)
  970. * @param 无
  971. * @retval 无
  972. */
  973. void check_set_bnqr(void)
  974. {
  975. // TBD n to deal
  976. Param_t *param;
  977. CheckComTx(0x40);
  978. finsh_tx(Check.tx_buf, 11);
  979. GetParam(&param);
  980. param->b = ((uint16_t)Check.rx_buf[10] << 8) | Check.rx_buf[9];
  981. param->n = ((uint16_t)Check.rx_buf[12] << 8) | Check.rx_buf[11];
  982. param->q = ((uint16_t)Check.rx_buf[14] << 8) | Check.rx_buf[13];
  983. param->r = ((uint16_t)Check.rx_buf[16] << 8) | Check.rx_buf[15];
  984. NBQR_PARA_DEF nbqr_para;
  985. nbqr_para.b = param->b;
  986. nbqr_para.n = param->n;
  987. nbqr_para.q = param->q;
  988. nbqr_para.r = param->r;
  989. // save bnqr to eeprom
  990. set_nbqr_para(&nbqr_para);
  991. float q3 = param->b;
  992. q3 = q3 / 10.0f; // 0.1m?/h->m?/h
  993. float q1 = param->r;
  994. q1 = q3 / q1; // Q1 = R/Q1
  995. CheckSpdLvlC_t *p = &(param->check_spdLvl_c[0]);
  996. // only use check_spdLvl_c[0]
  997. p->spd_lvl[0] = GetSpdLvl(q1);
  998. p->spd_lvl[1] = GetSpdLvl(1.6 * q1);
  999. p->spd_lvl[2] = GetSpdLvl(3.2 * q1);
  1000. p->spd_lvl[3] = GetSpdLvl(8 * q1);
  1001. p->spd_lvl[4] = GetSpdLvl(0.1 * q3);
  1002. p->spd_lvl[5] = GetSpdLvl(0.2 * q3);
  1003. p->spd_lvl[6] = GetSpdLvl(0.35 * (1.6 * q1 + q3));
  1004. p->spd_lvl[7] = GetSpdLvl(q3);
  1005. DELTA_TC_FLOW_PARA_DEF deltaTC_para;
  1006. deltaTC_para.temp = 50; // only use check_spdLvl_c[0]
  1007. memcpy(deltaTC_para.cur_c, p->cur_c, sizeof(p->cur_c));
  1008. memcpy(deltaTC_para.spd_lvl, p->spd_lvl, sizeof(p->spd_lvl));
  1009. // save deltaTC to eeprom
  1010. set_deltaTC_flow_para(&deltaTC_para);
  1011. LOG("check_set_bnqr b=%d,n=%d,q=%d,r=%d\n", param->b, param->n, param->q, param->r);
  1012. }
  1013. // add by yuewei 20260304
  1014. /**
  1015. * @brief 设置口径和温度补偿值(0X69)
  1016. * @param 无
  1017. * @retval 无
  1018. */
  1019. void check_set_diameter_and_temperature_compensation(void)
  1020. {
  1021. CheckComTx(0x40);
  1022. finsh_tx(Check.tx_buf, 11);
  1023. uint8_t rx_buf[4];
  1024. float recvValue;
  1025. uint16_t iCount = 28;
  1026. for (uint16_t i = 0; i < iCount; i++)
  1027. {
  1028. memcpy(rx_buf, (uint8_t *)&Check.rx_buf[i * 12 + 9], 4);
  1029. recvValue = *(float *)rx_buf;
  1030. pTdcItf->quad_cell_ary[i].value = recvValue;
  1031. memcpy(rx_buf, (uint8_t *)&Check.rx_buf[i * 12 + 13], 4);
  1032. recvValue = *(float *)rx_buf;
  1033. pTdcItf->quad_cell_ary[i].coe.m = recvValue;
  1034. memcpy(rx_buf, (uint8_t *)&Check.rx_buf[i * 12 + 17], 4);
  1035. recvValue = *(float *)rx_buf;
  1036. pTdcItf->quad_cell_ary[i].coe.k = recvValue;
  1037. }
  1038. // diameter
  1039. uint16_t iDiameter = Check.rx_buf[510];
  1040. iDiameter = ((iDiameter << 8) | Check.rx_buf[509]);
  1041. // save to memory
  1042. // Paramx.diameter = iDiameter;
  1043. // save to eeprom
  1044. set_diameter_and_quad_cell_data(iDiameter, pTdcItf->quad_cell_ary); // wxl2026-4-14
  1045. }
  1046. // add by yuewei 20260304
  1047. /**
  1048. * @brief 获取口径和温度补偿值(0X6A)
  1049. * @param 无
  1050. * @retval 无
  1051. */
  1052. void check_get_diameter_and_temperature_compensation(void)
  1053. {
  1054. memset((uint8_t *)Check.tx_buf, 0, CHECK_TX_BUF_LEN);
  1055. CheckComTx(0x6A);
  1056. uint8_t *sendValue;
  1057. uint16_t iCount = 28;
  1058. for (uint16_t i = 0; i < iCount; i++)
  1059. {
  1060. sendValue = (uint8_t *)(&pTdcItf->quad_cell_ary[i].value);
  1061. memcpy((uint8_t *)&Check.tx_buf[i * 12 + 9], sendValue, 4);
  1062. sendValue = (uint8_t *)(&pTdcItf->quad_cell_ary[i].coe.m);
  1063. memcpy((uint8_t *)&Check.tx_buf[i * 12 + 13], sendValue, 4);
  1064. sendValue = (uint8_t *)(&pTdcItf->quad_cell_ary[i].coe.k);
  1065. memcpy((uint8_t *)&Check.tx_buf[i * 12 + 17], sendValue, 4);
  1066. }
  1067. // diameter
  1068. uint16_t iDiameter = Paramx.diameter;
  1069. Check.tx_buf[10 + 500] = (uint8_t)iDiameter;
  1070. Check.tx_buf[10 + 501] = (uint8_t)(iDiameter >> 8);
  1071. uint16_t crc16;
  1072. uint16_t iLenth = 502;
  1073. crc16 = check_sum_2_byte(&Check.tx_buf[2], 8 + iLenth);
  1074. Check.tx_buf[10 + iLenth] = (uint8_t)crc16; // crc l
  1075. Check.tx_buf[11 + iLenth] = (uint8_t)(crc16 >> 8); // crc h
  1076. Check.tx_buf[12 + iLenth] = 0x16;
  1077. rt_enter_critical();
  1078. finsh_tx(Check.tx_buf, 13 + iLenth);
  1079. rt_exit_critical();
  1080. }
  1081. // add by yuewei 20260304
  1082. /**
  1083. * @brief 读取自动匹配电平参数(0X85)
  1084. * @param 无
  1085. * @retval 无
  1086. */
  1087. void check_get_auto_match_volage_level(void)
  1088. {
  1089. CheckComTx(0x85);
  1090. finsh_tx(Check.tx_buf, 11);
  1091. }
  1092. // add by yuewei 20260304
  1093. /**
  1094. * @brief 设置自动匹配电平参数(0X86)
  1095. * @param 无
  1096. * @retval 无
  1097. */
  1098. void check_set_auto_match_volage_level(void)
  1099. {
  1100. CheckComTx(0x40);
  1101. finsh_tx(Check.tx_buf, 11);
  1102. }
  1103. // add by yuewei 20260304
  1104. /**
  1105. * @brief 读取静态ot(0XA5)
  1106. * @param 无
  1107. * @retval 无
  1108. */
  1109. void check_get_static_ot(void)
  1110. {
  1111. uint16_t crc16;
  1112. // 68 A5 00 00 00 00 00
  1113. // 80 00 50 FF 2A 00
  1114. // 00 00 00 00 90 01 00 05 65 04 00 00 A0 00 00 00 00 00 26 15 03 03 26 24 00 46 38 43 35 33 00 00 00 00 00 00 00 10 EE EE EE EE DE 3C 16
  1115. LOG("check_get_static_ot\n");
  1116. memset((uint8_t *)Check.tx_buf, 0, CHECK_TX_BUF_LEN);
  1117. Check.tx_buf[0] = 0x68;
  1118. Check.tx_buf[1] = 0xA5;
  1119. Check.tx_buf[7] = 0x80;
  1120. Check.tx_buf[9] = 0x50;
  1121. Check.tx_buf[10] = 0xff;
  1122. Check.tx_buf[11] = 0x2a; // L
  1123. Check.tx_buf[12] = 0x00; // L
  1124. uint16_t ot1 = (uint16_t)fabs(TdcItf.path_skew[0] * 1000); // ps
  1125. uint16_t ot2 = (uint16_t)fabs(TdcItf.path_skew[1] * 1000); // ps
  1126. uint16_t ot3 = (uint16_t)fabs(TdcItf.path_skew[2] * 1000); // ps
  1127. uint16_t ot4 = (uint16_t)fabs(TdcItf.path_skew[3] * 1000); // ps
  1128. Check.tx_buf[13] = (uint8_t)ot1; // ot1 l
  1129. Check.tx_buf[14] = (uint8_t)(ot1 >> 8); // ot1 h
  1130. Check.tx_buf[27] = (uint8_t)ot2; // ot2 l
  1131. Check.tx_buf[28] = (uint8_t)(ot2 >> 8); // ot2 h
  1132. Check.tx_buf[29] = (uint8_t)ot3; // ot3 l
  1133. Check.tx_buf[30] = (uint8_t)(ot3 >> 8); // ot3 h
  1134. Check.tx_buf[23] = (uint8_t)ot4; // ot4 l
  1135. Check.tx_buf[24] = (uint8_t)(ot4 >> 8); // ot4 h
  1136. crc16 = crc16_modbus(&Check.tx_buf[0], 55);
  1137. Check.tx_buf[55] = (uint8_t)crc16; // crc l
  1138. Check.tx_buf[56] = (uint8_t)(crc16 >> 8); // crc h
  1139. Check.tx_buf[57] = 0x16;
  1140. finsh_tx(Check.tx_buf, 58);
  1141. }
  1142. // add by yuewei 20260316
  1143. /**
  1144. * @brief 设置硬件参数(0X00)
  1145. * @param 无
  1146. * @retval 无
  1147. */
  1148. void check_set_hardware_param(void)
  1149. {
  1150. Param_t *param;
  1151. GetParam(&param);
  1152. // modified by yuewei 20260610 start
  1153. // param->pressure_sensor = Check.rx_buf[9];
  1154. param->pressure_sensor = 2; // 无论红外设置是否带压力传感器,始终为‘不带压力传感器’
  1155. // modified by yuewei 20260610 end
  1156. uint32_t iTmp = Check.rx_buf[13];
  1157. iTmp = ((iTmp << 8) | Check.rx_buf[12]);
  1158. iTmp = ((iTmp << 8) | Check.rx_buf[11]);
  1159. iTmp = ((iTmp << 8) | Check.rx_buf[10]);
  1160. param->pipe_coe = iTmp / 1000000.0f;
  1161. LOG("check set pressure_sensor = %d\n", param->pressure_sensor);
  1162. LOG("check set pipe_coe = %.6f\n", param->pipe_coe);
  1163. HardWareParaDef HardWarePara;
  1164. HardWarePara.pressure_sensor = param->pressure_sensor;
  1165. HardWarePara.pipe_coe = param->pipe_coe;
  1166. // save to eeprom
  1167. set_hardware_para(&HardWarePara);
  1168. check_com_cmd_response(0x00, 1); // 1->success
  1169. finsh_tx(Check.tx_buf, 13);
  1170. }
  1171. // add by yuewei 20260316
  1172. /**
  1173. * @brief 触发上报(0X01)
  1174. * @param 无
  1175. * @retval 无
  1176. */
  1177. void check_set_trigger_nb_report(void)
  1178. {
  1179. LOG("check_set_trigger_nb_report\n");
  1180. check_com_cmd_response(0x01, 1); // 1->success
  1181. finsh_tx(Check.tx_buf, 13);
  1182. upload_dat.manual_runflag = 1; // wxl20260421
  1183. }
  1184. // add by yuewei 20260415
  1185. /**
  1186. * @brief
  1187. * @param package_no, time_out_s
  1188. * @retval 0 -> success, other -> fail
  1189. */
  1190. int8_t check_req_diff_package_and_save(PATCH_INFO_DEF patch_info, uint16_t package_no, uint16_t time_out_s)
  1191. {
  1192. uint16_t delay_ms = 100;
  1193. uint16_t receive_max_times = time_out_s / delay_ms;
  1194. uint16_t last_package_num = (patch_info.patch_len / patch_info.per_packet_len) + 1;
  1195. uint16_t last_package_bytes = patch_info.patch_len % patch_info.per_packet_len;
  1196. for (int i = 1; i <= 3; i++) // every package try to req 3 times if not response
  1197. {
  1198. Check.rx_flag = 0;
  1199. cmd = 0;
  1200. rx_data_len = 0;
  1201. rx_state = CHECK_RX_HEAD;
  1202. Check.rx_idx = 0;
  1203. // start req package
  1204. CheckComHead(0x03); // cmd
  1205. Check.tx_buf[9] = 14; // L
  1206. // now version
  1207. memcpy((uint8_t *)&Check.tx_buf[10], patch_info.cur_ver, 5);
  1208. // target version
  1209. memcpy((uint8_t *)&Check.tx_buf[15], patch_info.new_ver, 5);
  1210. // bytes of per package
  1211. Check.tx_buf[20] = (uint8_t)patch_info.per_packet_len;
  1212. Check.tx_buf[21] = (uint8_t)(patch_info.per_packet_len >> 8);
  1213. // current packet num
  1214. patch_info.cur_num = package_no;
  1215. Check.tx_buf[22] = (uint8_t)patch_info.cur_num;
  1216. Check.tx_buf[23] = (uint8_t)(patch_info.cur_num >> 8);
  1217. Check.tx_buf[24] = check_sum(&Check.tx_buf[2], 22);
  1218. Check.tx_buf[25] = 0x16;
  1219. finsh_tx(Check.tx_buf, 26);
  1220. // wait for response
  1221. for (int k = 1; k <= receive_max_times; k++)
  1222. {
  1223. feed_dog();
  1224. rt_thread_delay(delay_ms);
  1225. // us_delay(delay_ms);
  1226. if (Check.rx_flag & CHECK_RX_VLD)
  1227. {
  1228. Check.rx_flag &= ~CHECK_RX_VLD;
  1229. if (cmd == 0x03)
  1230. {
  1231. cmd = 0;
  1232. DIFF_PACKET_DEF Packet;
  1233. Packet.packet_numb = package_no;
  1234. memcpy(Packet.data, (uint8_t *)&Check.rx_buf[13], patch_info.per_packet_len);
  1235. // if(package_no == last_package_num)
  1236. // {
  1237. // //the last package
  1238. // Packet.packet_size = last_package_bytes;
  1239. // if(write_diff_patch_to_flash(&Packet) == 1)
  1240. // {
  1241. // return 0;
  1242. // }
  1243. // else
  1244. // {
  1245. // return -1;
  1246. // }
  1247. // }
  1248. // else
  1249. // {
  1250. // //the first or middle package
  1251. // Packet.packet_size = patch_info.per_packet_len;
  1252. // if(write_diff_patch_to_flash(&Packet) == 0x10)
  1253. // {
  1254. // return 0;
  1255. // }
  1256. // else
  1257. // {
  1258. // return -1;
  1259. // }
  1260. // }
  1261. if (package_no == last_package_num)
  1262. Packet.packet_size = last_package_bytes;
  1263. else
  1264. Packet.packet_size = patch_info.per_packet_len;
  1265. if (write_diff_patch_to_flash(&Packet) == 1)
  1266. return 0;
  1267. else
  1268. return -1;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. return -1;
  1274. }
  1275. // add by yuewei 20260414
  1276. /**
  1277. * @brief (0X02)
  1278. * @param 无
  1279. * @retval 无
  1280. */
  1281. void check_set_start_upgrade(void)
  1282. {
  1283. LOG("check_set_start_upgrade\n");
  1284. uint32_t iTmp;
  1285. PATCH_INFO_DEF patch_info;
  1286. // now version
  1287. memcpy(patch_info.cur_ver, (uint8_t *)&Check.rx_buf[9], 5);
  1288. // target version
  1289. memcpy(patch_info.new_ver, (uint8_t *)&Check.rx_buf[14], 5);
  1290. // total bytes
  1291. iTmp = Check.rx_buf[22];
  1292. iTmp = ((iTmp << 8) | Check.rx_buf[21]);
  1293. iTmp = ((iTmp << 8) | Check.rx_buf[20]);
  1294. iTmp = ((iTmp << 8) | Check.rx_buf[19]);
  1295. patch_info.patch_len = iTmp;
  1296. // bytes of per package
  1297. iTmp = Check.rx_buf[24];
  1298. iTmp = ((iTmp << 8) | Check.rx_buf[23]);
  1299. patch_info.per_packet_len = iTmp;
  1300. // crc
  1301. iTmp = Check.rx_buf[26];
  1302. iTmp = ((iTmp << 8) | Check.rx_buf[25]);
  1303. patch_info.packet_crc = iTmp;
  1304. int8_t ret = set_patch_info(&patch_info);
  1305. if (ret == 1)
  1306. {
  1307. // valid
  1308. int packet_nums = (patch_info.patch_len / patch_info.per_packet_len) + 1;
  1309. for (int i = 1; i <= packet_nums; i++)
  1310. {
  1311. int8_t req_save_ret = check_req_diff_package_and_save(patch_info, i, 5000);
  1312. if (i == packet_nums)
  1313. {
  1314. // the last package
  1315. if (req_save_ret == 0)
  1316. {
  1317. check_com_cmd_response(0x04, 1); // 1->success, other->fail
  1318. finsh_tx(Check.tx_buf, 13);
  1319. return;
  1320. }
  1321. else
  1322. {
  1323. check_com_cmd_response(0x04, 3); // 1->success, other->fail
  1324. finsh_tx(Check.tx_buf, 13);
  1325. return;
  1326. }
  1327. }
  1328. else
  1329. {
  1330. // the middle package
  1331. if (req_save_ret == 0)
  1332. {
  1333. continue;
  1334. }
  1335. else
  1336. {
  1337. check_com_cmd_response(0x04, 3); // 1->success, other->fail
  1338. finsh_tx(Check.tx_buf, 13);
  1339. return;
  1340. }
  1341. }
  1342. }
  1343. }
  1344. else
  1345. {
  1346. // invalid
  1347. check_com_cmd_response(0x04, ret); // 1->success, other->fail
  1348. finsh_tx(Check.tx_buf, 13);
  1349. }
  1350. }
  1351. // add by yuewei 20260331
  1352. /**
  1353. * @brief (0X10)
  1354. * @param 无
  1355. * @retval 无
  1356. */
  1357. void check_set_ip_and_port(void)
  1358. {
  1359. uint16_t iPort;
  1360. Param_t *param;
  1361. GetParam(&param);
  1362. LOG("check_set_ip_and_port\n");
  1363. SERVER_IP_PORT_DEF server_ip_para;
  1364. // main ip
  1365. server_ip_para.main_server_ip[0] = Check.rx_buf[9];
  1366. server_ip_para.main_server_ip[1] = Check.rx_buf[10];
  1367. server_ip_para.main_server_ip[2] = Check.rx_buf[11];
  1368. server_ip_para.main_server_ip[3] = Check.rx_buf[12];
  1369. // main port
  1370. iPort = Check.rx_buf[14];
  1371. iPort = (iPort << 8) | Check.rx_buf[13];
  1372. server_ip_para.main_server_port = iPort;
  1373. // sub ip
  1374. server_ip_para.sub_server_ip[0] = Check.rx_buf[15];
  1375. server_ip_para.sub_server_ip[1] = Check.rx_buf[16];
  1376. server_ip_para.sub_server_ip[2] = Check.rx_buf[17];
  1377. server_ip_para.sub_server_ip[3] = Check.rx_buf[18];
  1378. // sub port
  1379. iPort = Check.rx_buf[20];
  1380. iPort = (iPort << 8) | Check.rx_buf[19];
  1381. server_ip_para.sub_server_port = iPort;
  1382. memcpy(param->main_server_ip, server_ip_para.main_server_ip, sizeof(server_ip_para.main_server_ip));
  1383. param->main_server_port = server_ip_para.main_server_port;
  1384. memcpy(param->sub_server_ip, server_ip_para.sub_server_ip, sizeof(server_ip_para.sub_server_ip));
  1385. param->sub_server_port = server_ip_para.sub_server_port;
  1386. // save to eeprom
  1387. set_server_ip_port_para(&server_ip_para);
  1388. check_com_cmd_response(0x10, 1); // 1->success
  1389. finsh_tx(Check.tx_buf, 13);
  1390. }
  1391. // add by yuewei 20260331
  1392. /**
  1393. * @brief (0X11)
  1394. * @param 无
  1395. * @retval 无
  1396. */
  1397. void check_set_report_base_time(void)
  1398. {
  1399. uint16_t iTemp;
  1400. Param_t *param;
  1401. GetParam(&param);
  1402. LOG("check_set_report_base_time\n");
  1403. REGULAR_REPORT_DEF regular_report_para;
  1404. // base time
  1405. regular_report_para.report_base_hour = Check.rx_buf[9];
  1406. regular_report_para.report_base_min = Check.rx_buf[10];
  1407. regular_report_para.report_base_sec = Check.rx_buf[11];
  1408. // interval(min)
  1409. iTemp = Check.rx_buf[13];
  1410. iTemp = (iTemp << 8) | Check.rx_buf[12];
  1411. regular_report_para.report_interval = iTemp;
  1412. param->report_base_hour = regular_report_para.report_base_hour;
  1413. param->report_base_min = regular_report_para.report_base_min;
  1414. param->report_base_sec = regular_report_para.report_base_sec;
  1415. param->report_interval = regular_report_para.report_interval;
  1416. // save to eeprom
  1417. set_regular_report_para(&regular_report_para);
  1418. check_com_cmd_response(0x11, 1); // 1->success
  1419. finsh_tx(Check.tx_buf, 13);
  1420. }
  1421. // add by yuewei 20260331
  1422. /**
  1423. * @brief (0X12)
  1424. * @param 无
  1425. * @retval 无
  1426. */
  1427. void check_set_dma_report_time(void)
  1428. {
  1429. Param_t *param;
  1430. GetParam(&param);
  1431. LOG("check_set_dma_report_time\n");
  1432. DMA_REGULAR_REPORT_DEF dma_report_para;
  1433. // start time
  1434. dma_report_para.dma_report_start_hour = Check.rx_buf[9];
  1435. dma_report_para.dma_report_start_min = Check.rx_buf[10];
  1436. dma_report_para.dma_report_start_sec = Check.rx_buf[11];
  1437. // end time
  1438. dma_report_para.dma_report_end_hour = Check.rx_buf[12];
  1439. dma_report_para.dma_report_end_min = Check.rx_buf[13];
  1440. dma_report_para.dma_report_end_sec = Check.rx_buf[14];
  1441. // interval(min)
  1442. dma_report_para.dma_interval = Check.rx_buf[15];
  1443. param->dma_report_start[0] = dma_report_para.dma_report_start_hour;
  1444. param->dma_report_start[1] = dma_report_para.dma_report_start_min;
  1445. param->dma_report_start[2] = dma_report_para.dma_report_start_sec;
  1446. param->dma_report_end[0] = dma_report_para.dma_report_end_hour;
  1447. param->dma_report_end[1] = dma_report_para.dma_report_end_min;
  1448. param->dma_report_end[2] = dma_report_para.dma_report_end_sec;
  1449. param->dma_interval = dma_report_para.dma_interval;
  1450. // save to eeprom
  1451. set_dma_regular_report_para(&dma_report_para);
  1452. check_com_cmd_response(0x12, 1); // 1->success
  1453. finsh_tx(Check.tx_buf, 13);
  1454. }
  1455. // add by yuewei 20260331
  1456. /**
  1457. * @brief (0X13)
  1458. * @param 无
  1459. * @retval 无
  1460. */
  1461. void check_set_date_time(void)
  1462. {
  1463. LOG("check_set_date_time\n");
  1464. // Check.rx_buf[9] YY-H no use
  1465. set_rtc_date(Check.rx_buf[10], Check.rx_buf[11], Check.rx_buf[12]);
  1466. set_rtc_time(Check.rx_buf[13], Check.rx_buf[14], Check.rx_buf[15]);
  1467. save_cur_flow_flag = 1; // wxl2026-5-7 save to eeprom
  1468. check_com_cmd_response(0x13, 1); // 1->success
  1469. finsh_tx(Check.tx_buf, 13);
  1470. }
  1471. // add by yuewei 20260331
  1472. /**
  1473. * @brief (0X14)
  1474. * @param 无
  1475. * @retval 无
  1476. */
  1477. void check_set_flow_param(void)
  1478. {
  1479. Param_t *param;
  1480. uint32_t iTmp;
  1481. GetParam(&param);
  1482. LOG("check_set_flow_param\n");
  1483. FLOW_ALARM_PARA_DEF flow_alarm_para;
  1484. // large flow threshold
  1485. iTmp = Check.rx_buf[12];
  1486. iTmp = ((iTmp << 8) | Check.rx_buf[11]);
  1487. iTmp = ((iTmp << 8) | Check.rx_buf[10]);
  1488. iTmp = ((iTmp << 8) | Check.rx_buf[9]);
  1489. flow_alarm_para.large_flow_alarm_thres = iTmp;
  1490. // large flow time
  1491. iTmp = Check.rx_buf[14];
  1492. iTmp = ((iTmp << 8) | Check.rx_buf[13]);
  1493. flow_alarm_para.large_flow_cnti_moni_time = iTmp;
  1494. // continuous flow time
  1495. iTmp = Check.rx_buf[16];
  1496. iTmp = ((iTmp << 8) | Check.rx_buf[15]);
  1497. flow_alarm_para.cnti_flow_cnti_moni_time = iTmp;
  1498. // leak flow threshold
  1499. iTmp = Check.rx_buf[20];
  1500. iTmp = ((iTmp << 8) | Check.rx_buf[19]);
  1501. iTmp = ((iTmp << 8) | Check.rx_buf[18]);
  1502. iTmp = ((iTmp << 8) | Check.rx_buf[17]);
  1503. flow_alarm_para.leak_flow_alarm_thres = iTmp;
  1504. // leak flow time
  1505. iTmp = Check.rx_buf[22];
  1506. iTmp = ((iTmp << 8) | Check.rx_buf[21]);
  1507. flow_alarm_para.leak_flow_cnti_moni_time = iTmp;
  1508. param->large_flow_alarm_thres = flow_alarm_para.large_flow_alarm_thres;
  1509. param->large_flow_cnti_moni_time = flow_alarm_para.large_flow_cnti_moni_time;
  1510. param->cnti_flow_cnti_moni_time = flow_alarm_para.cnti_flow_cnti_moni_time;
  1511. param->leak_flow_alarm_thres = flow_alarm_para.leak_flow_alarm_thres;
  1512. param->leak_flow_cnti_moni_time = flow_alarm_para.leak_flow_cnti_moni_time;
  1513. // save to eeprom
  1514. set_flow_alarm_para(&flow_alarm_para);
  1515. check_com_cmd_response(0x14, 1); // 1->success
  1516. finsh_tx(Check.tx_buf, 13);
  1517. }
  1518. // add by yuewei 20260331
  1519. /**
  1520. * @brief (0X15)
  1521. * @param 无
  1522. * @retval 无
  1523. */
  1524. void check_set_pressure_param(void)
  1525. {
  1526. uint32_t iTmp;
  1527. Param_t *param;
  1528. GetParam(&param);
  1529. LOG("check_set_pressure_param\n");
  1530. // hight pressure param < low pressure param
  1531. if (Check.rx_buf[9] < Check.rx_buf[10])
  1532. {
  1533. check_com_cmd_response(0x15, 3); // 3->fail
  1534. finsh_tx(Check.tx_buf, 13);
  1535. return;
  1536. }
  1537. PRESS_ALARM_PARA_DEF press_alarm_para;
  1538. // high pressure threshold
  1539. iTmp = Check.rx_buf[9];
  1540. press_alarm_para.high_press_alarm_thres = iTmp;
  1541. // low pressure threshold
  1542. iTmp = Check.rx_buf[10];
  1543. press_alarm_para.low_press_alarm_thres = iTmp;
  1544. param->high_press_alarm_thres = press_alarm_para.high_press_alarm_thres;
  1545. param->low_press_alarm_thres = press_alarm_para.low_press_alarm_thres;
  1546. // save to eeprom
  1547. set_press_alarm_para(&press_alarm_para);
  1548. check_com_cmd_response(0x15, 1); // 1->success
  1549. finsh_tx(Check.tx_buf, 13);
  1550. }
  1551. // add by yuewei 20260331
  1552. /**
  1553. * @brief (0X16)
  1554. * @param 无
  1555. * @retval 无
  1556. */
  1557. void check_set_temperature_param(void)
  1558. {
  1559. int16_t iTmpH, iTmpL;
  1560. Param_t *param;
  1561. GetParam(&param);
  1562. LOG("check_set_temperature_param\n");
  1563. // high temperature threshold
  1564. iTmpH = Check.rx_buf[10];
  1565. iTmpH = ((iTmpH << 8) | Check.rx_buf[9]);
  1566. // low temperature threshold
  1567. iTmpL = Check.rx_buf[12];
  1568. iTmpL = ((iTmpL << 8) | Check.rx_buf[11]);
  1569. // hight temperature param < low temperature param
  1570. if (iTmpH < iTmpL)
  1571. {
  1572. check_com_cmd_response(0x16, 2); // 2->fail
  1573. finsh_tx(Check.tx_buf, 13);
  1574. return;
  1575. }
  1576. TEMP_ALARM_PARA_DEF temp_alarm_para;
  1577. // high pressure threshold
  1578. temp_alarm_para.high_temp_alarm_thres = iTmpH;
  1579. // low pressure threshold
  1580. temp_alarm_para.low_temp_alarm_thres = iTmpL;
  1581. param->high_temp_alarm_thres = temp_alarm_para.high_temp_alarm_thres;
  1582. param->low_temp_alarm_thres = temp_alarm_para.low_temp_alarm_thres;
  1583. // save to eeprom
  1584. set_temp_alarm_para(&temp_alarm_para);
  1585. check_com_cmd_response(0x16, 1); // 1->success
  1586. finsh_tx(Check.tx_buf, 13);
  1587. }
  1588. // add by yuewei 20260331
  1589. /**
  1590. * @brief (0X17)
  1591. * @param 无
  1592. * @retval 无
  1593. */
  1594. void check_set_settle_day(void)
  1595. {
  1596. Param_t *param;
  1597. GetParam(&param);
  1598. LOG("check_set_settle_day\n");
  1599. if (Check.rx_buf[9] > 31)
  1600. {
  1601. check_com_cmd_response(0x17, 2); // 2->fail
  1602. finsh_tx(Check.tx_buf, 13);
  1603. return;
  1604. }
  1605. SETTLEMENT_DAY_DEF settlement_day_para;
  1606. settlement_day_para.settlement_day = Check.rx_buf[9];
  1607. param->settlement_day = settlement_day_para.settlement_day;
  1608. // save to eeprom
  1609. set_settlement_day_para(&settlement_day_para);
  1610. check_com_cmd_response(0x17, 1); // 1->success
  1611. finsh_tx(Check.tx_buf, 13);
  1612. }
  1613. // add by yuewei 20260331
  1614. /**
  1615. * @brief (0X18)
  1616. * @param 无
  1617. * @retval 无
  1618. */
  1619. void check_set_base_read(void)
  1620. {
  1621. Param_t *param;
  1622. uint64_t iTmp;
  1623. GetParam(&param);
  1624. LOG("check_set_base_read\n");
  1625. METER_BOTTOM_DEF bottom_data;
  1626. // cover
  1627. // total positive
  1628. bottom_data.fw[0] = Check.rx_buf[9];
  1629. bottom_data.fw[1] = Check.rx_buf[10];
  1630. bottom_data.fw[2] = Check.rx_buf[11];
  1631. bottom_data.fw[3] = Check.rx_buf[12];
  1632. // total inverse
  1633. bottom_data.bw[0] = Check.rx_buf[13];
  1634. bottom_data.bw[1] = Check.rx_buf[14];
  1635. bottom_data.bw[2] = Check.rx_buf[15];
  1636. bottom_data.bw[3] = Check.rx_buf[16];
  1637. // save to eeprom
  1638. set_meter_bottom_data(&bottom_data);
  1639. check_com_cmd_response(0x18, 1); // 1->success
  1640. finsh_tx(Check.tx_buf, 13);
  1641. }
  1642. // add by yuewei 20260331
  1643. /**
  1644. * @brief (0X21)
  1645. * @param 无
  1646. * @retval 无
  1647. */
  1648. void check_report_month_data(void)
  1649. {
  1650. uint16_t iLenth = 18 * 11;
  1651. LOG("check_report_month_data\n");
  1652. uint8_t data[iLenth];
  1653. get_18month_record_data(data);
  1654. CheckComHead(0x21); // cmd
  1655. Check.tx_buf[9] = iLenth; // L
  1656. memcpy((uint8_t *)&Check.tx_buf[10], data, iLenth);
  1657. Check.tx_buf[10 + iLenth] = check_sum(&Check.tx_buf[2], 8 + iLenth);
  1658. Check.tx_buf[11 + iLenth] = 0x16;
  1659. finsh_tx(Check.tx_buf, 12 + iLenth);
  1660. }
  1661. // add by yuewei 20260331
  1662. /**
  1663. * @brief (0X23)
  1664. * @param 无
  1665. * @retval 无
  1666. */
  1667. void check_report_day_data(void)
  1668. {
  1669. uint16_t iLenth = 30 * 12;
  1670. LOG("check_report_day_data\n");
  1671. uint8_t data[iLenth];
  1672. get_30day_record_data(data);
  1673. CheckComHead(0x23); // cmd
  1674. Check.tx_buf[9] = 0xF1; // iLenth;//L 0xF1->30*12
  1675. memcpy((uint8_t *)&Check.tx_buf[10], data, iLenth);
  1676. Check.tx_buf[10 + iLenth] = check_sum(&Check.tx_buf[2], 8 + iLenth);
  1677. Check.tx_buf[11 + iLenth] = 0x16;
  1678. finsh_tx(Check.tx_buf, 12 + iLenth);
  1679. }
  1680. // add by yuewei 20260331
  1681. /**
  1682. * @brief (0X25)
  1683. * @param 无
  1684. * @retval 无
  1685. */
  1686. void check_report_hour_data(void)
  1687. {
  1688. uint16_t iLenth = 244;
  1689. LOG("check_report_hour_data\n");
  1690. // start yyyymmdd != end yyyymmdd
  1691. if ((Check.rx_buf[9] != Check.rx_buf[13]) ||
  1692. (Check.rx_buf[10] != Check.rx_buf[14]) ||
  1693. (Check.rx_buf[11] != Check.rx_buf[15]) ||
  1694. (Check.rx_buf[12] != Check.rx_buf[16]))
  1695. {
  1696. return;
  1697. }
  1698. uint8_t data[iLenth];
  1699. get_hour_record_data(Check.rx_buf[14], Check.rx_buf[15], Check.rx_buf[16], data);
  1700. CheckComHead(0x25); // cmd
  1701. Check.tx_buf[9] = iLenth; // L
  1702. memcpy((uint8_t *)&Check.tx_buf[10], data, iLenth);
  1703. Check.tx_buf[10 + iLenth] = check_sum(&Check.tx_buf[2], 8 + iLenth);
  1704. Check.tx_buf[11 + iLenth] = 0x16;
  1705. finsh_tx(Check.tx_buf, 12 + iLenth);
  1706. }
  1707. // add by yuewei 20260331
  1708. /**
  1709. * @brief (0X27)
  1710. * @param 无
  1711. * @retval 无
  1712. */
  1713. void check_report_5_min_data(void)
  1714. {
  1715. uint16_t iLenth = 2 * 192;
  1716. LOG("check_report_5_min_data\n");
  1717. uint8_t data[iLenth];
  1718. GET_5MIN_REOCORD_DEF record_data;
  1719. record_data.start_time[0] = Check.rx_buf[10]; // wxl2026-5-18
  1720. record_data.start_time[1] = Check.rx_buf[11];
  1721. record_data.start_time[2] = Check.rx_buf[12];
  1722. record_data.start_time[3] = Check.rx_buf[13];
  1723. record_data.start_time[4] = Check.rx_buf[14];
  1724. record_data.start_time[5] = 0;
  1725. record_data.end_time[0] = Check.rx_buf[16]; // wxl2026-5-18
  1726. record_data.end_time[1] = Check.rx_buf[17];
  1727. record_data.end_time[2] = Check.rx_buf[18];
  1728. record_data.end_time[3] = Check.rx_buf[19];
  1729. record_data.end_time[4] = Check.rx_buf[20];
  1730. record_data.end_time[5] = 0;
  1731. get_5min_record_data(&record_data, 0, data);
  1732. CheckComHead(0x27); // cmd
  1733. Check.tx_buf[9] = iLenth; // L
  1734. memcpy((uint8_t *)&Check.tx_buf[10], data, iLenth);
  1735. Check.tx_buf[10 + iLenth] = check_sum(&Check.tx_buf[2], 8 + iLenth);
  1736. Check.tx_buf[11 + iLenth] = 0x16;
  1737. finsh_tx(Check.tx_buf, 12 + iLenth);
  1738. }
  1739. // add by yuewei 20260331
  1740. /**
  1741. * @brief (0X29)
  1742. * @param 无
  1743. * @retval 无
  1744. */
  1745. void check_report_log_data(void)
  1746. {
  1747. uint16_t iLenth = 30 * 13;
  1748. LOG("check_report_log_data\n");
  1749. uint8_t data[iLenth];
  1750. get_log_record(0, 30, data);
  1751. CheckComHead(0x29); // cmd
  1752. Check.tx_buf[9] = iLenth; // L
  1753. memcpy((uint8_t *)&Check.tx_buf[10], data, iLenth);
  1754. Check.tx_buf[10 + iLenth] = check_sum(&Check.tx_buf[2], 8 + iLenth);
  1755. Check.tx_buf[11 + iLenth] = 0x16;
  1756. finsh_tx(Check.tx_buf, 12 + iLenth);
  1757. }
  1758. // add by yuewei 20260331
  1759. /**
  1760. * @brief (0X2A)
  1761. * @param 无
  1762. * @retval 无
  1763. */
  1764. void check_report_status(void)
  1765. {
  1766. uint16_t iLenth = 129;
  1767. uint32_t iTemp;
  1768. rtc_run_t *max_fr_rr;
  1769. rtc_run_t *rr;
  1770. LOG("check_report_status\n");
  1771. CheckComHead(0x2A); // cmd
  1772. Check.tx_buf[9] = iLenth; // L
  1773. // total positive
  1774. iTemp = FwCumFlow * 0.0001;
  1775. Check.tx_buf[10] = (uint8_t)iTemp;
  1776. Check.tx_buf[11] = (uint8_t)(iTemp >> 8);
  1777. Check.tx_buf[12] = (uint8_t)(iTemp >> 16);
  1778. Check.tx_buf[13] = (uint8_t)(iTemp >> 24);
  1779. // total inverse
  1780. iTemp = BwCumFlow * 0.0001;
  1781. Check.tx_buf[14] = (uint8_t)iTemp;
  1782. Check.tx_buf[15] = (uint8_t)(iTemp >> 8);
  1783. Check.tx_buf[16] = (uint8_t)(iTemp >> 16);
  1784. Check.tx_buf[17] = (uint8_t)(iTemp >> 24);
  1785. // daily max flow
  1786. iTemp = (uint32_t)(get_max_flow_rate() * 1000); // 3位小数点
  1787. Check.tx_buf[18] = (uint8_t)(iTemp);
  1788. Check.tx_buf[19] = (uint8_t)(iTemp >> 8);
  1789. Check.tx_buf[20] = (uint8_t)(iTemp >> 16);
  1790. Check.tx_buf[21] = (uint8_t)(iTemp >> 24);
  1791. // daily max flow time
  1792. get_max_flow_rate_time(&max_fr_rr);
  1793. if ((max_fr_rr->year == 0) && (max_fr_rr->month == 0))
  1794. {
  1795. // no data record
  1796. Check.tx_buf[22] = 0;
  1797. }
  1798. else
  1799. {
  1800. Check.tx_buf[22] = 0x14;
  1801. }
  1802. Check.tx_buf[23] = max_fr_rr->year;
  1803. Check.tx_buf[24] = max_fr_rr->month;
  1804. Check.tx_buf[25] = max_fr_rr->day;
  1805. Check.tx_buf[26] = max_fr_rr->hour;
  1806. Check.tx_buf[27] = max_fr_rr->minute;
  1807. Check.tx_buf[28] = max_fr_rr->second;
  1808. // current temperature
  1809. if (WaterTemp > 100.0f || WaterTemp < 0.0f)
  1810. {
  1811. iTemp = 0x8000; // yw 2026-5-22
  1812. }
  1813. else
  1814. {
  1815. iTemp = (uint32_t)(WaterTemp * 10 + 0.5);
  1816. }
  1817. Check.tx_buf[29] = (uint8_t)(iTemp);
  1818. Check.tx_buf[30] = (uint8_t)(iTemp >> 8);
  1819. // water pressure
  1820. if (Paramx.pressure_sensor == 1)
  1821. {
  1822. Check.tx_buf[31] = (uint8_t)(WaterPressure * 100);
  1823. }
  1824. else
  1825. {
  1826. Check.tx_buf[31] = 0x00;//无压力传感器,修改为零
  1827. }
  1828. // current voltage
  1829. // battery voltage unit: 0.1V
  1830. /*if (McuVbat > McuVTxbat)
  1831. {
  1832. // 判断哪个电池低就上传哪个数据
  1833. iTemp = McuVTxbat / 100;
  1834. }
  1835. else
  1836. {
  1837. iTemp = McuVbat / 100;
  1838. }*/
  1839. iTemp = McuVbat / 100; // wxl2026-6-2
  1840. Check.tx_buf[32] = (unsigned char)iTemp;
  1841. // now time
  1842. get_rtc_run(&rr);
  1843. Check.tx_buf[33] = 0x14;
  1844. Check.tx_buf[34] = rr->year;
  1845. Check.tx_buf[35] = rr->month;
  1846. Check.tx_buf[36] = rr->day;
  1847. Check.tx_buf[37] = rr->hour;
  1848. Check.tx_buf[38] = rr->minute;
  1849. Check.tx_buf[39] = rr->second;
  1850. Param_t *param;
  1851. GetParam(&param);
  1852. // version
  1853. Check.tx_buf[40] = 'V';
  1854. Check.tx_buf[41] = VER_TYPE;
  1855. Check.tx_buf[42] = VER_OPERATOR;
  1856. Check.tx_buf[43] = VER_MODULE;
  1857. Check.tx_buf[44] = VER_FIRMWARE;
  1858. // diameter
  1859. Check.tx_buf[45] = (unsigned char)(param->diameter);
  1860. Check.tx_buf[46] = (unsigned char)(param->diameter >> 8);
  1861. // channel number
  1862. Check.tx_buf[47] = param->chan_num;
  1863. // main server ip
  1864. Check.tx_buf[48] = param->main_server_ip[0];
  1865. Check.tx_buf[49] = param->main_server_ip[1];
  1866. Check.tx_buf[50] = param->main_server_ip[2];
  1867. Check.tx_buf[51] = param->main_server_ip[3];
  1868. // main server port
  1869. Check.tx_buf[52] = (unsigned char)(param->main_server_port);
  1870. Check.tx_buf[53] = (unsigned char)(param->main_server_port >> 8);
  1871. // sub server ip
  1872. Check.tx_buf[54] = param->sub_server_ip[0];
  1873. Check.tx_buf[55] = param->sub_server_ip[1];
  1874. Check.tx_buf[56] = param->sub_server_ip[2];
  1875. Check.tx_buf[57] = param->sub_server_ip[3];
  1876. // sub server port
  1877. Check.tx_buf[58] = (unsigned char)(param->sub_server_port);
  1878. Check.tx_buf[59] = (unsigned char)(param->sub_server_port >> 8);
  1879. // report base time
  1880. Check.tx_buf[60] = param->report_base_hour;
  1881. Check.tx_buf[61] = param->report_base_min;
  1882. Check.tx_buf[62] = param->report_base_sec;
  1883. // report interval minutes
  1884. Check.tx_buf[63] = (unsigned char)(param->report_interval);
  1885. Check.tx_buf[64] = (unsigned char)(param->report_interval >> 8);
  1886. // DMA report start time
  1887. Check.tx_buf[65] = param->dma_report_start[0];
  1888. Check.tx_buf[66] = param->dma_report_start[1];
  1889. Check.tx_buf[67] = param->dma_report_start[2];
  1890. // DMA report end time
  1891. Check.tx_buf[68] = param->dma_report_end[0];
  1892. Check.tx_buf[69] = param->dma_report_end[1];
  1893. Check.tx_buf[70] = param->dma_report_end[2];
  1894. // DMA report interval time
  1895. Check.tx_buf[71] = param->dma_interval;
  1896. // settlement day
  1897. Check.tx_buf[72] = param->settlement_day;
  1898. // high temperature alarm threshold
  1899. Check.tx_buf[73] = (unsigned char)(param->high_temp_alarm_thres);
  1900. Check.tx_buf[74] = (unsigned char)(param->high_temp_alarm_thres >> 8);
  1901. // low temperature alarm threshold
  1902. Check.tx_buf[75] = (unsigned char)(param->low_temp_alarm_thres);
  1903. Check.tx_buf[76] = (unsigned char)(param->low_temp_alarm_thres >> 8);
  1904. // large flow alarm threshold
  1905. Check.tx_buf[77] = (unsigned char)(param->large_flow_alarm_thres);
  1906. Check.tx_buf[78] = (unsigned char)(param->large_flow_alarm_thres >> 8);
  1907. Check.tx_buf[79] = (unsigned char)(param->large_flow_alarm_thres >> 16);
  1908. Check.tx_buf[80] = (unsigned char)(param->large_flow_alarm_thres >> 24);
  1909. // large flow continue monitor time
  1910. Check.tx_buf[81] = (unsigned char)(param->large_flow_cnti_moni_time);
  1911. Check.tx_buf[82] = (unsigned char)(param->large_flow_cnti_moni_time >> 8);
  1912. // continue flow continue monitor time
  1913. Check.tx_buf[83] = (unsigned char)(param->cnti_flow_cnti_moni_time);
  1914. Check.tx_buf[84] = (unsigned char)(param->cnti_flow_cnti_moni_time >> 8);
  1915. // leak flow alarm threshold
  1916. Check.tx_buf[85] = (unsigned char)(param->leak_flow_alarm_thres);
  1917. Check.tx_buf[86] = (unsigned char)(param->leak_flow_alarm_thres >> 8);
  1918. Check.tx_buf[87] = (unsigned char)(param->leak_flow_alarm_thres >> 16);
  1919. Check.tx_buf[88] = (unsigned char)(param->leak_flow_alarm_thres >> 24);
  1920. // leak flow continue monitor time
  1921. Check.tx_buf[89] = (unsigned char)(param->leak_flow_cnti_moni_time);
  1922. Check.tx_buf[90] = (unsigned char)(param->leak_flow_cnti_moni_time >> 8);
  1923. // high press alarm threshold
  1924. Check.tx_buf[91] = (unsigned char)(param->high_press_alarm_thres);
  1925. // low press alarm threshold
  1926. Check.tx_buf[92] = (unsigned char)(param->low_press_alarm_thres);
  1927. // pressure sensor
  1928. Check.tx_buf[93] = param->pressure_sensor;
  1929. ////////////nb para/////////
  1930. Nb_Run_t *nb_run;
  1931. char tmp[3];
  1932. uint16_t i;
  1933. GetNbRun(&nb_run);
  1934. // IMEI len = 8
  1935. if (strlen(nb_run->imei) == 15)
  1936. {
  1937. tmp[0] = '0';
  1938. tmp[1] = nb_run->imei[0];
  1939. tmp[2] = 0;
  1940. StrToHexBuf((char *)&Check.tx_buf[94], tmp, 2);
  1941. StrToHexBuf((char *)&Check.tx_buf[95], &(nb_run->imei[1]), 14);
  1942. }
  1943. else
  1944. {
  1945. StrToHexBuf((char *)&Check.tx_buf[94], tmp, 16);
  1946. }
  1947. // cell id in AT+NUESTATS response
  1948. Check.tx_buf[102] = (unsigned char)(nb_run->bts_id);
  1949. Check.tx_buf[103] = (unsigned char)(nb_run->bts_id >> 8);
  1950. Check.tx_buf[104] = 0x00;
  1951. Check.tx_buf[105] = 0x00;
  1952. // physical cell id
  1953. Check.tx_buf[106] = (unsigned char)(nb_run->physical_cell_id);
  1954. Check.tx_buf[107] = (unsigned char)(nb_run->physical_cell_id >> 8);
  1955. // rsrp
  1956. Check.tx_buf[108] = (unsigned char)(nb_run->rsrp);
  1957. Check.tx_buf[109] = (unsigned char)(nb_run->rsrp >> 8);
  1958. // snr
  1959. Check.tx_buf[110] = (unsigned char)(nb_run->snr);
  1960. Check.tx_buf[111] = (unsigned char)(nb_run->snr >> 8);
  1961. // csq
  1962. Check.tx_buf[112] = nb_run->rssi;
  1963. // iccid len = 10
  1964. for (i = 0; i < 10; i++)
  1965. {
  1966. Check.tx_buf[113 + i] = (unsigned char)(((nb_run->ccid[2 * i] - '0') << 4) | (nb_run->ccid[2 * i + 1] - '0'));
  1967. }
  1968. /////////////////////////
  1969. // ErrorNo
  1970. uint32_t *err_no;
  1971. all_alarm_status(&err_no);
  1972. Check.tx_buf[123] = (uint8_t)(*err_no);
  1973. Check.tx_buf[124] = (uint8_t)(*err_no >> 8);
  1974. Check.tx_buf[125] = (uint8_t)(*err_no >> 16);
  1975. Check.tx_buf[126] = (uint8_t)(*err_no >> 24);
  1976. // b
  1977. Check.tx_buf[127] = (uint8_t)param->b;
  1978. Check.tx_buf[128] = (uint8_t)(param->b >> 8);
  1979. // n
  1980. Check.tx_buf[129] = (uint8_t)param->n;
  1981. Check.tx_buf[130] = (uint8_t)(param->n >> 8);
  1982. // q
  1983. Check.tx_buf[131] = (uint8_t)param->q;
  1984. Check.tx_buf[132] = (uint8_t)(param->q >> 8);
  1985. // r
  1986. Check.tx_buf[133] = (uint8_t)param->r;
  1987. Check.tx_buf[134] = (uint8_t)(param->r >> 8);
  1988. // ErrorNo
  1989. iTemp = TdcItf.pipe_coe * 1000000;
  1990. Check.tx_buf[135] = (uint8_t)(iTemp);
  1991. Check.tx_buf[136] = (uint8_t)(iTemp >> 8);
  1992. Check.tx_buf[137] = (uint8_t)(iTemp >> 16);
  1993. Check.tx_buf[138] = (uint8_t)(iTemp >> 24);
  1994. Check.tx_buf[10 + iLenth] = check_sum(&Check.tx_buf[2], 8 + iLenth);
  1995. Check.tx_buf[11 + iLenth] = 0x16;
  1996. finsh_tx(Check.tx_buf, 12 + iLenth);
  1997. }
  1998. void CheckRxHandler(void)
  1999. {
  2000. uint8_t cmd = 0;
  2001. if (Check.rx_flag & CHECK_RX_VLD)
  2002. {
  2003. Check.rx_flag &= ~CHECK_RX_VLD;
  2004. if (!CheckRxValid())
  2005. {
  2006. CheckRxErr();
  2007. //////return err data//////////
  2008. // uint8_t i;
  2009. // for(i=0;i<Check.rx_idx;i++)
  2010. // Check.tx_buf[i]=Check.rx_buf[i];
  2011. // finsh_tx(Check.tx_buf, Check.rx_idx);
  2012. // LOG("-%d-\r\n",Check.rx_idx);
  2013. /////////////////////
  2014. return; // add by yuewei 20260608
  2015. }
  2016. Check.rx_err &= ~CHECK_RX_ERROR;
  2017. // cmd is 0xE0 and above is private debug cmd
  2018. cmd = Check.rx_buf[1];
  2019. if (cmd == 0x00)
  2020. {
  2021. check_set_hardware_param(); // add by yuewei 20260316
  2022. }
  2023. else if (cmd == 0x01)
  2024. {
  2025. check_set_trigger_nb_report(); // add by yuewei 20260316
  2026. }
  2027. else if (cmd == 0x02)
  2028. {
  2029. check_set_start_upgrade(); // add by yuewei 20260414
  2030. }
  2031. else if (cmd == 0x10)
  2032. {
  2033. check_set_ip_and_port(); // add by yuewei 20260331
  2034. }
  2035. else if (cmd == 0x11)
  2036. {
  2037. check_set_report_base_time(); // add by yuewei 20260331
  2038. }
  2039. else if (cmd == 0x12)
  2040. {
  2041. check_set_dma_report_time(); // add by yuewei 20260331
  2042. }
  2043. else if (cmd == 0x13)
  2044. {
  2045. check_set_date_time(); // add by yuewei 20260331
  2046. }
  2047. else if (cmd == 0x14)
  2048. {
  2049. check_set_flow_param(); // add by yuewei 20260331
  2050. }
  2051. else if (cmd == 0x15)
  2052. {
  2053. check_set_pressure_param(); // add by yuewei 20260331
  2054. }
  2055. else if (cmd == 0x16)
  2056. {
  2057. check_set_temperature_param(); // add by yuewei 20260331
  2058. }
  2059. else if (cmd == 0x17)
  2060. {
  2061. check_set_settle_day(); // add by yuewei 20260331
  2062. }
  2063. else if (cmd == 0x18)
  2064. {
  2065. check_set_base_read(); // add by yuewei 20260331
  2066. }
  2067. else if (cmd == 0x20)
  2068. {
  2069. check_report_month_data(); // add by yuewei 20260331
  2070. }
  2071. else if (cmd == 0x22)
  2072. {
  2073. check_report_day_data(); // add by yuewei 20260331
  2074. }
  2075. else if (cmd == 0x24)
  2076. {
  2077. check_report_hour_data(); // add by yuewei 20260331
  2078. }
  2079. else if (cmd == 0x26)
  2080. {
  2081. check_report_5_min_data(); // add by yuewei 20260331
  2082. }
  2083. else if (cmd == 0x28)
  2084. {
  2085. check_report_log_data(); // add by yuewei 20260331
  2086. }
  2087. else if (cmd == 0x2A)
  2088. {
  2089. check_report_status(); // add by yuewei 20260331
  2090. }
  2091. else if (cmd == 0x30)
  2092. { // enter check state chapter 4 format 1 set check param start normal meter
  2093. // modified by yuewei 20260310 start
  2094. // CheckMeterEnd();
  2095. CheckMeterStart();
  2096. // modified by yuewei 20260310 end
  2097. }
  2098. else if (cmd == 0x31)
  2099. { // add by yuewei 20260304 进入正常模式(退出检定模式)
  2100. check_enter_normal_mode();
  2101. }
  2102. else if (cmd == 0x35)
  2103. { // start meter chapter 4 data format 6 not set check param
  2104. CheckMeterStart();
  2105. LOG("**********************************************************\n");
  2106. LOG("--%.6f--%.6f\n",Check.curr_cum_flow, Check.start_cum_flow);
  2107. }
  2108. else if (cmd == 0x3C)
  2109. { // add by yuewei 20260304 清表数据(0X3C),主要清正、反向累计水量
  2110. check_clear_meter();
  2111. }
  2112. else if (cmd == 0x3D)
  2113. { // add by yuewei 20260304 设置声道数(0X3D)
  2114. check_set_channel_num();
  2115. }
  2116. else if (cmd == 0x52)
  2117. { // read meter address chapter 4 data format 17
  2118. CheckReadAddr();
  2119. }
  2120. else if (cmd == 0x53)
  2121. { // set meter address chapter 4 data format 18
  2122. CheckSetAddr();
  2123. }
  2124. else if (cmd == 0x58)
  2125. { // add by yuewei 20260304 设置静态ot(0X58)
  2126. check_set_static_ot();
  2127. }
  2128. else if (cmd == 0x59)
  2129. { // read check data chapter 4 data format 24
  2130. CheckReadCumFlow();
  2131. }
  2132. else if (cmd == 0x62)
  2133. { // read flow param
  2134. CheckReadFlowParam();
  2135. LOG("-----------------------------------------------------------\n");
  2136. LOG("--%.6f--%.6f\n",Check.curr_cum_flow, Check.start_cum_flow);
  2137. }
  2138. else if (cmd == 0x63)
  2139. { // set flow param chapter 4 format 8/4
  2140. CheckSetFlowParam();
  2141. }
  2142. else if (cmd == 0x64)
  2143. { // read delta t and C
  2144. CheckReadDeltaTC();
  2145. }
  2146. else if (cmd == 0x65)
  2147. { // add by yuewei 20260304 设置Δt和C(0X65)
  2148. check_set_delta_t_and_c();
  2149. }
  2150. else if (cmd == 0x67)
  2151. { // add by yuewei 20260316 设置bnqr(0X67)
  2152. check_set_bnqr();
  2153. }
  2154. else if (cmd == 0x69)
  2155. { // add by yuewei 20260304 设置口径和温度补偿值(0X69)
  2156. check_set_diameter_and_temperature_compensation();
  2157. }
  2158. else if (cmd == 0x6A)
  2159. { // add by yuewei 20260304 获取口径和温度补偿值(0X6A)
  2160. check_get_diameter_and_temperature_compensation();
  2161. }
  2162. else if (cmd == 0x6B)
  2163. { // read flow param in tf board but the last param is 00 01
  2164. CheckReadFlowParamInTf();
  2165. }
  2166. else if (cmd == 0x85)
  2167. { // add by yuewei 20260304 读取自动匹配电平参数(0X85)
  2168. // check_get_auto_match_volage_level();
  2169. }
  2170. else if (cmd == 0x86)
  2171. { // add by yuewei 20260304 设置自动匹配电平参数(0X86)
  2172. // check_set_auto_match_volage_level();
  2173. }
  2174. else if (cmd == 0xA5)
  2175. { // add by yuewei 20260304 读取静态ot(0XA5)
  2176. check_get_static_ot();
  2177. }
  2178. else if (cmd == 0xE0)
  2179. { // switch ir to shell work mode
  2180. // CheckSwitchIrWorkMode();
  2181. }
  2182. else if (cmd == 0xE1)
  2183. { // set tof state(0-TOF_ST_METER; 1-TOF_ST_CALIB)
  2184. // CheckSetTofState();
  2185. }
  2186. else if (cmd == 0xE2)
  2187. { // set ref flow
  2188. // CheckSetRefFlow();
  2189. }
  2190. Check.rx_idx = 0;
  2191. rx_data_len = 0;
  2192. rx_state = CHECK_RX_HEAD;
  2193. Check.rx_over_time = 0;
  2194. }
  2195. }
  2196. extern uint16_t LcdTimer;
  2197. uint8_t CheckRxState(void)
  2198. {
  2199. if (Check.rx_err && LcdTimer == 0)
  2200. {
  2201. Check.rx_err = 0;
  2202. }
  2203. return (Check.rx_err & CHECK_RX_ERROR) ? 1 : 0;
  2204. }
  2205. void CheckingCoundown(void)
  2206. {
  2207. if (Check.state != CHECKING)
  2208. {
  2209. return;
  2210. }
  2211. if (Check.checking_cnt > 0)
  2212. {
  2213. Check.checking_cnt--;
  2214. if (Check.checking_cnt == 0)
  2215. {
  2216. Check.state = CHECKED;
  2217. }
  2218. }
  2219. }