tools.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "tools.h"
  2. #include "time.h"
  3. #include "includes.h"
  4. #define CRC16_INIT_VALUE 0x0000
  5. //CRC校验
  6. //CRC16-IBM
  7. uint16_t CRC16_get(uint8_t *_buff,uint32_t _len)
  8. {
  9. uint32_t i,j;
  10. uint16_t crc;
  11. uint16_t temp;
  12. crc=(uint16_t)CRC16_INIT_VALUE;
  13. for(i=0;i<_len;i++) {
  14. temp=_buff[i];
  15. temp &=0x00FF;
  16. crc^=temp;
  17. for(j=0;j<8;j++) {
  18. if((crc&0x0001)!=0x00) {
  19. crc>>=1;
  20. crc^=0xA001;
  21. } else {
  22. crc>>=1;
  23. }
  24. }
  25. }
  26. return crc;
  27. }
  28. //报税口明文校验
  29. uint16_t _crc_get(uint8_t *data, uint8_t size)
  30. {
  31. uint8_t i, crc = 0;
  32. for(i = 0;i < size;i++){
  33. crc ^=data[i];
  34. }
  35. return crc;
  36. }
  37. //转义
  38. uint8_t buff_code[2048];
  39. void msg_data_code(uint8_t *buff, uint16_t *len, uint8_t head, uint8_t tail)
  40. {
  41. // uint8_t buff_code[2048];
  42. uint16_t i, tlen = 0;
  43. /* 帧中除了帧头为 FE 外,不再出现 FE,如果出现 FE 则转义为
  44. FD 01;如果出现 FD 则转义为 FD 00;
  45. 帧中除了帧尾为 04 外,不再出现 04,如果出现 04 则转义为
  46. 05 01;如果出现 05 则转义为 05 00;帧长度和校验码都按转义前
  47. 来计算。*/
  48. memset(buff_code,0,2048);
  49. for(i = 0; i < (*len); i++)
  50. {
  51. if(i>1 && i<(*len)-1)//不判断头和尾,头为0xFEFE,尾为0x04
  52. {
  53. if(buff[i] == (head-1)){
  54. buff_code[tlen++] = head-1;
  55. buff_code[tlen++] = 0x00;
  56. } else if(buff[i] == head){
  57. buff_code[tlen++] = head-1;
  58. buff_code[tlen++] = 0x01;
  59. } else if(buff[i] == (tail+1)){
  60. buff_code[tlen++] = tail+1;
  61. buff_code[tlen++] = 0x00;
  62. } else if(buff[i] == tail){
  63. buff_code[tlen++] = tail+1;
  64. buff_code[tlen++] = 0x01;
  65. }
  66. else{
  67. buff_code[tlen++] = buff[i];
  68. }
  69. }
  70. else{
  71. buff_code[tlen++] = buff[i];
  72. }
  73. }
  74. memcpy(buff,buff_code,tlen);
  75. *len = tlen;
  76. }
  77. //反转义
  78. void msg_data_decode(uint8_t *buff, uint16_t *len, uint8_t head, uint8_t tail)
  79. {
  80. // uint8_t buff_code[2048];
  81. uint16_t i, tlen = 0;
  82. /* 帧中除了帧头为 FE 外,不再出现 FE,如果出现 FE 则转义为
  83. FF 01;如果出现 FF 则转义为 FF 00;
  84. 帧中除了帧尾为 04 外,不再出现 04,如果出现 04 则转义为
  85. 05 01;如果出现 05 则转义为 05 00;帧长度和校验码都按转义前
  86. 来计算。*/
  87. memset(buff_code,0,2048);
  88. for(i = 0;i < *len; i ++){
  89. if(i>1 && i<(*len)-1)//不判断头和尾,头为0xFEFE,尾为0x04
  90. {
  91. if((buff[i] == (head-1))&&(buff[i+1] == 0x00)) {
  92. buff_code[tlen++] = head-1;
  93. i += 1;
  94. } else if((buff[i] == (head-1))&&(buff[i+1] == 0x01)){
  95. buff_code[tlen++] = head;
  96. i += 1;
  97. } else if((buff[i] == (tail+1))&&(buff[i+1] == 0x00)) {
  98. buff_code[tlen++] = tail+1;
  99. i += 1;
  100. } else if((buff[i] == (tail+1))&&(buff[i+1] == 0x01)){
  101. buff_code[tlen++] = tail;
  102. i += 1;
  103. }
  104. else{
  105. buff_code[tlen++] = buff[i];
  106. }
  107. }
  108. else{
  109. buff_code[tlen++] = buff[i];
  110. }
  111. }
  112. memcpy(buff, buff_code, tlen);
  113. *len = tlen;
  114. }
  115. //检测指定长度字符串中是否包含指定字节
  116. // 如果在s中找到ch,则返回第一个ch的索引,否则返回-1
  117. int HasByte_head(uint8_t *s, char ch , uint16_t len)
  118. {
  119. int i;
  120. for(i = 0; i < len; ++i)
  121. if(s[i] == ch) return i;
  122. return -1;
  123. }
  124. //检测指定长度字符串中是否包含指定字节
  125. // 如果在s中找到ch,则返回最后一个ch的索引,否则返回-1
  126. int HasByte_tail(uint8_t *s, char ch , uint16_t len)
  127. {
  128. int i;
  129. int ret = -1;
  130. for(i = 0; i < len; ++i)
  131. {
  132. if(s[i] == ch)
  133. ret = i;
  134. }
  135. return ret;
  136. }
  137. unsigned char HexToChar(unsigned char bChar)
  138. {
  139. if((bChar>=0x30)&&(bChar<=0x39))
  140. {
  141. bChar -= 0x30;
  142. }
  143. else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
  144. {
  145. bChar -= 0x37;
  146. }
  147. else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
  148. {
  149. bChar -= 0x57;
  150. }
  151. else
  152. {
  153. bChar = 0xff;
  154. }
  155. return bChar;
  156. }
  157. //ascii转十六进制
  158. int asciitohex(char *data, uint8_t *out_data, int len)
  159. {
  160. int i,slen=0;
  161. uint8_t temp1,temp2;
  162. for(i = 0; i < len; i+=2)
  163. {
  164. temp1 = HexToChar(data[i]);
  165. temp2 = HexToChar(data[i+1]);
  166. out_data[slen] = (temp1<<4) | temp2;
  167. slen++;
  168. }
  169. return slen;
  170. }
  171. //十进制转BCD码
  172. int decimal_bcd_code(int decimal)
  173. {
  174. int sum = 0; //sum返回的BCD码
  175. int i;
  176. for (i = 0; decimal > 0; i++)
  177. {
  178. sum |= ((decimal % 10 ) << ( 4*i));
  179. decimal /= 10;
  180. }
  181. return sum;
  182. }
  183. //设置标记高位取反
  184. //将一个数设置为高八位是第八位的取反
  185. uint16_t set_flag_not(uint16_t data)
  186. {
  187. uint16_t temp_data,ret_data = 0;
  188. uint16_t temp_h,temp_l;
  189. temp_data = data;
  190. temp_h = ((~temp_data)<<8)&0xff00;
  191. temp_l = temp_data&0x00ff;
  192. ret_data = temp_h | temp_l;
  193. return ret_data;
  194. }
  195. //标记高位取反判断
  196. uint8_t flag_not_cmp(uint16_t data)
  197. {
  198. uint8_t ret = 0;
  199. uint16_t temp_data;
  200. uint8_t temp_h,temp_l;
  201. temp_data = data;
  202. temp_h = (uint8_t)((temp_data&0xff00)>>8);
  203. temp_l = (uint8_t)(temp_data&0x00ff);
  204. if((temp_h^temp_l) == 0xff)
  205. {
  206. ret = 1;
  207. }
  208. return ret;
  209. }
  210. /**************************************************************************
  211. 以下定时器均由滴答定时器驱动,systickCount
  212. ***************************************************************************/
  213. /**
  214. * @brief 设置定时器
  215. * @par param[timeout_t] *tt
  216. * @par param[uint32_t] val,延时ms
  217. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  218. */
  219. void timeout_setValue(timeout_t *tt,uint32_t val,uint8_t flg){
  220. tt->flag = flg;
  221. tt->count = TickCounter;
  222. tt->timeout = val;
  223. };
  224. /**
  225. * @brief 启动定时器
  226. * @par param[timeout_t] *tt
  227. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  228. */
  229. /**
  230. * @brief 启动定时器
  231. * @param tt 定时器指针
  232. * @param flg 是否开启
  233. */
  234. void timeout_start(timeout_t *tt,uint8_t flg){
  235. tt->flag = flg;
  236. if(flg){
  237. tt->count = TickCounter;
  238. }
  239. };
  240. /**
  241. * @brief 停止定时器
  242. * @param tt 定时器指针
  243. */
  244. void timeout_stop(timeout_t *tt){
  245. tt->flag = 0;
  246. };
  247. /**
  248. * @brief 返回定时器是否超时
  249. * @param tt 定时器指针
  250. * @return uint8_t 1:超时,0:未超时
  251. */
  252. uint8_t timeout_isOut(timeout_t *tt){
  253. if(tt->flag){
  254. if((TickCounter - tt->count) >= tt->timeout){
  255. tt->count = TickCounter;
  256. if(tt->flag == 1){
  257. tt->flag = 0;
  258. }
  259. return 1;
  260. }
  261. else{
  262. return 0;
  263. }
  264. }
  265. else{
  266. return 0;
  267. }
  268. };
  269. /**************************************************************************
  270. 以上定时器均由滴答定时器驱动,systickCount
  271. ***************************************************************************/
  272. /**************************************************************************
  273. 以下定时器均由timer定时器驱动,Tickcount_us
  274. ***************************************************************************/
  275. uint32_t Get_Time(void)
  276. {
  277. return 0;
  278. }
  279. /**
  280. * @brief 设置定时器
  281. * @par param[timeout_t] *tt
  282. * @par param[uint32_t] val,延时10us
  283. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  284. */
  285. void timeout_setValue_us(timeout_t *tt,uint32_t val,uint8_t flg){
  286. tt->flag = flg;
  287. tt->count = Get_Time();
  288. tt->timeout = val;
  289. };
  290. /**
  291. * @brief 启动定时器
  292. * @par param[timeout_t] *tt
  293. * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
  294. */
  295. /**
  296. * @brief 启动定时器
  297. * @param tt 定时器指针
  298. * @param flg 是否开启
  299. */
  300. void timeout_start_us(timeout_t *tt,uint8_t flg){
  301. tt->flag = flg;
  302. if(flg){
  303. tt->count = Get_Time();
  304. }
  305. };
  306. /**
  307. * @brief 停止定时器
  308. * @param tt 定时器指针
  309. */
  310. void timeout_stop_us(timeout_t *tt){
  311. tt->flag = 0;
  312. };
  313. /**
  314. * @brief 返回定时器是否超时
  315. * @param tt 定时器指针
  316. * @return uint8_t 1:超时,0:未超时
  317. */
  318. uint8_t timeout_isOut_us(timeout_t *tt){
  319. if(tt->flag){
  320. if((Get_Time() - tt->count) > tt->timeout){
  321. tt->count = Get_Time();
  322. if(tt->flag == 1){
  323. tt->flag = 0;
  324. }
  325. return 1;
  326. }
  327. else{
  328. return 0;
  329. }
  330. }
  331. else{
  332. return 0;
  333. }
  334. };
  335. /**************************************************************************
  336. 以上定时器均由timer定时器驱动,Tickcount_us
  337. ***************************************************************************/
  338. //串口波特率判断
  339. //返回值:1 数据正确,2 数据错误
  340. uint8_t uart_baud_judge(uint32_t baud)
  341. {
  342. //波特率范围1200到460800
  343. if(baud >= 1200 && baud <= 460800)
  344. return 1;
  345. else
  346. return 0;
  347. }
  348. //判断数组是否全部为0
  349. //返回值:1 全为0,0 不全为0
  350. uint8_t buf_is_zero(uint8_t *buf, uint16_t size)
  351. {
  352. int i;
  353. for(i = 0; i < size; i++)
  354. {
  355. if(buf[i])
  356. break;
  357. }
  358. if(i == size)
  359. return 1;
  360. else
  361. return 0;
  362. }
  363. //printf打印函数
  364. void printf_debug(uint8_t cmd1, const char *fmt, ...)
  365. {
  366. va_list args; //定义一个va_list类型的变量,用来储存单个参数
  367. if(cmd1)
  368. {
  369. va_start(args, fmt); //使args指向可变参数的第一个参数
  370. vprintf(fmt, args); //必须用vprintf等带V的
  371. va_end(args); //结束可变参数的获取
  372. }
  373. }
  374. //十六进制打印
  375. void data_dump_debug(uint8_t cmd1, const char *name, uint8_t *data, uint16_t len)
  376. {
  377. if(cmd1)
  378. {
  379. data_dump(name, data, len);
  380. }
  381. }