| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- /*
- * Change Logs:
- * Date Author Notes
- * 2020-08-21 chenbin small modbus the first version
- */
- #include "small_modbus_base.h"
- #include "small_modbus_utils.h"
- #include "stdint.h"
- #include "string.h"
- /* Table of CRC values for high-order byte */
- static const uint8_t table_crc_hi[] = {
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
- };
- /* Table of CRC values for low-order byte */
- static const uint8_t table_crc_lo[] = {
- 0x00, 0xC0, 0xC1, 0x01, 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, 0x75, 0xB5,
- 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
- 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
- 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
- 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
- 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
- 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
- 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
- 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
- };
- uint16_t modbus_crc16(uint8_t *buffer, uint16_t buffer_length)
- {
- uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
- uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
- unsigned int i; /* will index into CRC lookup */
- /* pass through message buffer */
- while (buffer_length--) {
- i = crc_hi ^ *buffer++; /* calculate the CRC */
- crc_hi = crc_lo ^ table_crc_hi[i];
- crc_lo = table_crc_lo[i];
- }
- return (crc_hi << 8 | crc_lo);
- }
- int modbus_check_addr_num(uint8_t function,uint16_t address,uint16_t num)
- {
- switch (function)
- {
- case MODBUS_FC_READ_HOLDING_COILS:
- case MODBUS_FC_READ_INPUTS_COILS:
- {
- if((0 < num)&&(num <= MODBUS_MAX_READ_BITS)) //读取到的线圈数量大于0且小于2000个
- {
- return 1;
- }
- }break;
- case MODBUS_FC_READ_HOLDING_REGISTERS:
- case MODBUS_FC_READ_INPUT_REGISTERS:
- {
- if((0 < num)&&(num <= MODBUS_MAX_READ_REGISTERS)) //读取到的寄存器数量大于0且小于125个
- {
- return 1;
- }
- }break;
- case MODBUS_FC_WRITE_MULTIPLE_COILS:
- {
- if((0 < num)&&(num <= MODBUS_MAX_WRITE_BITS)) //读取到的寄存器数量大于0且小于1968个
- {
- return 1;
- }
- }break;
- case MODBUS_FC_WRITE_MULTIPLE_REGISTERS:
- {
- if((0 < num)&&(num <= MODBUS_MAX_WRITE_REGISTERS)) //读取到的寄存器数量大于0且小于123个
- {
- return 1;
- }
- }break;
- }
- return 0;
- }
- void modbus_byte_copy(uint8_t *des,uint8_t *src,int num)
- {
- while(num--)
- {
- *des = *src;
- des++;
- src++;
- }
- }
- void modbus_coil_h2m(uint8_t *des,uint8_t *src,int coil_num)
- {
- int num = (coil_num / 8) + ((coil_num % 8) ? 1 : 0);
- do{
- *des = *src;
- des++;
- src++;
- }while(--num);
- }
- void modbus_coil_m2h(uint8_t *des,uint8_t *src,int coil_num)
- {
- int num = (coil_num / 8) + ((coil_num % 8) ? 1 : 0);
- do{
- *des = *src;
- des++;
- src++;
- }while(--num);
- }
- #define MODBUS_REG_SWAP(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
- /*
- host uint16_t >> modbus_reg
- */
- void modbus_reg_h2m(void *dest_modbus_reg,void *source_host,int reg_num)
- {
- uint16_t *source_host_u16 = source_host;
- uint16_t *dest_modbus_u16 = dest_modbus_reg;
- uint16_t temp = 0;
-
- do{
- temp = *source_host_u16;
- *dest_modbus_u16 = MODBUS_REG_SWAP(temp);
- source_host_u16++;
- dest_modbus_u16++;
- }while(--reg_num);
- }
- /*
- modbus_reg >> host uint16_t
- */
- void modbus_reg_m2h(void *dest_host,void *source_modbus_reg,int reg_num)
- {
- uint16_t *source_modbus_u16 = source_modbus_reg;
- uint16_t *dest_host_u16 = dest_host;
- uint16_t temp = 0;
-
- do{
- temp = *source_modbus_u16;
- *dest_host_u16 = MODBUS_REG_SWAP(temp);
- source_modbus_u16++;
- dest_host_u16++;
- }while(--reg_num);
- }
- int dio_get_val(uint8_t *array,uint16_t index)
- {
- uint8_t offset_bit = (index & 0x07); //(index%8); //
- uint8_t offset_arr = (index >> 0x03); //(index/8); //
- return (array[offset_arr] & (0x01 << offset_bit))?1:0;
- }
- void dio_set_val(uint8_t *array,uint16_t index,int status)
- {
- uint8_t offset_bit = (index & 0x07); //(index%8); //
- uint8_t offset_arr = (index >> 0x03); //(index/8); //
- if(status)
- {
- array[offset_arr] |= (0x01 << offset_bit);
- }else
- {
- array[offset_arr] &= ~(0x01 << offset_bit);
- }
- }
- int aio_get_val(uint16_t *array,uint16_t index)
- {
- return array[index];
- }
- void aio_set_val(uint8_t *array,uint16_t index,int status)
- {
- array[index] = status;
- }
- static inline uint16_t bswap_16(uint16_t x)
- {
- return (x >> 8) | (x << 8);
- }
- static inline uint32_t bswap_32(uint32_t x)
- {
- return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16));
- }
- #define modbus_ntohl(x) (x)
- #define modbus_htonl(x) (x)
- /* Sets many bits from a single byte value (all 8 bits of the byte value are
- set) */
- void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value)
- {
- int i;
- for (i=0; i < 8; i++) {
- dest[idx+i] = (value & (1 << i)) ? 1 : 0;
- }
- }
- /* Sets many bits from a table of bytes (only the bits between idx and
- idx + nb_bits are set) */
- void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,const uint8_t *tab_byte)
- {
- unsigned int i;
- int shift = 0;
- for (i = idx; i < idx + nb_bits; i++) {
- dest[i] = tab_byte[(i - idx) / 8] & (1 << shift) ? 1 : 0;
- /* gcc doesn't like: shift = (++shift) % 8; */
- shift++;
- shift %= 8;
- }
- }
- /* Gets the byte value from many bits.
- To obtain a full byte, set nb_bits to 8. */
- uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,unsigned int nb_bits)
- {
- unsigned int i;
- uint8_t value = 0;
- if (nb_bits > 8) {
- nb_bits = 8;
- }
- for (i=0; i < nb_bits; i++) {
- value |= (src[idx+i] << i);
- }
- return value;
- }
- /* Get a float from 4 bytes (Modbus) without any conversion (ABCD) */
- float modbus_get_float_abcd(const uint16_t *src)
- {
- float f;
- uint32_t i;
- i = modbus_ntohl(((uint32_t)src[0] << 16) + src[1]);
- memcpy(&f, &i, sizeof(float));
- return f;
- }
- /* Get a long from 4 bytes (Modbus) without any conversion (ABCD) */
- long modbus_get_long_abcd(const uint16_t *src)
- {
- long l;
- uint32_t i;
- i = modbus_ntohl(((uint32_t)src[0] << 16) + src[1]);
- memcpy(&l, &i, sizeof(long));
- return l;
- }
- /* Get a float from 4 bytes (Modbus) in inversed format (DCBA) */
- float modbus_get_float_dcba(const uint16_t *src)
- {
- float f;
- uint32_t i;
- i = modbus_ntohl(bswap_32((((uint32_t)src[0]) << 16) + src[1]));
- memcpy(&f, &i, sizeof(float));
- return f;
- }
- /* Get a long from 4 bytes (Modbus) in inversed format (DCBA) */
- long modbus_get_long_dcba(const uint16_t *src)
- {
- long l;
- uint32_t i;
- i = modbus_ntohl(bswap_32((((uint32_t)src[0]) << 16) + src[1]));
- memcpy(&l, &i, sizeof(long));
- return l;
- }
- /* Get a float from 4 bytes (Modbus) with swapped bytes (BADC) */
- float modbus_get_float_badc(const uint16_t *src)
- {
- float f;
- uint32_t i;
- i = modbus_ntohl((uint32_t)(bswap_16(src[0]) << 16) + bswap_16(src[1]));
- memcpy(&f, &i, sizeof(float));
- return f;
- }
- /* Get a long from 4 bytes (Modbus) with swapped bytes (BADC) */
- long modbus_get_long_badc(const uint16_t *src)
- {
- long l;
- uint32_t i;
- i = modbus_ntohl((uint32_t)(bswap_16(src[0]) << 16) + bswap_16(src[1]));
- memcpy(&l, &i, sizeof(long));
- return l;
- }
- /* Get a float from 4 bytes (Modbus) with swapped words (CDAB) */
- float modbus_get_float_cdab(const uint16_t *src)
- {
- float f;
- uint32_t i;
- i = modbus_ntohl((((uint32_t)src[1]) << 16) + src[0]);
- memcpy(&f, &i, sizeof(float));
- return f;
- }
- /* Get a long from 4 bytes (Modbus) with swapped words (CDAB) */
- long modbus_get_long_cdab(const uint16_t *src)
- {
- long l;
- uint32_t i;
- i = modbus_ntohl((((uint32_t)src[1]) << 16) + src[0]);
- memcpy(&l, &i, sizeof(long));
- return l;
- }
- /* DEPRECATED - Get a float from 4 bytes in sort of Modbus format */
- float modbus_get_float(const uint16_t *src)
- {
- float f;
- uint32_t i;
- i = (((uint32_t)src[1]) << 16) + src[0];
- memcpy(&f, &i, sizeof(float));
- return f;
- }
- /* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */
- void modbus_set_float_abcd(float f, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &f, sizeof(uint32_t));
- i = modbus_htonl(i);
- dest[0] = (uint16_t)(i >> 16);
- dest[1] = (uint16_t)i;
- }
- /* Set a long to 4 bytes for Modbus w/o any conversion (ABCD) */
- void modbus_set_long_abcd(long l, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &l, sizeof(uint32_t));
- i = modbus_htonl(i);
- dest[0] = (uint16_t)(i >> 16);
- dest[1] = (uint16_t)i;
- }
- /* Set a float to 4 bytes for Modbus with byte and word swap conversion (DCBA) */
- void modbus_set_float_dcba(float f, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &f, sizeof(uint32_t));
- i = bswap_32(modbus_htonl(i));
- dest[0] = (uint16_t)(i >> 16);
- dest[1] = (uint16_t)i;
- }
- /* Set a long to 4 bytes for Modbus with byte and word swap conversion (DCBA) */
- void modbus_set_long_dcba(long l, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &l, sizeof(uint32_t));
- i = bswap_32(modbus_htonl(i));
- dest[0] = (uint16_t)(i >> 16);
- dest[1] = (uint16_t)i;
- }
- /* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */
- void modbus_set_float_badc(float f, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &f, sizeof(uint32_t));
- i = modbus_htonl(i);
- dest[0] = (uint16_t)bswap_16(i >> 16);
- dest[1] = (uint16_t)bswap_16(i & 0xFFFF);
- }
- /* Set a long to 4 bytes for Modbus with byte swap conversion (BADC) */
- void modbus_set_long_badc(long l, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &l, sizeof(uint32_t));
- i = modbus_htonl(i);
- dest[0] = (uint16_t)bswap_16(i >> 16);
- dest[1] = (uint16_t)bswap_16(i & 0xFFFF);
- }
- /* Set a float to 4 bytes for Modbus with word swap conversion (CDAB) */
- void modbus_set_float_cdab(float f, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &f, sizeof(uint32_t));
- i = modbus_htonl(i);
- dest[0] = (uint16_t)i;
- dest[1] = (uint16_t)(i >> 16);
- }
- /* Set a long to 4 bytes for Modbus with word swap conversion (CDAB) */
- void modbus_set_long_cdab(long l, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &l, sizeof(uint32_t));
- i = modbus_htonl(i);
- dest[0] = (uint16_t)i;
- dest[1] = (uint16_t)(i >> 16);
- }
- /* DEPRECATED - Set a float to 4 bytes in a sort of Modbus format! */
- void modbus_set_float(float f, uint16_t *dest)
- {
- uint32_t i;
- memcpy(&i, &f, sizeof(uint32_t));
- dest[0] = (uint16_t)i;
- dest[1] = (uint16_t)(i >> 16);
- }
|