mmodbus.c 21 KB

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