can_board.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /********************************************************************
  2. 版权所有:
  3. 文件版本: V1.00
  4. 文件名称: can_board.c
  5. 生成日期: 2020年4月20日
  6. 作 者: oujie
  7. 使用范围:
  8. 功 能:通过协议与can子板通信处理程序(非硬件确定canid)
  9. 更新信息:
  10. 更新日志1:
  11. 修改者:
  12. 修改日期:
  13. 修改内容:
  14. 修改原因:
  15. *********************************************************************/
  16. /*------------------------------- 头文件 --------------------------------------
  17. */
  18. #include "head.h"
  19. #ifndef CAN_DISABLE
  20. #ifdef CAN_SLAVE_BOARD
  21. const CAN_BOARD_NAME can_board_name[] =
  22. {
  23. {BOARD_TYPE_METERING, "计量板"}};
  24. #define CAN_NAME_NUM (sizeof(can_board_name) / sizeof(can_board_name[0]))
  25. struct can_board_res can_board[CAN_BOARD_NUM];
  26. u8 can_board_enable;
  27. int get_line_string(char *src_buf, char *dst_buf, u16 len)
  28. {
  29. u16 i = 0;
  30. while (i < len)
  31. {
  32. if (strncmp(&src_buf[i], "\r\n", 2) == 0)
  33. return (i + 2);
  34. dst_buf[i] = src_buf[i];
  35. i++;
  36. }
  37. return -1;
  38. }
  39. // 读n行字符串
  40. int get_part_line(char *src_buf, char *dst_buf, u16 n, u16 line_max)
  41. {
  42. int len = 0;
  43. int i = 0;
  44. int total_len = 0;
  45. while (i < n)
  46. {
  47. len = get_line_string(src_buf + total_len, dst_buf + i * line_max, line_max);
  48. if (len == -1)
  49. break;
  50. total_len += len;
  51. i++;
  52. }
  53. return i;
  54. }
  55. void Hex2Str(const char *sSrc, char *sDest, int nSrcLen)
  56. {
  57. int i;
  58. char szTmp[3];
  59. for (i = 0; i < nSrcLen; i++)
  60. {
  61. sprintf(szTmp, "%02X", (unsigned char)sSrc[i]);
  62. memcpy(&sDest[i * 2], szTmp, 2);
  63. }
  64. return;
  65. }
  66. // 十六进制字符串转换为字节流
  67. int HexStr2Bytes(const char *pSrc, unsigned char *pDst, int nSrcLength)
  68. {
  69. int i;
  70. for (i = 0; i < nSrcLength; i += 2)
  71. {
  72. // 输出高4位
  73. if ((*pSrc >= '0') && (*pSrc <= '9'))
  74. {
  75. *pDst = (*pSrc - '0') << 4;
  76. }
  77. else
  78. {
  79. *pDst = (*pSrc - 'A' + 10) << 4;
  80. }
  81. pSrc++;
  82. // 输出低4位
  83. if ((*pSrc >= '0') && (*pSrc <= '9'))
  84. {
  85. *pDst |= *pSrc - '0';
  86. }
  87. else
  88. {
  89. *pDst |= *pSrc - 'A' + 10;
  90. }
  91. pSrc++;
  92. // TRACE("%s",pSrc);
  93. pDst++;
  94. // TRACE("%s",pDst);
  95. }
  96. // 返回目标数据长度
  97. rt_printf("\r\n");
  98. return (nSrcLength / 2);
  99. }
  100. /******************************************************************************
  101. 函数名称: equ_init_can_file_res
  102. 函数版本: 01.00
  103. 创建作者: xxxxxx
  104. 创建日期: 2020-04-21
  105. 函数说明: 从can板卡资源文件装载can板卡资源。
  106. 参数说明:
  107. 返回值:
  108. 0: 成功
  109. 其它: 失败
  110. 修改记录:
  111. */
  112. u32 chipid[3] = {0};
  113. u32 chipid_hex[3] = {0};
  114. int equ_init_can_file_res(void)
  115. {
  116. u32 len;
  117. u8 *buf;
  118. char *ppos1;
  119. char *ppos2;
  120. // u8 line_buf[128] = {0};
  121. u8 dst_buf[CAN_BOARD_NUM + 1][128] = {{0}};
  122. u8 num, str_len, line_num;
  123. u8 i = 0;
  124. #ifdef CAN_BOARD_DEBUG
  125. u8 j = 0;
  126. #endif
  127. int pfile;
  128. loff_t pos;
  129. int slot;
  130. u8 id_len;
  131. // 打开文件
  132. pfile = rt_file_open("/app/data/can_brd.csv", O_RDONLY, 0);
  133. if (IS_ERR(pfile))
  134. {
  135. rt_printf("未找到can_brd.csv文件\r\n");
  136. return -1;
  137. }
  138. // 得到文件长度
  139. len = rt_file_getfile_size(pfile);
  140. // 分配内存
  141. buf = rt_malloc(len);
  142. if ((buf) == NULL)
  143. {
  144. rt_file_close(pfile, 0);
  145. return -2;
  146. }
  147. // 读出内容
  148. pos = 0;
  149. if (rt_file_read(pfile, buf, len, &pos) != len)
  150. {
  151. rt_file_close(pfile, 0);
  152. rt_free(buf);
  153. return -3;
  154. }
  155. // 关闭文件
  156. rt_file_close(pfile, 0);
  157. #ifdef CAN_BOARD_DEBUG
  158. rt_printf("读can_brd.csv\r\n");
  159. #endif
  160. num = get_part_line(buf, (char *)dst_buf, CAN_BOARD_NUM + 1, sizeof(dst_buf[0]));
  161. line_num = (num > CAN_BOARD_NUM) ? CAN_BOARD_NUM : num;
  162. #ifdef CAN_BOARD_DEBUG
  163. rt_printf("行数=%d\r\n", num);
  164. rt_printf("用于匹配行数=%d\r\n", line_num);
  165. #endif
  166. #if 1
  167. for (i = 1; i < line_num; i++) // i=1,从第二行获取
  168. {
  169. #ifdef CAN_BOARD_DEBUG
  170. rt_printf("%s\r\n", &dst_buf[i][0]);
  171. #endif
  172. str_len = strlen((char *)&dst_buf[i][0]);
  173. if (strncmp(&dst_buf[i][0], "ID", 2) == 0)
  174. {
  175. #ifdef CAN_BOARD_DEBUG
  176. rt_printf("找到匹配行=%d\r\n", i);
  177. #endif
  178. ppos1 = strchr((char *)&dst_buf[i][0], ',');
  179. if (ppos1)
  180. {
  181. ppos1++;
  182. ppos2 = strchr(ppos1, ',');
  183. if (ppos2)
  184. {
  185. id_len = (u32)ppos2 - (u32)ppos1;
  186. #ifdef CAN_BOARD_DEBUG
  187. rt_printf("id_len=%d\r\n", id_len);
  188. #endif
  189. if (id_len > (sizeof(can_board[i - 1].chip_id) * 2))
  190. {
  191. rt_printf("第%d行 ID号过长,不能超过32个字符串\r\n", i);
  192. continue;
  193. }
  194. memset(can_board[i - 1].chip_id, 0, sizeof(can_board[i - 1].chip_id));
  195. HexStr2Bytes(ppos1, (u8 *)can_board[i - 1].chip_id, id_len);
  196. #ifdef CAN_BOARD_DEBUG
  197. for (j = 0; j < CHIP_ID_SIZE; j++)
  198. rt_printf("chip_id[%d]=%x\r\n", j, can_board[i - 1].chip_id[j]);
  199. #endif
  200. }
  201. else
  202. continue;
  203. }
  204. else
  205. continue;
  206. ppos2 = strchr(ppos1, ',');
  207. if (ppos2)
  208. {
  209. slot = atoi(ppos2 + 1);
  210. if (slot >= 0)
  211. {
  212. can_board[i - 1].slot = slot;
  213. can_board[i - 1].file_have = 1;
  214. can_board_enable = 1;
  215. #ifdef CAN_BOARD_DEBUG
  216. rt_printf("can_board[%d].slot=%d\r\n", i - 1, can_board[i - 1].slot);
  217. #endif
  218. }
  219. else
  220. rt_printf("文件板卡号有误\r\n");
  221. }
  222. else
  223. continue;
  224. }
  225. #if 0
  226. for(k=0; k<sizeof(can_board[0].chip_id); k++)
  227. rt_printf("chip_id[%d]=%d ", k, can_board[i-1].chip_id[k]);
  228. rt_printf("\r\n");
  229. rt_printf("can_board[%d].slot=%d\r\n", i-1, can_board[i-1].slot);
  230. #endif
  231. }
  232. #endif
  233. return 0;
  234. }
  235. /******************************************************************************
  236. 函数名称: equ_get_can_slot
  237. 函数版本: 01.00
  238. 创建作者: oujie
  239. 创建日期: 2020-08-08
  240. 函数说明: 通过子板的chipid查找插槽号。
  241. 参数说明:
  242. chip_id 子板id。
  243. id_size id大小
  244. 返回值: 插槽号
  245. 修改记录:
  246. */
  247. struct can_board_res *equ_get_can_slot(u8 give_slot, u8 type, u8 *chip_id, u8 id_size)
  248. {
  249. u32 i;
  250. for (i = 0; i < CAN_BOARD_NUM; i++)
  251. {
  252. if (can_board[i].file_have && ((give_slot != can_board[0].slot) || (give_slot == 0) || (give_slot == 0xff))) // 子板应答卡号为0,需要从发,防止子板复位
  253. {
  254. if (memcmp((u8 *)can_board[i].chip_id, chip_id, id_size) == 0)
  255. {
  256. rt_printf("和文件中的ID匹配\r\n");
  257. return &can_board[i];
  258. }
  259. }
  260. }
  261. return NULL;
  262. }
  263. u8 is_can_slot_valid(u8 i)
  264. {
  265. return can_board[i].given_slot;
  266. }
  267. u8 get_can_slot(u8 i)
  268. {
  269. return can_board[i].slot;
  270. }
  271. void can_id_printf(void)
  272. {
  273. u8 i;
  274. char buf[64] = {0};
  275. char *name = NULL;
  276. for (i = 0; i < CAN_BOARD_NUM; i++)
  277. {
  278. if (can_board[i].given_slot)
  279. {
  280. Hex2Str((const char *)can_board[i].chip_id, buf, CHIP_ID_SIZE * 4);
  281. name = get_can_name(can_board[i].type);
  282. if (name)
  283. rt_printf("\r\nCAN子板%d:名称 :%s\r\n", i + 1, name);
  284. else
  285. rt_printf("\r\nCAN子板%d:名称 :无\r\n", i + 1);
  286. rt_printf(" ID :%s\r\n", buf);
  287. rt_printf(" 板卡号 :%d\r\n", can_board[i].slot);
  288. }
  289. else
  290. rt_printf("CAN子板%d:无\r\n", i + 1);
  291. }
  292. }
  293. char *get_can_name(u8 type)
  294. {
  295. u8 i;
  296. for (i = 0; i < CAN_NAME_NUM; i++)
  297. {
  298. if (type == can_board_name[i].type)
  299. return can_board_name[i].name;
  300. }
  301. return NULL;
  302. }
  303. int get_can_type_from_slot(u8 slot)
  304. {
  305. u8 i;
  306. for (i = 0; i < CAN_BOARD_NUM; i++)
  307. {
  308. if (can_board[i].slot == slot)
  309. {
  310. if (can_board[i].type)
  311. {
  312. return can_board[i].type;
  313. }
  314. else
  315. {
  316. return -1;
  317. }
  318. }
  319. }
  320. return -1;
  321. }
  322. void can_brd_error_set(void)
  323. {
  324. u8 i;
  325. for (i = 0; i < CAN_BOARD_NUM; i++)
  326. {
  327. if (BOARD_TYPE_METERING == can_board[i].type)
  328. {
  329. if (can_board[i].slot)
  330. {
  331. #if defined CAN_SLAVE_BOARD && defined METERING_ENERGY
  332. if (soe_check(EV_CAN_METERING_ERR) == false)
  333. {
  334. soe_record_ev(EV_CAN_METERING_ERR, 1, can_board[i].slot, 0, 0);
  335. }
  336. #endif
  337. }
  338. }
  339. }
  340. }
  341. void can_brd_error_clear(u8 slot)
  342. {
  343. u8 type;
  344. type = get_can_type_from_slot(slot);
  345. if (BOARD_TYPE_METERING == type)
  346. {
  347. #if defined CAN_SLAVE_BOARD && defined METERING_ENERGY
  348. if (soe_check(EV_CAN_METERING_ERR) == true)
  349. {
  350. soe_record_ev(EV_CAN_METERING_ERR, 0, slot, 0, 0);
  351. }
  352. #endif
  353. }
  354. }
  355. #endif
  356. #endif
  357. /*------------------------------- 宏定义 --------------------------------------
  358. */