TerminalSlave485_jt808.c 21 KB

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