TerminalSlave485_jt808.c 21 KB

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