| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- #include "tools.h"
- #include "time.h"
- #include "includes.h"
- #define CRC16_INIT_VALUE 0x0000
- //CRC校验
- //CRC16-IBM
- uint16_t CRC16_get(uint8_t *_buff,uint32_t _len)
- {
- uint32_t i,j;
- uint16_t crc;
- uint16_t temp;
- crc=(uint16_t)CRC16_INIT_VALUE;
- for(i=0;i<_len;i++) {
- temp=_buff[i];
- temp &=0x00FF;
- crc^=temp;
- for(j=0;j<8;j++) {
- if((crc&0x0001)!=0x00) {
- crc>>=1;
- crc^=0xA001;
- } else {
- crc>>=1;
- }
- }
- }
- return crc;
- }
- //报税口明文校验
- uint16_t _crc_get(uint8_t *data, uint8_t size)
- {
- uint8_t i, crc = 0;
-
- for(i = 0;i < size;i++){
- crc ^=data[i];
- }
-
- return crc;
- }
- //转义
- uint8_t buff_code[2048];
- void msg_data_code(uint8_t *buff, uint16_t *len, uint8_t head, uint8_t tail)
- {
- // uint8_t buff_code[2048];
- uint16_t i, tlen = 0;
- /* 帧中除了帧头为 FE 外,不再出现 FE,如果出现 FE 则转义为
- FD 01;如果出现 FD 则转义为 FD 00;
- 帧中除了帧尾为 04 外,不再出现 04,如果出现 04 则转义为
- 05 01;如果出现 05 则转义为 05 00;帧长度和校验码都按转义前
- 来计算。*/
- memset(buff_code,0,2048);
- for(i = 0; i < (*len); i++)
- {
- if(i>1 && i<(*len)-1)//不判断头和尾,头为0xFEFE,尾为0x04
- {
- if(buff[i] == (head-1)){
- buff_code[tlen++] = head-1;
- buff_code[tlen++] = 0x00;
- } else if(buff[i] == head){
- buff_code[tlen++] = head-1;
- buff_code[tlen++] = 0x01;
- } else if(buff[i] == (tail+1)){
- buff_code[tlen++] = tail+1;
- buff_code[tlen++] = 0x00;
- } else if(buff[i] == tail){
- buff_code[tlen++] = tail+1;
- buff_code[tlen++] = 0x01;
- }
- else{
- buff_code[tlen++] = buff[i];
- }
- }
- else{
- buff_code[tlen++] = buff[i];
- }
- }
- memcpy(buff,buff_code,tlen);
- *len = tlen;
- }
- //反转义
- void msg_data_decode(uint8_t *buff, uint16_t *len, uint8_t head, uint8_t tail)
- {
- // uint8_t buff_code[2048];
- uint16_t i, tlen = 0;
- /* 帧中除了帧头为 FE 外,不再出现 FE,如果出现 FE 则转义为
- FF 01;如果出现 FF 则转义为 FF 00;
- 帧中除了帧尾为 04 外,不再出现 04,如果出现 04 则转义为
- 05 01;如果出现 05 则转义为 05 00;帧长度和校验码都按转义前
- 来计算。*/
- memset(buff_code,0,2048);
- for(i = 0;i < *len; i ++){
- if(i>1 && i<(*len)-1)//不判断头和尾,头为0xFEFE,尾为0x04
- {
- if((buff[i] == (head-1))&&(buff[i+1] == 0x00)) {
- buff_code[tlen++] = head-1;
- i += 1;
- } else if((buff[i] == (head-1))&&(buff[i+1] == 0x01)){
- buff_code[tlen++] = head;
- i += 1;
- } else if((buff[i] == (tail+1))&&(buff[i+1] == 0x00)) {
- buff_code[tlen++] = tail+1;
- i += 1;
- } else if((buff[i] == (tail+1))&&(buff[i+1] == 0x01)){
- buff_code[tlen++] = tail;
- i += 1;
- }
- else{
- buff_code[tlen++] = buff[i];
- }
- }
- else{
- buff_code[tlen++] = buff[i];
- }
- }
- memcpy(buff, buff_code, tlen);
- *len = tlen;
- }
- //检测指定长度字符串中是否包含指定字节
- // 如果在s中找到ch,则返回第一个ch的索引,否则返回-1
- int HasByte_head(uint8_t *s, char ch , uint16_t len)
- {
- int i;
- for(i = 0; i < len; ++i)
- if(s[i] == ch) return i;
- return -1;
- }
- //检测指定长度字符串中是否包含指定字节
- // 如果在s中找到ch,则返回最后一个ch的索引,否则返回-1
- int HasByte_tail(uint8_t *s, char ch , uint16_t len)
- {
- int i;
- int ret = -1;
- for(i = 0; i < len; ++i)
- {
- if(s[i] == ch)
- ret = i;
- }
- return ret;
- }
- unsigned char HexToChar(unsigned char bChar)
- {
- if((bChar>=0x30)&&(bChar<=0x39))
- {
- bChar -= 0x30;
- }
- else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
- {
- bChar -= 0x37;
- }
- else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
- {
- bChar -= 0x57;
- }
- else
- {
- bChar = 0xff;
- }
- return bChar;
- }
- //ascii转十六进制
- int asciitohex(char *data, uint8_t *out_data, int len)
- {
- int i,slen=0;
- uint8_t temp1,temp2;
- for(i = 0; i < len; i+=2)
- {
- temp1 = HexToChar(data[i]);
- temp2 = HexToChar(data[i+1]);
- out_data[slen] = (temp1<<4) | temp2;
- slen++;
- }
- return slen;
- }
- //十进制转BCD码
- int decimal_bcd_code(int decimal)
- {
- int sum = 0; //sum返回的BCD码
- int i;
- for (i = 0; decimal > 0; i++)
- {
- sum |= ((decimal % 10 ) << ( 4*i));
- decimal /= 10;
- }
- return sum;
- }
- //设置标记高位取反
- //将一个数设置为高八位是第八位的取反
- uint16_t set_flag_not(uint16_t data)
- {
- uint16_t temp_data,ret_data = 0;
- uint16_t temp_h,temp_l;
- temp_data = data;
- temp_h = ((~temp_data)<<8)&0xff00;
- temp_l = temp_data&0x00ff;
- ret_data = temp_h | temp_l;
- return ret_data;
- }
- //标记高位取反判断
- uint8_t flag_not_cmp(uint16_t data)
- {
- uint8_t ret = 0;
- uint16_t temp_data;
- uint8_t temp_h,temp_l;
- temp_data = data;
- temp_h = (uint8_t)((temp_data&0xff00)>>8);
- temp_l = (uint8_t)(temp_data&0x00ff);
- if((temp_h^temp_l) == 0xff)
- {
- ret = 1;
- }
- return ret;
- }
- /**************************************************************************
- 以下定时器均由滴答定时器驱动,systickCount
- ***************************************************************************/
- /**
- * @brief 设置定时器
- * @par param[timeout_t] *tt
- * @par param[uint32_t] val,延时ms
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- void timeout_setValue(timeout_t *tt,uint32_t val,uint8_t flg){
- tt->flag = flg;
- tt->count = TickCounter;
- tt->timeout = val;
- };
- /**
- * @brief 启动定时器
- * @par param[timeout_t] *tt
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- /**
- * @brief 启动定时器
- * @param tt 定时器指针
- * @param flg 是否开启
- */
- void timeout_start(timeout_t *tt,uint8_t flg){
- tt->flag = flg;
- if(flg){
- tt->count = TickCounter;
- }
- };
- /**
- * @brief 停止定时器
- * @param tt 定时器指针
- */
- void timeout_stop(timeout_t *tt){
- tt->flag = 0;
- };
- /**
- * @brief 返回定时器是否超时
- * @param tt 定时器指针
- * @return uint8_t 1:超时,0:未超时
- */
- uint8_t timeout_isOut(timeout_t *tt){
- if(tt->flag){
- if((TickCounter - tt->count) >= tt->timeout){
- tt->count = TickCounter;
- if(tt->flag == 1){
- tt->flag = 0;
- }
- return 1;
- }
- else{
- return 0;
- }
- }
- else{
- return 0;
- }
- };
- /**************************************************************************
- 以上定时器均由滴答定时器驱动,systickCount
- ***************************************************************************/
- /**************************************************************************
- 以下定时器均由timer定时器驱动,Tickcount_us
- ***************************************************************************/
- uint32_t Get_Time(void)
- {
- return 0;
- }
- /**
- * @brief 设置定时器
- * @par param[timeout_t] *tt
- * @par param[uint32_t] val,延时10us
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- void timeout_setValue_us(timeout_t *tt,uint32_t val,uint8_t flg){
- tt->flag = flg;
- tt->count = Get_Time();
- tt->timeout = val;
- };
- /**
- * @brief 启动定时器
- * @par param[timeout_t] *tt
- * @par param[uint8_t] flg。0:不使能;1单次使能;0xFF连续使能
- */
- /**
- * @brief 启动定时器
- * @param tt 定时器指针
- * @param flg 是否开启
- */
- void timeout_start_us(timeout_t *tt,uint8_t flg){
- tt->flag = flg;
- if(flg){
- tt->count = Get_Time();
- }
- };
- /**
- * @brief 停止定时器
- * @param tt 定时器指针
- */
- void timeout_stop_us(timeout_t *tt){
- tt->flag = 0;
- };
- /**
- * @brief 返回定时器是否超时
- * @param tt 定时器指针
- * @return uint8_t 1:超时,0:未超时
- */
- uint8_t timeout_isOut_us(timeout_t *tt){
- if(tt->flag){
- if((Get_Time() - tt->count) > tt->timeout){
- tt->count = Get_Time();
- if(tt->flag == 1){
- tt->flag = 0;
- }
- return 1;
- }
- else{
- return 0;
- }
- }
- else{
- return 0;
- }
- };
- /**************************************************************************
- 以上定时器均由timer定时器驱动,Tickcount_us
- ***************************************************************************/
- //串口波特率判断
- //返回值:1 数据正确,2 数据错误
- uint8_t uart_baud_judge(uint32_t baud)
- {
- //波特率范围1200到460800
- if(baud >= 1200 && baud <= 460800)
- return 1;
- else
- return 0;
-
- }
- //判断数组是否全部为0
- //返回值:1 全为0,0 不全为0
- uint8_t buf_is_zero(uint8_t *buf, uint16_t size)
- {
- int i;
- for(i = 0; i < size; i++)
- {
- if(buf[i])
- break;
- }
- if(i == size)
- return 1;
- else
- return 0;
- }
- //printf打印函数
- void printf_debug(uint8_t cmd1, const char *fmt, ...)
- {
- va_list args; //定义一个va_list类型的变量,用来储存单个参数
- if(cmd1)
- {
- va_start(args, fmt); //使args指向可变参数的第一个参数
- vprintf(fmt, args); //必须用vprintf等带V的
- va_end(args); //结束可变参数的获取
- }
- }
- //十六进制打印
- void data_dump_debug(uint8_t cmd1, const char *name, uint8_t *data, uint16_t len)
- {
- if(cmd1)
- {
- data_dump(name, data, len);
- }
- }
|