small_modbus_utils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Change Logs:
  3. * Date Author Notes
  4. * 2020-08-21 chenbin small modbus the first version
  5. */
  6. #include "small_modbus_base.h"
  7. #include "small_modbus_utils.h"
  8. #include "stdint.h"
  9. #include "string.h"
  10. /* Table of CRC values for high-order byte */
  11. static const uint8_t table_crc_hi[] = {
  12. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  13. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  14. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  15. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  16. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  17. 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  18. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  19. 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  20. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  21. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  22. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  23. 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  24. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  25. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  26. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  27. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  28. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  29. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  30. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  31. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  32. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  33. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  34. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  35. 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  36. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  37. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
  38. };
  39. /* Table of CRC values for low-order byte */
  40. static const uint8_t table_crc_lo[] = {
  41. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
  42. 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
  43. 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
  44. 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
  45. 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
  46. 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  47. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
  48. 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
  49. 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
  50. 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
  51. 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
  52. 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  53. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
  54. 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
  55. 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
  56. 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
  57. 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
  58. 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  59. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
  60. 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
  61. 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
  62. 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
  63. 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
  64. 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  65. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
  66. 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
  67. };
  68. uint16_t modbus_crc16(uint8_t *buffer, uint16_t buffer_length)
  69. {
  70. uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
  71. uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
  72. unsigned int i; /* will index into CRC lookup */
  73. /* pass through message buffer */
  74. while (buffer_length--) {
  75. i = crc_hi ^ *buffer++; /* calculate the CRC */
  76. crc_hi = crc_lo ^ table_crc_hi[i];
  77. crc_lo = table_crc_lo[i];
  78. }
  79. return (crc_hi << 8 | crc_lo);
  80. }
  81. int modbus_check_addr_num(uint8_t function,uint16_t address,uint16_t num)
  82. {
  83. switch (function)
  84. {
  85. case MODBUS_FC_READ_HOLDING_COILS:
  86. case MODBUS_FC_READ_INPUTS_COILS:
  87. {
  88. if((0 < num)&&(num <= MODBUS_MAX_READ_BITS)) //读取到的线圈数量大于0且小于2000个
  89. {
  90. return 1;
  91. }
  92. }break;
  93. case MODBUS_FC_READ_HOLDING_REGISTERS:
  94. case MODBUS_FC_READ_INPUT_REGISTERS:
  95. {
  96. if((0 < num)&&(num <= MODBUS_MAX_READ_REGISTERS)) //读取到的寄存器数量大于0且小于125个
  97. {
  98. return 1;
  99. }
  100. }break;
  101. case MODBUS_FC_WRITE_MULTIPLE_COILS:
  102. {
  103. if((0 < num)&&(num <= MODBUS_MAX_WRITE_BITS)) //读取到的寄存器数量大于0且小于1968个
  104. {
  105. return 1;
  106. }
  107. }break;
  108. case MODBUS_FC_WRITE_MULTIPLE_REGISTERS:
  109. {
  110. if((0 < num)&&(num <= MODBUS_MAX_WRITE_REGISTERS)) //读取到的寄存器数量大于0且小于123个
  111. {
  112. return 1;
  113. }
  114. }break;
  115. }
  116. return 0;
  117. }
  118. void modbus_byte_copy(uint8_t *des,uint8_t *src,int num)
  119. {
  120. while(num--)
  121. {
  122. *des = *src;
  123. des++;
  124. src++;
  125. }
  126. }
  127. void modbus_coil_h2m(uint8_t *des,uint8_t *src,int coil_num)
  128. {
  129. int num = (coil_num / 8) + ((coil_num % 8) ? 1 : 0);
  130. do{
  131. *des = *src;
  132. des++;
  133. src++;
  134. }while(--num);
  135. }
  136. void modbus_coil_m2h(uint8_t *des,uint8_t *src,int coil_num)
  137. {
  138. int num = (coil_num / 8) + ((coil_num % 8) ? 1 : 0);
  139. do{
  140. *des = *src;
  141. des++;
  142. src++;
  143. }while(--num);
  144. }
  145. #define MODBUS_REG_SWAP(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
  146. /*
  147. host uint16_t >> modbus_reg
  148. */
  149. void modbus_reg_h2m(void *dest_modbus_reg,void *source_host,int reg_num)
  150. {
  151. uint16_t *source_host_u16 = source_host;
  152. uint16_t *dest_modbus_u16 = dest_modbus_reg;
  153. uint16_t temp = 0;
  154. do{
  155. temp = *source_host_u16;
  156. *dest_modbus_u16 = MODBUS_REG_SWAP(temp);
  157. source_host_u16++;
  158. dest_modbus_u16++;
  159. }while(--reg_num);
  160. }
  161. /*
  162. modbus_reg >> host uint16_t
  163. */
  164. void modbus_reg_m2h(void *dest_host,void *source_modbus_reg,int reg_num)
  165. {
  166. uint16_t *source_modbus_u16 = source_modbus_reg;
  167. uint16_t *dest_host_u16 = dest_host;
  168. uint16_t temp = 0;
  169. do{
  170. temp = *source_modbus_u16;
  171. *dest_host_u16 = MODBUS_REG_SWAP(temp);
  172. source_modbus_u16++;
  173. dest_host_u16++;
  174. }while(--reg_num);
  175. }
  176. int dio_get_val(uint8_t *array,uint16_t index)
  177. {
  178. uint8_t offset_bit = (index & 0x07); //(index%8); //
  179. uint8_t offset_arr = (index >> 0x03); //(index/8); //
  180. return (array[offset_arr] & (0x01 << offset_bit))?1:0;
  181. }
  182. void dio_set_val(uint8_t *array,uint16_t index,int status)
  183. {
  184. uint8_t offset_bit = (index & 0x07); //(index%8); //
  185. uint8_t offset_arr = (index >> 0x03); //(index/8); //
  186. if(status)
  187. {
  188. array[offset_arr] |= (0x01 << offset_bit);
  189. }else
  190. {
  191. array[offset_arr] &= ~(0x01 << offset_bit);
  192. }
  193. }
  194. int aio_get_val(uint16_t *array,uint16_t index)
  195. {
  196. return array[index];
  197. }
  198. void aio_set_val(uint8_t *array,uint16_t index,int status)
  199. {
  200. array[index] = status;
  201. }
  202. static inline uint16_t bswap_16(uint16_t x)
  203. {
  204. return (x >> 8) | (x << 8);
  205. }
  206. static inline uint32_t bswap_32(uint32_t x)
  207. {
  208. return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16));
  209. }
  210. #define modbus_ntohl(x) (x)
  211. #define modbus_htonl(x) (x)
  212. /* Sets many bits from a single byte value (all 8 bits of the byte value are
  213. set) */
  214. void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value)
  215. {
  216. int i;
  217. for (i=0; i < 8; i++) {
  218. dest[idx+i] = (value & (1 << i)) ? 1 : 0;
  219. }
  220. }
  221. /* Sets many bits from a table of bytes (only the bits between idx and
  222. idx + nb_bits are set) */
  223. void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,const uint8_t *tab_byte)
  224. {
  225. unsigned int i;
  226. int shift = 0;
  227. for (i = idx; i < idx + nb_bits; i++) {
  228. dest[i] = tab_byte[(i - idx) / 8] & (1 << shift) ? 1 : 0;
  229. /* gcc doesn't like: shift = (++shift) % 8; */
  230. shift++;
  231. shift %= 8;
  232. }
  233. }
  234. /* Gets the byte value from many bits.
  235. To obtain a full byte, set nb_bits to 8. */
  236. uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,unsigned int nb_bits)
  237. {
  238. unsigned int i;
  239. uint8_t value = 0;
  240. if (nb_bits > 8) {
  241. nb_bits = 8;
  242. }
  243. for (i=0; i < nb_bits; i++) {
  244. value |= (src[idx+i] << i);
  245. }
  246. return value;
  247. }
  248. /* Get a float from 4 bytes (Modbus) without any conversion (ABCD) */
  249. float modbus_get_float_abcd(const uint16_t *src)
  250. {
  251. float f;
  252. uint32_t i;
  253. i = modbus_ntohl(((uint32_t)src[0] << 16) + src[1]);
  254. memcpy(&f, &i, sizeof(float));
  255. return f;
  256. }
  257. /* Get a long from 4 bytes (Modbus) without any conversion (ABCD) */
  258. long modbus_get_long_abcd(const uint16_t *src)
  259. {
  260. long l;
  261. uint32_t i;
  262. i = modbus_ntohl(((uint32_t)src[0] << 16) + src[1]);
  263. memcpy(&l, &i, sizeof(long));
  264. return l;
  265. }
  266. /* Get a float from 4 bytes (Modbus) in inversed format (DCBA) */
  267. float modbus_get_float_dcba(const uint16_t *src)
  268. {
  269. float f;
  270. uint32_t i;
  271. i = modbus_ntohl(bswap_32((((uint32_t)src[0]) << 16) + src[1]));
  272. memcpy(&f, &i, sizeof(float));
  273. return f;
  274. }
  275. /* Get a long from 4 bytes (Modbus) in inversed format (DCBA) */
  276. long modbus_get_long_dcba(const uint16_t *src)
  277. {
  278. long l;
  279. uint32_t i;
  280. i = modbus_ntohl(bswap_32((((uint32_t)src[0]) << 16) + src[1]));
  281. memcpy(&l, &i, sizeof(long));
  282. return l;
  283. }
  284. /* Get a float from 4 bytes (Modbus) with swapped bytes (BADC) */
  285. float modbus_get_float_badc(const uint16_t *src)
  286. {
  287. float f;
  288. uint32_t i;
  289. i = modbus_ntohl((uint32_t)(bswap_16(src[0]) << 16) + bswap_16(src[1]));
  290. memcpy(&f, &i, sizeof(float));
  291. return f;
  292. }
  293. /* Get a long from 4 bytes (Modbus) with swapped bytes (BADC) */
  294. long modbus_get_long_badc(const uint16_t *src)
  295. {
  296. long l;
  297. uint32_t i;
  298. i = modbus_ntohl((uint32_t)(bswap_16(src[0]) << 16) + bswap_16(src[1]));
  299. memcpy(&l, &i, sizeof(long));
  300. return l;
  301. }
  302. /* Get a float from 4 bytes (Modbus) with swapped words (CDAB) */
  303. float modbus_get_float_cdab(const uint16_t *src)
  304. {
  305. float f;
  306. uint32_t i;
  307. i = modbus_ntohl((((uint32_t)src[1]) << 16) + src[0]);
  308. memcpy(&f, &i, sizeof(float));
  309. return f;
  310. }
  311. /* Get a long from 4 bytes (Modbus) with swapped words (CDAB) */
  312. long modbus_get_long_cdab(const uint16_t *src)
  313. {
  314. long l;
  315. uint32_t i;
  316. i = modbus_ntohl((((uint32_t)src[1]) << 16) + src[0]);
  317. memcpy(&l, &i, sizeof(long));
  318. return l;
  319. }
  320. /* DEPRECATED - Get a float from 4 bytes in sort of Modbus format */
  321. float modbus_get_float(const uint16_t *src)
  322. {
  323. float f;
  324. uint32_t i;
  325. i = (((uint32_t)src[1]) << 16) + src[0];
  326. memcpy(&f, &i, sizeof(float));
  327. return f;
  328. }
  329. /* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */
  330. void modbus_set_float_abcd(float f, uint16_t *dest)
  331. {
  332. uint32_t i;
  333. memcpy(&i, &f, sizeof(uint32_t));
  334. i = modbus_htonl(i);
  335. dest[0] = (uint16_t)(i >> 16);
  336. dest[1] = (uint16_t)i;
  337. }
  338. /* Set a long to 4 bytes for Modbus w/o any conversion (ABCD) */
  339. void modbus_set_long_abcd(long l, uint16_t *dest)
  340. {
  341. uint32_t i;
  342. memcpy(&i, &l, sizeof(uint32_t));
  343. i = modbus_htonl(i);
  344. dest[0] = (uint16_t)(i >> 16);
  345. dest[1] = (uint16_t)i;
  346. }
  347. /* Set a float to 4 bytes for Modbus with byte and word swap conversion (DCBA) */
  348. void modbus_set_float_dcba(float f, uint16_t *dest)
  349. {
  350. uint32_t i;
  351. memcpy(&i, &f, sizeof(uint32_t));
  352. i = bswap_32(modbus_htonl(i));
  353. dest[0] = (uint16_t)(i >> 16);
  354. dest[1] = (uint16_t)i;
  355. }
  356. /* Set a long to 4 bytes for Modbus with byte and word swap conversion (DCBA) */
  357. void modbus_set_long_dcba(long l, uint16_t *dest)
  358. {
  359. uint32_t i;
  360. memcpy(&i, &l, sizeof(uint32_t));
  361. i = bswap_32(modbus_htonl(i));
  362. dest[0] = (uint16_t)(i >> 16);
  363. dest[1] = (uint16_t)i;
  364. }
  365. /* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */
  366. void modbus_set_float_badc(float f, uint16_t *dest)
  367. {
  368. uint32_t i;
  369. memcpy(&i, &f, sizeof(uint32_t));
  370. i = modbus_htonl(i);
  371. dest[0] = (uint16_t)bswap_16(i >> 16);
  372. dest[1] = (uint16_t)bswap_16(i & 0xFFFF);
  373. }
  374. /* Set a long to 4 bytes for Modbus with byte swap conversion (BADC) */
  375. void modbus_set_long_badc(long l, uint16_t *dest)
  376. {
  377. uint32_t i;
  378. memcpy(&i, &l, sizeof(uint32_t));
  379. i = modbus_htonl(i);
  380. dest[0] = (uint16_t)bswap_16(i >> 16);
  381. dest[1] = (uint16_t)bswap_16(i & 0xFFFF);
  382. }
  383. /* Set a float to 4 bytes for Modbus with word swap conversion (CDAB) */
  384. void modbus_set_float_cdab(float f, uint16_t *dest)
  385. {
  386. uint32_t i;
  387. memcpy(&i, &f, sizeof(uint32_t));
  388. i = modbus_htonl(i);
  389. dest[0] = (uint16_t)i;
  390. dest[1] = (uint16_t)(i >> 16);
  391. }
  392. /* Set a long to 4 bytes for Modbus with word swap conversion (CDAB) */
  393. void modbus_set_long_cdab(long l, uint16_t *dest)
  394. {
  395. uint32_t i;
  396. memcpy(&i, &l, sizeof(uint32_t));
  397. i = modbus_htonl(i);
  398. dest[0] = (uint16_t)i;
  399. dest[1] = (uint16_t)(i >> 16);
  400. }
  401. /* DEPRECATED - Set a float to 4 bytes in a sort of Modbus format! */
  402. void modbus_set_float(float f, uint16_t *dest)
  403. {
  404. uint32_t i;
  405. memcpy(&i, &f, sizeof(uint32_t));
  406. dest[0] = (uint16_t)i;
  407. dest[1] = (uint16_t)(i >> 16);
  408. }