123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #include "dlt645_private.h"
- #include <string.h>
- #include "data_task.h"
- typedef enum
- {
- BYTE_RESET = 0,
- BYTE_LOW_ADDRESS,
- BYTE_HIGH_ADDRESS
- } byte_part;
- int _crc(uint8_t *msg, int len)
- {
- uint8_t crc = 0;
- while (len--)
- {
- crc += *msg++;
- }
- return crc;
- }
- int dlt645_common_check(uint8_t *msg, int len, uint8_t *addr)
- {
-
- if (len < 7)
- {
- return -1;
- }
-
- if (msg[0] != DL645_START_CODE ||
- msg[DL645_ADDR_LEN + 1] != DL645_START_CODE ||
- msg[len - 1] != DL645_STOP_CODE)
- {
- DLT645_LOG("check code error!\n");
- return -1;
- }
-
- uint8_t crc = _crc(msg, len - 2);
- if (crc != msg[len - 2])
- {
- DLT645_LOG("check crc error!\n");
- return -1;
- }
-
- if ((msg[DL645_CONTROL_POS] & C_TD_MASK) == (C_TD_MASTER << C_TD_POS))
- {
- DLT645_LOG("check control direction error!\n");
- return -1;
- }
-
- if ((msg[DL645_CONTROL_POS] & C_ACK_MASK) == (C_ACK_ERR << C_ACK_POS))
- {
- DLT645_LOG("check ACK error!\n");
- return msg[len - 3];
- }
-
- if (memcmp(msg + 1, addr, 6) != 0)
- {
- return -1;
- }
- return 0;
- }
- uint32_t dec_to_bcd(uint32_t val)
- {
- uint32_t data = 0;
- if (val < 100)
- {
- uint8_t byte0 = val % 10;
- uint8_t byte1 = val / 10;
- data = (byte1 << 4) + byte0;
- }
- else if (val < 10000)
- {
- uint8_t byte0 = val % 10;
- uint8_t byte1 = (val / 10) % 10;
- uint8_t byte2 = (val / 100) % 10;
- uint8_t byte3 = (val / 1000) % 10;
- data = (byte3 << 12) +
- (byte2 << 8) +
- (byte1 << 4) + byte0;
- }
- else if (val < 1000000)
- {
- uint8_t byte0 = val % 10;
- uint8_t byte1 = (val / 10) % 10;
- uint8_t byte2 = (val / 100) % 10;
- uint8_t byte3 = (val / 1000) % 10;
- uint8_t byte4 = (val / 10000) % 10;
- uint8_t byte5 = (val / 100000) % 10;
- data = (byte5 << 20) +
- (byte4 << 16) +
- (byte3 << 12) +
- (byte2 << 8) +
- (byte1 << 4) + byte0;
- }
- else if (val < 100000000)
- {
- uint8_t byte0 = val % 10;
- uint8_t byte1 = (val / 10) % 10;
- uint8_t byte2 = (val / 100) % 10;
- uint8_t byte3 = (val / 1000) % 10;
- uint8_t byte4 = (val / 10000) % 10;
- uint8_t byte5 = (val / 100000) % 10;
- uint8_t byte6 = (val / 1000000) % 10;
- uint8_t byte7 = (val / 10000000) % 10;
- data = (byte7 << 28) +
- (byte6 << 24) +
- (byte5 << 20) +
- (byte4 << 16) +
- (byte3 << 12) +
- (byte2 << 8) +
- (byte1 << 4) + byte0;
- }
- return data;
- }
- int str_to_bcd(char *str, uint8_t *bcd_store_address, uint16_t bcd_len)
- {
-
- int str_pos = bcd_len * 2 - strlen(str);
-
- if (str_pos < 0)
- {
- return -1;
- }
- memset(bcd_store_address, 0, bcd_len);
- for (int i = 0; i < strlen(str); i++)
- {
- if (str[i] >= '0' && str[i] <= '9')
- {
- bcd_store_address[(i + str_pos) / 2] |= (str[i] - '0') << (4 * ((i + 1 + (strlen(str) % 2)) % 2));
- }
- else
- {
-
- return -1;
- }
- }
- return 0;
- }
- int data_package_translate_to_int(uint8_t *read_data, uint16_t len)
- {
-
- uint8_t number_weight = 0;
-
- uint8_t current_index = 0;
-
- uint8_t current_byte_part = BYTE_RESET;
-
- int i_value = 0;
- while (len--)
- {
- current_byte_part = BYTE_LOW_ADDRESS;
- do
- {
- switch (current_byte_part)
- {
-
- case BYTE_LOW_ADDRESS:
- i_value += ((read_data[current_index] - 0x33) & 0x0f) * my_pow(10,number_weight);
- number_weight++;
- current_byte_part = BYTE_HIGH_ADDRESS;
- break;
-
- case BYTE_HIGH_ADDRESS:
- i_value += ((read_data[current_index] - 0x33) >> 4) * my_pow(10,number_weight);
- number_weight++;
- current_byte_part = BYTE_RESET;
- break;
- }
- } while (current_byte_part != BYTE_RESET);
- current_index++;
- }
- return i_value;
- }
- int dlt645_data_parse_by_format_to_float(uint8_t *read_data, uint16_t read_len, const char *data_format, uint8_t *store_address)
- {
-
- int num_weight = 0;
- int ival = data_package_translate_to_int(read_data, read_len);
- for (int i = 0; i < strlen(data_format); i++)
- {
- if (*(data_format + i) == '.')
- {
- num_weight = strlen(data_format) - i - 1;
- if (num_weight < 0)
- {
- return -1;
- }
- break;
- }
- }
- float fval = ival / my_pow(10,num_weight);
- memcpy(store_address, &fval, 4);
- return 0;
- }
- int dlt645_data_parse_by_format_to_float_and_DATE(uint8_t *read_data, uint16_t read_len, const char *data_format, uint8_t *store_address)
- {
- int num_weight = 0;
- int ival = data_package_translate_to_int(read_data, read_len);
- for (int i = 0; i < strlen(data_format); i++)
- {
- if (*(data_format + i) == '.')
- {
- num_weight = strlen(data_format) - i - 1;
- if (num_weight < 0)
- {
- return -1;
- }
- break;
- }
- }
- float fval = ival / my_pow(10,num_weight);
- memcpy(store_address, &fval, 4);
- return 0;
- }
|