TerminalSlave485_jt808.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*********************************************************
  2. //file :hd_dev_gpio.c
  3. //author :boly
  4. //date :2021/10/21
  5. //version :V1.0
  6. //brief :GSP HARD层GPIO接口C文件
  7. *********************************************************/
  8. /* Includes-----------------------------------------------------------------------------------*/
  9. #define APP_TERMINALSLAVE485_JT808_USE
  10. #ifdef APP_TERMINALSLAVE485_JT808_USE
  11. /* Includes-----------------------------------------------------------------------------------*/
  12. #include "TerminalSlave485.h"
  13. #include "TerminalSlave485_jt808.h"
  14. #include "CollectMaster485.h"
  15. #include "ScreenMaster485.h"
  16. #include "KeySlave485.h"
  17. #include "Elec_Seal.h"
  18. #include "usart.h"
  19. #include "Data_deal.h"
  20. #include "Randomcode.h"
  21. #include "spi.h"
  22. #include "Dwin.h"
  23. #include "DS1302.h"
  24. #include "leaf_ota.h"
  25. #include "md5c.h"
  26. #include "cmsis_os.h"
  27. #include "func_ram_record.h"
  28. /* Private macro------------------------------------------------------------------------------*/
  29. /* Private typedef----------------------------------------------------------------------------*/
  30. typedef struct SEND_DATA_UNIT_STC
  31. {
  32. uint32_t flowID; //发送数据ID
  33. uint32_t lenth; //发送数据长度
  34. uint32_t send_cnt; //发送计数器
  35. uint8_t buf[DATA_UNIT_SIZE - sizeof(uint32_t)*3]; //发送数据缓冲区
  36. }SEND_DATA_UNIT_STC;
  37. typedef struct JT808_DATA_OBJ
  38. {
  39. uint32_t flowID; //Y为32Bit的流水号-高位在前,此流水号来源于控制板主动上报的传感器数据包内,如A1封装
  40. uint32_t back_cnt; //接收超时计数
  41. uint32_t link_cnt; //连接计数器
  42. uint32_t link_ok; //连接标志位
  43. SEND_DATA_UNIT_STC send_data;
  44. }JT808_DATA_OBJ;
  45. /* Private define-----------------------------------------------------------------------------*/
  46. /* Private variables--------------------------------------------------------------------------*/
  47. Pass41SensorScanParam_TypeDef Pass41SensorScanParam;
  48. JT808_DATA_OBJ jt808_obj = {
  49. .flowID = 1,
  50. .back_cnt = 0, //接收超时计数
  51. .link_cnt = 0,
  52. .link_ok = 0,
  53. .send_data.flowID = 0,
  54. .send_data.lenth = 0,
  55. .send_data.buf = {0},
  56. };
  57. extern uint8_t Cang_IO_tbak[4][64];
  58. extern uint32_t overflow_cnt_bak;
  59. extern unsigned char overflow_flag_bak;
  60. extern uint8_t Cang01_IO[64];
  61. extern uint8_t Cang02_IO[64];
  62. extern uint8_t Cang03_IO[64];
  63. extern uint8_t Cang04_IO[64];
  64. extern uint8_t Cang05_IO[64];
  65. extern uint8_t Cang06_IO[64];
  66. extern uint8_t Cang07_IO[64];
  67. extern uint8_t Cang08_IO[64];
  68. extern uint32_t overflow_cnt;
  69. /* Private function prototypes----------------------------------------------------------------*/
  70. int jt808_Build_0x02_TimeCmd(uint8_t *pBuf);
  71. int jt808_Build_0x01_flowID(uint8_t *pBuf, uint32_t flow_id);
  72. int jt808_Build_0x61_ManHole_Big_SW(uint8_t *pBuf);
  73. int jt808_Build_0x62_ManHole_Small_SW(uint8_t *pBuf);
  74. int jt808_Build_0x63_Dump_info(uint8_t *pBuf);
  75. int jt808_Build_0x64_HaiDiFa(uint8_t *pBuf);
  76. int jt808_Build_0x6d_Cang_info(uint8_t *pBuf);
  77. int jt808_Build_0x70_Sealing(uint8_t *pBuf);
  78. int jt808_Build_0x71_DumpBox_info(uint8_t *pBuf);
  79. int jt808_Build_0x72_ManHole_Box_info(uint8_t *pBuf);
  80. void jt808_update_0x40_data(void);
  81. int jt808_BuildSealTxCmd(uint8_t *pBuf, uint8_t *pEmergencyflag); // 40命令
  82. void mem_swap(uint8_t *des,int len)
  83. {
  84. int i,tlen;
  85. uint8_t dat;
  86. tlen = (len>>1);
  87. len--;
  88. for(i=0;i<tlen;i++)
  89. {
  90. dat = des[i];
  91. des[i] = des[len-i];
  92. des[len-i]=dat;
  93. }
  94. return;
  95. }
  96. /**
  97. ***************************************
  98. * 构建A1命令数据 -- hex制式
  99. * 输入:
  100. * pBuf-构建数据的存放首地址
  101. * pEmergy,如果是数据有变动,表示紧急上报,将标记存于此地址
  102. * flowID, 流水id值
  103. * 返回: 构建数据的字节总数
  104. ***************************************
  105. */
  106. int jt808_Build_A1_Cmd(uint8_t *pBuf, uint32_t flowID, uint8_t *pEmergencyflag)
  107. {
  108. int send_pos=0;
  109. unsigned char emergencyflag=0;
  110. unsigned char Lrc_temp;
  111. unsigned char overflow_flag=0;
  112. unsigned char temp_i = 0x00;
  113. UNUSED(Lrc_temp);
  114. UNUSED(temp_i);
  115. jt808_update_0x40_data();
  116. //标识头 总长度 紧急上报位 数据状态 仓数
  117. pBuf[send_pos++] = 0xA1;
  118. pBuf[send_pos++] = 120;
  119. if(StoreNumber>0)
  120. {
  121. if(memcmp(Cang_IO_tbak[0],Cang01_IO,64))
  122. {
  123. emergencyflag=0x80;
  124. }
  125. memcpy(Cang_IO_tbak[0],Cang01_IO,64);
  126. }
  127. if(StoreNumber>1)
  128. {
  129. if(memcmp(Cang_IO_tbak[1],Cang02_IO,64))
  130. {
  131. emergencyflag=0x80;
  132. }
  133. memcpy(Cang_IO_tbak[1],Cang02_IO,64);
  134. }
  135. if(StoreNumber>2)
  136. {
  137. if(memcmp(Cang_IO_tbak[2],Cang03_IO,64))
  138. {
  139. emergencyflag=0x80;
  140. }
  141. memcpy(Cang_IO_tbak[2],Cang03_IO,64);
  142. }
  143. if(StoreNumber>3)
  144. {
  145. if(memcmp(Cang_IO_tbak[3],Cang04_IO,64))
  146. {
  147. emergencyflag=0x80;
  148. }
  149. memcpy(Cang_IO_tbak[3],Cang04_IO,64);
  150. }
  151. if(StoreNumber > 4)
  152. {
  153. StoreNumber = 4;
  154. }
  155. if((overflow_cnt==0) && (overflow_cnt==overflow_cnt_bak))
  156. {
  157. overflow_flag=0;
  158. }
  159. else
  160. {
  161. overflow_flag=1;
  162. }
  163. overflow_cnt_bak=overflow_cnt;
  164. if(overflow_flag_bak!=overflow_flag)
  165. {
  166. emergencyflag=0x80;
  167. }
  168. overflow_flag_bak=overflow_flag;
  169. pBuf[send_pos++] = emergencyflag; //紧急上报位
  170. pBuf[send_pos++] = 0x00; //数据状态:00正常;01未收到数据;02乱码
  171. pBuf[send_pos++] = StoreNumber;
  172. pBuf[send_pos++] = 0x22;
  173. pBuf[send_pos++] = overflow_flag;
  174. //61 人孔大盖 开关
  175. send_pos += jt808_Build_0x61_ManHole_Big_SW((uint8_t *)pBuf+send_pos);
  176. //62 人孔小盖 开关
  177. send_pos += jt808_Build_0x62_ManHole_Small_SW((uint8_t *)pBuf+send_pos);
  178. //63 卸油阀 开关
  179. send_pos += jt808_Build_0x63_Dump_info((uint8_t *)pBuf+send_pos);
  180. //64 底阀 开关
  181. send_pos += jt808_Build_0x64_HaiDiFa((uint8_t *)pBuf+send_pos);
  182. //6D 仓状态
  183. send_pos += jt808_Build_0x6d_Cang_info((uint8_t *)pBuf+send_pos);
  184. //70 铅封状态
  185. send_pos += jt808_Build_0x70_Sealing((uint8_t *)pBuf+send_pos);
  186. //71 卸油箱门 开关
  187. send_pos += jt808_Build_0x71_DumpBox_info((uint8_t *)pBuf+send_pos);
  188. //72 人孔箱护罩 开关
  189. send_pos += jt808_Build_0x72_ManHole_Box_info((uint8_t *)pBuf+send_pos);
  190. //01 流水号 4字节 整数
  191. send_pos += jt808_Build_0x01_flowID((uint8_t *)pBuf+send_pos, flowID);
  192. //02 时间戳 6字节 字符串 BCD[6] YY-MM-DD-hh-mm-ss(GMT+8 时间,本标准中之后涉及的时间均采用此时区)
  193. send_pos += jt808_Build_0x02_TimeCmd((uint8_t *)pBuf+send_pos);
  194. //数据帧长度
  195. pBuf[1] = send_pos-2;
  196. #if 0
  197. while (send_pos<(120+2))
  198. {
  199. pBuf[send_pos++] = 0;
  200. };
  201. #endif
  202. if (pEmergencyflag) *pEmergencyflag = emergencyflag;
  203. return send_pos;
  204. }
  205. /**
  206. ***************************************
  207. * 更新老协议0x40系统状态
  208. * 输入:void
  209. * 返回: void
  210. ***************************************
  211. */
  212. uint8_t JT808_0x40_buf[256]={0};//大小+1 136
  213. uint8_t JT808_0x40_len = 0;
  214. void jt808_update_0x40_data(void)
  215. {
  216. uint8_t JT808_emergencyflag = 0;
  217. JT808_0x40_len = jt808_BuildSealTxCmd(JT808_0x40_buf, &JT808_emergencyflag);
  218. return;
  219. }
  220. /**
  221. ***************************************
  222. * 构建6D 仓状态
  223. * 输入:pBuf-构建数据的存放首地址
  224. * 返回: 构建数据的字节总数
  225. ***************************************
  226. */
  227. int jt808_Build_0x6d_Cang_info(uint8_t *pBuf)
  228. {
  229. int idx=0;
  230. int i = 0;
  231. pBuf[idx++] = 0x6D; //类型
  232. for (i = 0; i < StoreNumber; i++)
  233. {
  234. //pBuf[idx++] = 1; //每仓数量
  235. //Sealing_Data.Sealing_state_oiltype[i] :仓状态与油品:
  236. //Sealing_Data.Sealing_state_oiltype[i] = (Byte_high<<4)|Byte_low;
  237. //高4位值1:装油,2:有油,3:卸油,4:空仓(无油),低4位代表油品,高四位非等于4(空仓)值默认1,否则为0;
  238. pBuf[idx++] = (Sealing_Data.Sealing_state_oiltype[i] &0xf0)>> 4;
  239. }
  240. return idx;
  241. }
  242. /**
  243. ***************************************
  244. * 构建61 人孔大盖 开关状态
  245. * 输入:pBuf-构建数据的存放首地址
  246. * 返回: 构建数据的字节总数
  247. ***************************************
  248. */
  249. int jt808_Build_0x61_ManHole_Big_SW(uint8_t *pBuf)
  250. {
  251. int idx=0;
  252. int i,j;
  253. if(((Config_info_all.ManHole_Big_info/StoreNumber) == 0)
  254. ||((Config_info_all.ManHole_Big_info&0x80)==0x80))
  255. {
  256. return 0;
  257. }
  258. pBuf[idx++] = 0x61;
  259. for (i = 0; i < StoreNumber; i++)
  260. {
  261. pBuf[idx++] = Config_info_all.ManHole_Big_info/StoreNumber;
  262. for(j=0;j<Config_info_all.ManHole_Big_info/StoreNumber;++j)
  263. {
  264. pBuf[idx++] = Cang_IO_tbak[i][indexDaGai01+10*j];
  265. }
  266. }
  267. return idx;
  268. }
  269. /**
  270. ***************************************
  271. * 构建62 人孔小盖 开关状态
  272. * 输入:pBuf-构建数据的存放首地址
  273. * 返回: 构建数据的字节总数
  274. ***************************************
  275. */
  276. int jt808_Build_0x62_ManHole_Small_SW(uint8_t *pBuf)
  277. {
  278. int idx=0;
  279. int i,j;
  280. if(((Config_info_all.ManHole_small_info/StoreNumber) == 0)
  281. ||((Config_info_all.ManHole_small_info&0x80)==0x80))
  282. {
  283. return 0;
  284. }
  285. pBuf[idx++] = 0x62;
  286. for (i = 0; i < StoreNumber; i++)
  287. {
  288. pBuf[idx++] = Config_info_all.ManHole_small_info/StoreNumber;
  289. for(j=0;j<Config_info_all.ManHole_small_info/StoreNumber;++j)
  290. {
  291. pBuf[idx++] = Cang_IO_tbak[i][indexXiaoGai01+10*j];
  292. }
  293. }
  294. return idx;
  295. }
  296. /**
  297. ***************************************
  298. * 构建63 卸油阀 开关
  299. * 输入:pBuf-构建数据的存放首地址
  300. * 返回: 构建数据的字节总数
  301. ***************************************
  302. */
  303. int jt808_Build_0x63_Dump_info(uint8_t *pBuf)
  304. {
  305. int idx=0;
  306. int i,j;
  307. if(((Config_info_all.Dump_info/StoreNumber) == 0)
  308. ||((Config_info_all.Dump_info&0x80)==0x80))
  309. {
  310. return 0;
  311. }
  312. pBuf[idx++] = 0x63;
  313. for (i = 0; i < StoreNumber; i++)
  314. {
  315. pBuf[idx++] = Config_info_all.Dump_info/StoreNumber;
  316. for(j=0;j<Config_info_all.Dump_info/StoreNumber;++j)
  317. {
  318. pBuf[idx++] = Cang_IO_tbak[i][indexXieYouFa01+10*j];
  319. }
  320. }
  321. return idx;
  322. }
  323. /**
  324. ***************************************
  325. * 构建72 人孔箱护罩 开关
  326. * 输入:pBuf-构建数据的存放首地址
  327. * 返回: 构建数据的字节总数
  328. ***************************************
  329. */
  330. int jt808_Build_0x72_ManHole_Box_info(uint8_t *pBuf)
  331. {
  332. int idx=0;
  333. int i,j;
  334. if(((Config_info_all.ManHole_Big_info/StoreNumber) == 0)
  335. ||((Config_info_all.ManHole_Big_info&0x80)==0x80))
  336. {
  337. return 0;
  338. }
  339. pBuf[idx++] = 0x72;
  340. for (i = 0; i < StoreNumber; i++)
  341. {
  342. pBuf[idx++] = Config_info_all.ManHole_Big_info/StoreNumber;
  343. for(j=0;j<Config_info_all.ManHole_Big_info/StoreNumber;++j)
  344. {
  345. pBuf[idx++] = Cang_IO_tbak[i][indexDaGai01+10*j];
  346. }
  347. }
  348. return idx;
  349. }
  350. /**
  351. ***************************************
  352. * 构建71 卸油箱门 开关
  353. * 输入:pBuf-构建数据的存放首地址
  354. * 返回: 构建数据的字节总数
  355. ***************************************
  356. */
  357. int jt808_Build_0x71_DumpBox_info(uint8_t *pBuf)
  358. {
  359. int idx=0;
  360. int i,j;
  361. if(((Config_info_all.Dump_info/StoreNumber) == 0)
  362. ||((Config_info_all.Dump_info&0x80)==0x80))
  363. {
  364. return 0;
  365. }
  366. pBuf[idx++] = 0x71;
  367. for (i = 0; i < StoreNumber; i++)
  368. {
  369. pBuf[idx++] = Config_info_all.Dump_info/StoreNumber;
  370. for(j=0;j<Config_info_all.Dump_info/StoreNumber;++j)
  371. {
  372. pBuf[idx++] = Cang_IO_tbak[i][indexXieYouFa01+10*j];
  373. }
  374. }
  375. return idx;
  376. }
  377. /**
  378. ***************************************
  379. * 构建64 底阀 开关
  380. * 输入:pBuf-构建数据的存放首地址
  381. * 返回: 构建数据的字节总数
  382. ***************************************
  383. */
  384. int jt808_Build_0x64_HaiDiFa(uint8_t *pBuf)
  385. {
  386. int idx=0;
  387. int i,j;
  388. if(((Config_info_all.HaiDiFa_info/StoreNumber) == 0)
  389. ||((Config_info_all.HaiDiFa_info&0x80)==0x80))
  390. {
  391. return 0;
  392. }
  393. pBuf[idx++] = 0x64;
  394. for (i = 0; i < StoreNumber; i++)
  395. {
  396. pBuf[idx++] = Config_info_all.HaiDiFa_info/StoreNumber;
  397. for(j=0;j<Config_info_all.HaiDiFa_info/StoreNumber;++j)
  398. {
  399. pBuf[idx++] = Cang_IO_tbak[i][indexHaiDiFa01+10*j];
  400. }
  401. }
  402. return idx;
  403. }
  404. /**
  405. ***************************************
  406. * 构建70 铅封状态
  407. * 输入:pBuf-构建数据的存放首地址
  408. * 返回: 构建数据的字节总数
  409. ***************************************
  410. */
  411. int jt808_Build_0x70_Sealing(uint8_t *pBuf)
  412. {
  413. int idx=0;
  414. int i = 0;
  415. pBuf[idx++] = 0x70; //类型
  416. for (i = 0; i < StoreNumber; i++)
  417. {
  418. //pBuf[idx++] = 1; //每仓数量
  419. //Sealing_Data.Sealing_Mode[i] :仓状态与油品:
  420. //1仓铅封状态/装油模式:高4位值1: 施封状态,值2:解封状态,值3:破封状态,值4:施封并输入密码超限,值5:非法破封;
  421. // 低4位(装油情况下)值1:上装,值2:下装;
  422. pBuf[idx++] = (Sealing_Data.Sealing_Mode[i] &0xf0)>> 4;
  423. }
  424. return idx;
  425. }
  426. /**
  427. ***************************************
  428. * 构建01 流水号 4字节 整数
  429. * 输入:pBuf-构建数据的存放首地址
  430. * 返回: 构建数据的字节总数
  431. ***************************************
  432. */
  433. int jt808_Build_0x01_flowID(uint8_t *pBuf,uint32_t flow_id)
  434. {
  435. int idx=0;
  436. // 2022-8-25, 新增加的子命令
  437. // 01, 流水号
  438. // 02, 时间戳
  439. do {
  440. pBuf[idx++] = 0x01;
  441. pBuf[idx++] = (flow_id>>24) & 0xff;
  442. pBuf[idx++] = (flow_id>>16) & 0xff;
  443. pBuf[idx++] = (flow_id>>8) & 0xff;
  444. pBuf[idx++] = (flow_id>>0) & 0xff;
  445. }while(0);
  446. return idx;
  447. }
  448. /**
  449. ***************************************
  450. * 构建20命令数据 时间戳 -- hex制式
  451. * 输入:pBuf-构建数据的存放首地址
  452. * 返回: 构建数据的字节总数
  453. ***************************************
  454. */
  455. extern SDateTime m_datetime;
  456. int jt808_Build_0x02_TimeCmd(uint8_t *pBuf)
  457. {
  458. int send_pos=0;
  459. //02 时间戳 6字节 字符串 BCD[6] YY-MM-DD-hh-mm-ss(GMT+8 时间,本标准中之后涉及的时间均采用此时区)
  460. pBuf[send_pos++] = 0x02;
  461. pBuf[send_pos++] = HEXtoBCD(m_datetime.year);
  462. pBuf[send_pos++] = HEXtoBCD(m_datetime.month);
  463. pBuf[send_pos++] = HEXtoBCD(m_datetime.day);
  464. pBuf[send_pos++] = HEXtoBCD(m_datetime.hour);
  465. pBuf[send_pos++] = HEXtoBCD(m_datetime.min);
  466. pBuf[send_pos++] = HEXtoBCD(m_datetime.sec);
  467. return send_pos;
  468. }
  469. /**
  470. ***************************************
  471. * 构建40命令数据 电子铅封 -- hex制式
  472. * 输入:pBuf-构建数据的存放首地址
  473. * 返回: 构建数据的字节总数
  474. ***************************************
  475. */
  476. int jt808_BuildSealTxCmd(uint8_t *pBuf, uint8_t *pEmergencyflag) // 40命令
  477. {
  478. Return_data *ret;
  479. int n = 0;
  480. uint8_t emergencyflag = 0;
  481. *pBuf++ = 0x40;
  482. ret = Seal_Tx_Readvalue(0,0);
  483. *pBuf++ = ret->length/2; // 调用返回的数据是asii制式的,所以真实的hex数是其一半
  484. for(n=0; n < ret->length/2 && n < 100; n++) {
  485. pBuf[n] = MODBUS_ASCII_AsciiToHex(ret->data + 2*n);
  486. }
  487. emergencyflag = pBuf[0];
  488. if(pEmergencyflag) *pEmergencyflag = emergencyflag;
  489. return n+2;
  490. }
  491. /**
  492. ***************************************
  493. * 构建91命令数据 IO状态 -- hex制式
  494. * 输入:pBuf-构建数据的存放首地址
  495. * 返回: 构建数据的字节总数
  496. ***************************************
  497. */
  498. int jt808_Build91Cmd(uint8_t *pBuf, uint8_t *pEmergencyflag)
  499. {
  500. Return_data *ret;
  501. int n = 0;
  502. uint8_t emergencyflag = 0;
  503. *pBuf++ = 0x91;
  504. ret = IO_Tx_Readvalue(0,0);
  505. *pBuf++ = ret->length/2; // 调用返回的数据是asii制式的,所以真实的hex数是其一半
  506. for(n=0; n < ret->length/2 && n < 100; n++) {
  507. pBuf[n] = MODBUS_ASCII_AsciiToHex(ret->data + 2*n);
  508. }
  509. emergencyflag = pBuf[0];
  510. if(pEmergencyflag) *pEmergencyflag = emergencyflag;
  511. return n+2;
  512. }
  513. /**
  514. ***************************************
  515. * 检测当前连接状态
  516. * 输入:无
  517. * 返回: 无
  518. ***************************************
  519. */
  520. void jt808_link_check(void)
  521. {
  522. if(jt808_obj.back_cnt < (Pass41SensorScanParam.nstep*Pass41SensorScanParam.step))
  523. {
  524. jt808_obj.link_cnt++;
  525. }
  526. else
  527. {
  528. jt808_obj.link_cnt = 0;
  529. }
  530. if(jt808_obj.link_cnt > 5)
  531. {
  532. jt808_obj.link_ok = true;
  533. }
  534. else
  535. {
  536. jt808_obj.link_ok = false;
  537. }
  538. return;
  539. }
  540. /**
  541. ***************************************
  542. * 将缓冲区数据帧内容,放入串口发送数据缓冲区
  543. * 输入:无
  544. * 返回: 无
  545. ***************************************
  546. */
  547. void jt808_send_buf_data(void)
  548. {
  549. if(jt808_obj.send_data.lenth == 0)
  550. {
  551. return;
  552. }
  553. //将缓冲区数据帧内容,放入串口发送数据缓冲区
  554. memcpy(USART1_TX_BUF,jt808_obj.send_data.buf,jt808_obj.send_data.lenth);
  555. //发送次数进行累加
  556. jt808_obj.send_data.send_cnt++;
  557. //通过串口将数据发送出去
  558. TerminalSlave485_Send_Data(USART1_TX_BUF,jt808_obj.send_data.lenth);
  559. return;
  560. }
  561. /**
  562. ***************************************
  563. * 处理接收到的返回数据帧
  564. * 输入:无
  565. * 返回: 无
  566. ***************************************
  567. */
  568. JT808_DataBack_DATA jt808_data_back = {0};
  569. void jt808_recv_func_DataBack(uint8_t * buf ,uint8_t len)
  570. {
  571. uint32_t read_len = 0;
  572. jt808_obj.back_cnt = 0;
  573. memcpy(&jt808_data_back, buf ,sizeof(jt808_data_back));
  574. mem_swap((uint8_t *)&(jt808_data_back.flowID),sizeof(jt808_data_back.flowID));
  575. if(jt808_data_back.flowID == jt808_obj.send_data.flowID)
  576. {
  577. //清除发送缓冲区数据
  578. memset((void *)&(jt808_obj.send_data), 0x00, sizeof(jt808_obj.send_data));
  579. //从内存数据栈中读取缓冲数据
  580. read_len = func_ram_record_delete((void *)&(jt808_obj.send_data), DATA_UNIT_SIZE);
  581. if( read_len != DATA_UNIT_SIZE)
  582. {
  583. memset((void *)&(jt808_obj.send_data), 0x00, sizeof(jt808_obj.send_data));
  584. }
  585. //如果本帧数据重发次数大于5,则清除本帧数据
  586. if(jt808_obj.send_data.send_cnt > 5)
  587. {
  588. memset((void *)&(jt808_obj.send_data), 0x00, sizeof(jt808_obj.send_data));
  589. }
  590. }
  591. return;
  592. }
  593. /**
  594. ***************************************
  595. * 将发送缓冲区中的数据,放入RAM,并将当前
  596. * 产生的数据放入缓冲区
  597. * 输入:无
  598. * 返回: 无
  599. ***************************************
  600. */
  601. void jt808_save_send_data(uint8_t * buf ,uint16_t len)
  602. {
  603. if(jt808_obj.send_data.flowID != 0)
  604. {
  605. func_ram_record_write((uint8_t *)&(jt808_obj.send_data) , DATA_UNIT_SIZE);
  606. memset((void *)&(jt808_obj.send_data), 0x00, sizeof(jt808_obj.send_data));
  607. }
  608. jt808_obj.send_data.flowID = jt808_obj.flowID;
  609. jt808_obj.send_data.lenth = len;
  610. if(len > sizeof(jt808_obj.send_data.buf))
  611. {
  612. len = sizeof(jt808_obj.send_data.buf);
  613. }
  614. memset(jt808_obj.send_data.buf, 0x00, sizeof(jt808_obj.send_data.buf));
  615. memcpy(jt808_obj.send_data.buf, buf, len);
  616. return;
  617. }
  618. /**
  619. ***************************************
  620. * xy,新透传,41串口透传,模拟F3轮询传感器
  621. * 按照周期1构建数据,按照周期2发送数据
  622. * 输入:无
  623. * 返回: 无
  624. * 发送区的格式为:
  625. * 数据负载类别,1Byte
  626. * 传感器命令数据包数,1Byte
  627. * 具体的各个命令数据包,NByte
  628. ***************************************
  629. */
  630. void jt808_DoInternalSensorScanAndPost(void)
  631. {
  632. int xlen=0, ylen=0;
  633. uint8_t emergencyflag=0;
  634. uint8_t *pSendBuf = USART1_TX_BUF;
  635. static int step = 0;
  636. static int nstep = 0;
  637. if(++step % Pass41SensorScanParam.step == 0)
  638. {
  639. for(int n=0; n<Pass41SensorScanParam.cmd_num; n++) {
  640. switch (Pass41SensorScanParam.cmd_arr[n]){
  641. case 0x40:
  642. xlen = jt808_BuildSealTxCmd(pSendBuf, emergencyflag?NULL:&emergencyflag);
  643. pSendBuf += xlen;
  644. ylen += xlen;
  645. break;
  646. case 0x91:
  647. xlen = jt808_Build91Cmd(pSendBuf, emergencyflag?NULL:&emergencyflag);
  648. pSendBuf += xlen;
  649. ylen += xlen;
  650. break;
  651. case 0xA1:
  652. xlen = jt808_Build_A1_Cmd(pSendBuf, jt808_obj.flowID, emergencyflag?NULL:&emergencyflag);
  653. pSendBuf += xlen;
  654. ylen += xlen;
  655. break;
  656. default:
  657. break;
  658. }
  659. }
  660. if(emergencyflag)
  661. {
  662. TerminalSlave485_Send_Data(USART1_TX_BUF, ylen);
  663. //保存发送数据
  664. jt808_save_send_data(USART1_TX_BUF, ylen);
  665. nstep = 0;
  666. emergencyflag = 0;
  667. jt808_obj.flowID++;
  668. }
  669. else
  670. {
  671. if(++nstep % Pass41SensorScanParam.nstep == 0)
  672. {
  673. TerminalSlave485_Send_Data(USART1_TX_BUF,ylen);
  674. //保存发送数据
  675. jt808_save_send_data(USART1_TX_BUF,ylen);
  676. //检测当前连接状态,每一大循环执行一次
  677. jt808_link_check();
  678. jt808_obj.flowID++;
  679. }
  680. else if(nstep % Pass41SensorScanParam.nstep == (Pass41SensorScanParam.nstep/2))
  681. {
  682. //重发缓冲区数据
  683. if(jt808_obj.link_ok == true) //如果连接正常,则重发数据;
  684. {
  685. jt808_send_buf_data();
  686. }
  687. }
  688. }
  689. }
  690. jt808_obj.back_cnt++;
  691. return;
  692. }
  693. /**
  694. ***************************************
  695. * 初始化发送配置参数
  696. * 输入:无
  697. * 返回: 无
  698. ***************************************
  699. */
  700. void jt808_InitSensorScanParam(void)
  701. {
  702. uint8_t data[16];
  703. //初始化补传缓冲区内存
  704. func_ram_record_init();
  705. FM25L16B_Read_N_Bytes(ADDR_Sensor_Scan_Param, data, 16);
  706. if(data[0] == 0x5A){
  707. uint8_t *buf = data+1;
  708. Pass41SensorScanParam.enable = 1;
  709. if(buf[0] > 1 && buf[1] > 0){
  710. Pass41SensorScanParam.step = buf[0];
  711. Pass41SensorScanParam.nstep = buf[1];
  712. if(buf[2] > 0 && buf[2] <= 8){
  713. Pass41SensorScanParam.cmd_num = buf[2];
  714. for (int n = 0; n < Pass41SensorScanParam.cmd_num; n++){
  715. Pass41SensorScanParam.cmd_arr[n] = buf[3+n];
  716. }
  717. }else{
  718. Pass41SensorScanParam.cmd_num = 2;
  719. Pass41SensorScanParam.cmd_arr[0] = 0x40;
  720. Pass41SensorScanParam.cmd_arr[1] = 0x91;
  721. }
  722. }else{
  723. Pass41SensorScanParam.step = 5;
  724. Pass41SensorScanParam.nstep = 6;
  725. Pass41SensorScanParam.cmd_num = 2;
  726. Pass41SensorScanParam.cmd_arr[0] = 0x40;
  727. Pass41SensorScanParam.cmd_arr[1] = 0x91;
  728. }
  729. }else{
  730. Pass41SensorScanParam.enable = 0;
  731. Pass41SensorScanParam.step = 5;
  732. Pass41SensorScanParam.nstep = 6;
  733. Pass41SensorScanParam.cmd_num = 2;
  734. Pass41SensorScanParam.cmd_arr[0] = 0x40;
  735. Pass41SensorScanParam.cmd_arr[1] = 0x91;
  736. }
  737. }
  738. #endif /*************APP_TERMINALSLAVE485_JT808_USE*******************/