mmodbus.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #include "mmodbus.h"
  2. #include "usart.h"
  3. #include "delay.h"
  4. #include "log.h"
  5. #include "led.h"
  6. MModBus_t mmodbus;
  7. // #####################################################################################################
  8. #if (_MMODBUS_RTU == 1)
  9. static const uint16_t wCRCTable[] =
  10. {
  11. 0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
  12. 0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
  13. 0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
  14. 0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
  15. 0XD801, 0X18C0, 0X1980, 0XD941, 0X1B00, 0XDBC1, 0XDA81, 0X1A40,
  16. 0X1E00, 0XDEC1, 0XDF81, 0X1F40, 0XDD01, 0X1DC0, 0X1C80, 0XDC41,
  17. 0X1400, 0XD4C1, 0XD581, 0X1540, 0XD701, 0X17C0, 0X1680, 0XD641,
  18. 0XD201, 0X12C0, 0X1380, 0XD341, 0X1100, 0XD1C1, 0XD081, 0X1040,
  19. 0XF001, 0X30C0, 0X3180, 0XF141, 0X3300, 0XF3C1, 0XF281, 0X3240,
  20. 0X3600, 0XF6C1, 0XF781, 0X3740, 0XF501, 0X35C0, 0X3480, 0XF441,
  21. 0X3C00, 0XFCC1, 0XFD81, 0X3D40, 0XFF01, 0X3FC0, 0X3E80, 0XFE41,
  22. 0XFA01, 0X3AC0, 0X3B80, 0XFB41, 0X3900, 0XF9C1, 0XF881, 0X3840,
  23. 0X2800, 0XE8C1, 0XE981, 0X2940, 0XEB01, 0X2BC0, 0X2A80, 0XEA41,
  24. 0XEE01, 0X2EC0, 0X2F80, 0XEF41, 0X2D00, 0XEDC1, 0XEC81, 0X2C40,
  25. 0XE401, 0X24C0, 0X2580, 0XE541, 0X2700, 0XE7C1, 0XE681, 0X2640,
  26. 0X2200, 0XE2C1, 0XE381, 0X2340, 0XE101, 0X21C0, 0X2080, 0XE041,
  27. 0XA001, 0X60C0, 0X6180, 0XA141, 0X6300, 0XA3C1, 0XA281, 0X6240,
  28. 0X6600, 0XA6C1, 0XA781, 0X6740, 0XA501, 0X65C0, 0X6480, 0XA441,
  29. 0X6C00, 0XACC1, 0XAD81, 0X6D40, 0XAF01, 0X6FC0, 0X6E80, 0XAE41,
  30. 0XAA01, 0X6AC0, 0X6B80, 0XAB41, 0X6900, 0XA9C1, 0XA881, 0X6840,
  31. 0X7800, 0XB8C1, 0XB981, 0X7940, 0XBB01, 0X7BC0, 0X7A80, 0XBA41,
  32. 0XBE01, 0X7EC0, 0X7F80, 0XBF41, 0X7D00, 0XBDC1, 0XBC81, 0X7C40,
  33. 0XB401, 0X74C0, 0X7580, 0XB541, 0X7700, 0XB7C1, 0XB681, 0X7640,
  34. 0X7200, 0XB2C1, 0XB381, 0X7340, 0XB101, 0X71C0, 0X7080, 0XB041,
  35. 0X5000, 0X90C1, 0X9181, 0X5140, 0X9301, 0X53C0, 0X5280, 0X9241,
  36. 0X9601, 0X56C0, 0X5780, 0X9741, 0X5500, 0X95C1, 0X9481, 0X5440,
  37. 0X9C01, 0X5CC0, 0X5D80, 0X9D41, 0X5F00, 0X9FC1, 0X9E81, 0X5E40,
  38. 0X5A00, 0X9AC1, 0X9B81, 0X5B40, 0X9901, 0X59C0, 0X5880, 0X9841,
  39. 0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
  40. 0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
  41. 0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
  42. 0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040};
  43. // crc校验
  44. uint16_t mmodbus_crc16(const uint8_t *nData, uint16_t wLength)
  45. {
  46. uint8_t nTemp;
  47. uint16_t wCRCWord = 0xFFFF;
  48. while (wLength--)
  49. {
  50. nTemp = *nData++ ^ wCRCWord;
  51. wCRCWord >>= 8;
  52. wCRCWord ^= wCRCTable[nTemp];
  53. }
  54. return wCRCWord;
  55. }
  56. #endif
  57. void mmodbus_callback(void)
  58. {
  59. if (RESET != usart_interrupt_flag_get(_MMODBUS_USART, USART_INT_FLAG_RBNE))
  60. {
  61. if (mmodbus.rxIndex < _MMODBUS_RXSIZE - 1)
  62. {
  63. mmodbus.rxBuf[mmodbus.rxIndex] = usart_data_receive(_MMODBUS_USART);
  64. mmodbus.rxIndex++;
  65. }
  66. else
  67. {
  68. usart_data_receive(_MMODBUS_USART);
  69. }
  70. usart_interrupt_flag_clear(_MMODBUS_USART, USART_INT_FLAG_RBNE);
  71. }
  72. if ((mmodbus.rxIndex > 0) && RESET != usart_interrupt_flag_get(_MMODBUS_USART, USART_INT_FLAG_IDLE))
  73. {
  74. mmodbus.done = 1;
  75. usart_data_receive(_MMODBUS_USART);
  76. usart_interrupt_flag_clear(_MMODBUS_USART, USART_INT_FLAG_IDLE);
  77. gd_eval_led_toggle(LED_485RX);
  78. return;
  79. }
  80. else
  81. {
  82. usart_data_receive(USART0);
  83. usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
  84. }
  85. mmodbus.rxTime = gettick();
  86. }
  87. // ##################################################################################################
  88. uint16_t mmodbus_receiveRaw(uint32_t timeout)
  89. {
  90. uint32_t startTime = gettick();
  91. while (1)
  92. {
  93. if (gettick() - startTime> timeout * 1000)
  94. return 0;
  95. if (mmodbus.done == 1)
  96. {
  97. return mmodbus.rxIndex;
  98. }
  99. mmodbus_delay(5);
  100. }
  101. }
  102. // ##################################################################################################
  103. bool mmodbus_sendRaw(uint8_t *data, uint16_t size, uint32_t timeout)
  104. {
  105. while(mmodbus.txBusy == 1)
  106. mmodbus_delay(1);
  107. mmodbus.txBusy = 1;
  108. memset(mmodbus.rxBuf, 0, _MMODBUS_RXSIZE);
  109. mmodbus.rxIndex = 0;
  110. mmodbus.done = 0;
  111. uint32_t startTime = gettick();
  112. portENTER_CRITICAL();
  113. gpio_bit_write(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,1);
  114. mmodbus_delay(1);
  115. for (uint16_t i = 0; i < size; i++)
  116. {
  117. usart_data_transmit(_MMODBUS_USART,data[i]);
  118. while (usart_flag_get(_MMODBUS_USART, USART_FLAG_TBE ) == RESET);
  119. }
  120. while (RESET == usart_flag_get(_MMODBUS_USART, USART_FLAG_TC));
  121. gpio_bit_write(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,0);
  122. portEXIT_CRITICAL();
  123. mmodbus.done=0;
  124. mmodbus.txBusy = 0;
  125. return true;
  126. }
  127. // ##################################################################################################
  128. bool mmodbus_init(uint32_t timeout)
  129. {
  130. // HAL_GPIO_WritePin(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN, GPIO_PIN_RESET); 此处初始化在485 init过
  131. gpio_bit_write(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,0);
  132. memset(&mmodbus, 0, sizeof(mmodbus));
  133. mmodbus.timeout = timeout;
  134. return true;
  135. }
  136. // ##################################################################################################
  137. void mmodbus_set16bitOrder(MModBus_16bitOrder_t MModBus_16bitOrder_)
  138. {
  139. mmodbus.byteOrder16 = MModBus_16bitOrder_;
  140. }
  141. // ##################################################################################################
  142. void mmodbus_set32bitOrder(MModBus_32bitOrder_t MModBus_32bitOrder_)
  143. {
  144. mmodbus.byteOrder32 = MModBus_32bitOrder_;
  145. }
  146. // ##################################################################################################
  147. bool mmodbus_readCoil(uint8_t slaveAddress, uint16_t number, uint8_t *data)
  148. {
  149. return mmodbus_readCoils(slaveAddress, number, 1, data);
  150. }
  151. // ##################################################################################################
  152. // 读线圈
  153. bool mmodbus_readCoils(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  154. {
  155. #if (_MMODBUS_RTU == 1)
  156. uint8_t txData[8];
  157. txData[0] = slaveAddress;
  158. txData[1] = MModbusCMD_ReadCoilStatus;
  159. txData[2] = (startnumber & 0xFF00) >> 8;
  160. txData[3] = (startnumber & 0x00FF);
  161. txData[4] = (length & 0xFF00) >> 8;
  162. txData[5] = (length & 0x00FF);
  163. static uint16_t crc;
  164. crc = mmodbus_crc16(txData, 6);
  165. txData[6] = (crc & 0x00FF);
  166. txData[7] = (crc & 0xFF00) >> 8;
  167. mmodbus_sendRaw(txData, 8, 100);
  168. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  169. if (recLen == 0)
  170. return false;
  171. if (mmodbus.rxBuf[0] != slaveAddress)
  172. return false;
  173. if (mmodbus.rxBuf[1] != MModbusCMD_ReadCoilStatus)
  174. return false;
  175. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  176. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  177. return false;
  178. if (data != NULL)
  179. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  180. return true;
  181. #endif
  182. #if (_MMODBUS_ASCII == 1)
  183. #endif
  184. }
  185. // ##################################################################################################
  186. bool mmodbus_readDiscreteInput(uint8_t slaveAddress, uint16_t number, uint8_t *data)
  187. {
  188. return mmodbus_readDiscreteInputs(slaveAddress, number, 1, data);
  189. }
  190. // ##################################################################################################
  191. // 读取线圈功能码处理
  192. bool mmodbus_readDiscreteInputs(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  193. {
  194. #if (_MMODBUS_RTU == 1)
  195. uint8_t txData[8];
  196. txData[0] = slaveAddress;
  197. txData[1] = MModbusCMD_ReadDiscreteInputs;
  198. txData[2] = (startnumber & 0xFF00) >> 8;
  199. txData[3] = (startnumber & 0x00FF);
  200. txData[4] = (length & 0xFF00) >> 8;
  201. txData[5] = (length & 0x00FF);
  202. static uint16_t crc;
  203. crc = mmodbus_crc16(txData, 6);
  204. txData[6] = (crc & 0x00FF);
  205. txData[7] = (crc & 0xFF00) >> 8;
  206. mmodbus_sendRaw(txData, 8, 100);
  207. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  208. if (recLen == 0)
  209. return false;
  210. if (mmodbus.rxBuf[0] != slaveAddress)
  211. return false;
  212. if (mmodbus.rxBuf[1] != MModbusCMD_ReadDiscreteInputs)
  213. return false;
  214. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  215. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  216. return false;
  217. if (data != NULL)
  218. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  219. return true;
  220. #endif
  221. }
  222. // ##################################################################################################
  223. bool mmodbus_readInputRegisters8i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  224. {
  225. #if (_MMODBUS_RTU == 1)
  226. uint8_t txData[8];
  227. txData[0] = slaveAddress;
  228. txData[1] = MModbusCMD_ReadInputRegisters;
  229. txData[2] = (startnumber & 0xFF00) >> 8;
  230. txData[3] = (startnumber & 0x00FF);
  231. txData[4] = (length & 0xFF00) >> 8;
  232. txData[5] = (length & 0x00FF);
  233. static uint16_t crc;
  234. crc = mmodbus_crc16(txData, 6);
  235. txData[6] = (crc & 0x00FF);
  236. txData[7] = (crc & 0xFF00) >> 8;
  237. mmodbus_sendRaw(txData, 8, 100);
  238. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  239. if (recLen == 0)
  240. return false;
  241. if (mmodbus.rxBuf[0] != slaveAddress)
  242. return false;
  243. if (mmodbus.rxBuf[1] != MModbusCMD_ReadInputRegisters)
  244. return false;
  245. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  246. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  247. return false;
  248. if (data != NULL)
  249. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  250. return true;
  251. #endif
  252. }
  253. // ##################################################################################################
  254. bool mmodbus_readInputRegister32f(uint8_t slaveAddress, uint16_t number, float *data)
  255. {
  256. return mmodbus_readInputRegisters32f(slaveAddress, number, 1, data);
  257. }
  258. // ##################################################################################################
  259. bool mmodbus_readInputRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data)
  260. {
  261. bool ret = mmodbus_readInputRegisters8i(slaveAddress, startnumber, length * 2, (uint8_t *)data);
  262. if (ret == true)
  263. {
  264. for (uint16_t i = 0; i < length; i++)
  265. {
  266. uint8_t tmp1[4], tmp2[4];
  267. switch (mmodbus.byteOrder32)
  268. {
  269. case MModBus_32bitOrder_DCBA:
  270. memcpy(tmp1, &data[i], 4);
  271. tmp2[0] = tmp1[3];
  272. tmp2[1] = tmp1[2];
  273. tmp2[2] = tmp1[1];
  274. tmp2[3] = tmp1[0];
  275. memcpy(&data[i], tmp2, 4);
  276. break;
  277. case MModBus_32bitOrder_BADC:
  278. memcpy(tmp1, &data[i], 4);
  279. tmp2[0] = tmp1[1];
  280. tmp2[1] = tmp1[0];
  281. tmp2[2] = tmp1[3];
  282. tmp2[3] = tmp1[2];
  283. memcpy(&data[i], tmp2, 4);
  284. break;
  285. case MModBus_32bitOrder_CDAB:
  286. memcpy(tmp1, &data[i], 4);
  287. tmp2[0] = tmp1[2];
  288. tmp2[1] = tmp1[3];
  289. tmp2[2] = tmp1[0];
  290. tmp2[3] = tmp1[1];
  291. memcpy(&data[i], tmp2, 4);
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. return true;
  298. }
  299. else
  300. return false;
  301. }
  302. // ##################################################################################################
  303. bool mmodbus_readInputRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data)
  304. {
  305. return mmodbus_readInputRegisters32i(slaveAddress, number, 1, data);
  306. }
  307. // ##################################################################################################
  308. bool mmodbus_readInputRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data)
  309. {
  310. return mmodbus_readInputRegisters32f(slaveAddress, startnumber, length, (float *)data);
  311. }
  312. // ##################################################################################################
  313. bool mmodbus_readInputRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data)
  314. {
  315. return mmodbus_readInputRegisters16i(slaveAddress, number, 1, data);
  316. }
  317. // ##################################################################################################
  318. bool mmodbus_readInputRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data)
  319. {
  320. bool ret = mmodbus_readInputRegisters8i(slaveAddress, startnumber, length * 1, (uint8_t *)data);
  321. if (ret == true)
  322. {
  323. uint8_t tmp1[2], tmp2[2];
  324. for (uint16_t i = 0; i < length; i++)
  325. {
  326. switch (mmodbus.byteOrder16)
  327. {
  328. case MModBus_16bitOrder_AB:
  329. memcpy(tmp1, &data[i], 2);
  330. tmp2[0] = tmp1[0];
  331. tmp2[1] = tmp1[1];
  332. memcpy(&data[i], tmp2, 2);
  333. break;
  334. default:
  335. memcpy(tmp1, &data[i], 2);
  336. tmp2[0] = tmp1[1];
  337. tmp2[1] = tmp1[0];
  338. memcpy(&data[i], tmp2, 2);
  339. break;
  340. }
  341. }
  342. return true;
  343. }
  344. else
  345. return false;
  346. }
  347. // ##################################################################################################
  348. bool mmodbus_readHoldingRegisters8i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  349. {
  350. #if (_MMODBUS_RTU == 1)
  351. uint8_t txData[8];
  352. txData[0] = slaveAddress;
  353. txData[1] = MModbusCMD_ReadHoldingRegisters;
  354. txData[2] = (startnumber & 0xFF00) >> 8;
  355. txData[3] = (startnumber & 0x00FF);
  356. txData[4] = (length & 0xFF00) >> 8;
  357. txData[5] = (length & 0x00FF);
  358. static uint16_t crc;
  359. crc = mmodbus_crc16(txData, 6);
  360. txData[6] = (crc & 0x00FF);
  361. txData[7] = (crc & 0xFF00) >> 8;
  362. mmodbus_sendRaw(txData, 8, 100);
  363. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  364. if (recLen == 0)
  365. return false;
  366. if (mmodbus.rxBuf[0] != slaveAddress)
  367. return false;
  368. if (mmodbus.rxBuf[1] != MModbusCMD_ReadHoldingRegisters)
  369. return false;
  370. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  371. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  372. return false;
  373. if (data != NULL)
  374. {
  375. for (uint8_t i = 0; i < mmodbus.rxBuf[2]; i += 2)
  376. {
  377. uint8_t H = mmodbus.rxBuf[i + 3];
  378. mmodbus.rxBuf[i + 3] = mmodbus.rxBuf[i + 3 + 1];
  379. mmodbus.rxBuf[i + 3 + 1] = H;
  380. }
  381. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  382. }
  383. return true;
  384. #endif
  385. }
  386. // ##################################################################################################
  387. bool mmodbus_readHoldingRegister32f(uint8_t slaveAddress, uint16_t number, float *data)
  388. {
  389. return mmodbus_readHoldingRegisters32f(slaveAddress, number, 1, data);
  390. }
  391. // ##################################################################################################
  392. bool mmodbus_readHoldingRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data)
  393. {
  394. bool ret = mmodbus_readHoldingRegisters8i(slaveAddress, startnumber, length * 2, (uint8_t *)data);
  395. if (ret == true)
  396. {
  397. for (uint16_t i = 0; i < length; i++)
  398. {
  399. uint8_t tmp1[4], tmp2[4];
  400. switch (mmodbus.byteOrder32)
  401. {
  402. case MModBus_32bitOrder_DCBA:
  403. memcpy(tmp1, &data[i], 4);
  404. tmp2[0] = tmp1[3];
  405. tmp2[1] = tmp1[2];
  406. tmp2[2] = tmp1[1];
  407. tmp2[3] = tmp1[0];
  408. memcpy(&data[i], tmp2, 4);
  409. break;
  410. case MModBus_32bitOrder_BADC:
  411. memcpy(tmp1, &data[i], 4);
  412. tmp2[0] = tmp1[1];
  413. tmp2[1] = tmp1[0];
  414. tmp2[2] = tmp1[3];
  415. tmp2[3] = tmp1[2];
  416. memcpy(&data[i], tmp2, 4);
  417. break;
  418. case MModBus_32bitOrder_CDAB:
  419. memcpy(tmp1, &data[i], 4);
  420. tmp2[0] = tmp1[2];
  421. tmp2[1] = tmp1[3];
  422. tmp2[2] = tmp1[0];
  423. tmp2[3] = tmp1[1];
  424. memcpy(&data[i], tmp2, 4);
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. return true;
  431. }
  432. else
  433. return false;
  434. }
  435. // ##################################################################################################
  436. bool mmodbus_readHoldingRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data)
  437. {
  438. return mmodbus_readHoldingRegisters32i(slaveAddress, number, 1, data);
  439. }
  440. // ##################################################################################################
  441. bool mmodbus_readHoldingRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data)
  442. {
  443. return mmodbus_readHoldingRegisters32f(slaveAddress, startnumber, length, (float *)data);
  444. }
  445. // ##################################################################################################
  446. bool mmodbus_readHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data)
  447. {
  448. return mmodbus_readHoldingRegisters16i(slaveAddress, number, 1, data);
  449. }
  450. // ##################################################################################################
  451. bool mmodbus_readHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data)
  452. {
  453. bool ret = mmodbus_readHoldingRegisters8i(slaveAddress, startnumber, length * 1, (uint8_t *)data);
  454. if (ret == true)
  455. {
  456. uint8_t tmp1[2], tmp2[2];
  457. for (uint16_t i = 0; i < length; i++)
  458. {
  459. switch (mmodbus.byteOrder16)
  460. {
  461. case MModBus_16bitOrder_AB:
  462. memcpy(tmp1, &data[i], 2);
  463. tmp2[0] = tmp1[0];
  464. tmp2[1] = tmp1[1];
  465. memcpy(&data[i], tmp2, 2);
  466. break;
  467. default:
  468. memcpy(tmp1, &data[i], 2);
  469. tmp2[0] = tmp1[1];
  470. tmp2[1] = tmp1[0];
  471. memcpy(&data[i], tmp2, 2);
  472. break;
  473. }
  474. }
  475. return true;
  476. }
  477. else
  478. return false;
  479. }
  480. // ##################################################################################################
  481. bool mmodbus_writeCoil(uint8_t slaveAddress, uint16_t number, uint8_t data)
  482. {
  483. #if (_MMODBUS_RTU == 1)
  484. uint8_t txData[8];
  485. txData[0] = slaveAddress;
  486. txData[1] = MModbusCMD_WriteSingleCoil;
  487. txData[2] = (number & 0xFF00) >> 8;
  488. txData[3] = (number & 0x00FF);
  489. if (data == 0)
  490. txData[4] = 0;
  491. else
  492. txData[4] = 0xFF;
  493. txData[5] = 0;
  494. static uint16_t crc;
  495. crc = mmodbus_crc16(txData, 6);
  496. txData[6] = (crc & 0x00FF);
  497. txData[7] = (crc & 0xFF00) >> 8;
  498. mmodbus_sendRaw(txData, 8, 100);
  499. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  500. if (recLen == 0)
  501. return false;
  502. if (memcmp(txData, mmodbus.rxBuf, 8) == 0)
  503. return true;
  504. else
  505. return false;
  506. #endif
  507. #if (_MMODBUS_ASCII == 1)
  508. #endif
  509. }
  510. // ##################################################################################################
  511. bool mmodbus_writeHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t data)
  512. {
  513. #if (_MMODBUS_RTU == 1)
  514. uint8_t txData[8];
  515. txData[0] = slaveAddress;
  516. txData[1] = MModbusCMD_WriteSingleRegister;
  517. txData[2] = (number & 0xFF00) >> 8;
  518. txData[3] = (number & 0x00FF);
  519. txData[4] = (data & 0xFF00) >> 8;
  520. txData[5] = data & 0x00FF;
  521. static uint16_t crc;
  522. crc = mmodbus_crc16(txData, 6);
  523. txData[6] = (crc & 0x00FF);
  524. txData[7] = (crc & 0xFF00) >> 8;
  525. mmodbus_sendRaw(txData, 8, 100);
  526. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  527. if (recLen == 0)
  528. return false;
  529. if (memcmp(txData, mmodbus.rxBuf, 8) == 0)
  530. return true;
  531. else
  532. return false;
  533. #endif
  534. #if (_MMODBUS_ASCII == 1)
  535. #endif
  536. }
  537. // ##################################################################################################
  538. bool mmodbus_writeHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data)
  539. {
  540. #if (_MMODBUS_RTU == 1)
  541. if (length == 1)
  542. {
  543. return mmodbus_writeHoldingRegister16i(slaveAddress, startnumber, data[0]);
  544. }
  545. else
  546. {
  547. uint8_t txData[7 + length * 2 + 2];
  548. txData[0] = slaveAddress;
  549. txData[1] = MModbusCMD_WriteMultipleRegisters;
  550. txData[2] = (startnumber & 0xFF00) >> 8;
  551. txData[3] = (startnumber & 0x00FF);
  552. txData[4] = (length & 0xFF00) >> 8;
  553. txData[5] = (length & 0x00FF);
  554. txData[6] = (length * 2);
  555. uint8_t tmp1[2], tmp2[2];
  556. for (uint16_t i = 0; i < length; i++)
  557. {
  558. switch (mmodbus.byteOrder16)
  559. {
  560. case MModBus_16bitOrder_AB:
  561. memcpy(tmp1, &data[i], 2);
  562. tmp2[0] = tmp1[1];
  563. tmp2[1] = tmp1[0];
  564. memcpy(&txData[7 + i * 2], tmp2, 2);
  565. break;
  566. default:
  567. memcpy(tmp1, &data[i], 2);
  568. tmp2[0] = tmp1[0];
  569. tmp2[1] = tmp1[1];
  570. memcpy(&txData[7 + i * 2], tmp2, 2);
  571. break;
  572. }
  573. }
  574. static uint16_t crc;
  575. crc = mmodbus_crc16(txData, 7 + length * 2);
  576. txData[7 + length * 2 + 0] = (crc & 0x00FF);
  577. txData[7 + length * 2 + 1] = (crc & 0xFF00) >> 8;
  578. mmodbus_sendRaw(txData, 7 + length * 2 + 2, 100);
  579. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  580. if (recLen == 0)
  581. return false;
  582. crc = mmodbus_crc16(txData, 6);
  583. txData[6] = (crc & 0x00FF);
  584. txData[7] = (crc & 0xFF00) >> 8;
  585. if (memcmp(txData, mmodbus.rxBuf, 8) == 0)
  586. return true;
  587. else
  588. return false;
  589. }
  590. #endif
  591. #if (_MMODBUS_ASCII == 1)
  592. #endif
  593. }
  594. // ##################################################################################################