mmodbus.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #include "mmodbus.h"
  2. #include "usart.h"
  3. #include "delay.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 (RESET != USART_GetFlagStatus(_MMODBUS_USART,USART_FLAG_RXNE))
  59. {
  60. if (mmodbus.rxIndex < _MMODBUS_RXSIZE - 1)
  61. {
  62. mmodbus.rxBuf[mmodbus.rxIndex] = USART_ReceiveData(_MMODBUS_USART);
  63. mmodbus.rxIndex++;
  64. }
  65. else
  66. USART_ReceiveData(_MMODBUS_USART);
  67. }
  68. if ((mmodbus.rxIndex > 0) && RESET != USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_IDLE))
  69. {
  70. uint8_t i;
  71. mmodbus.done = 1;
  72. i = _MMODBUS_USART->SR;
  73. i = _MMODBUS_USART->DR;
  74. return;
  75. }
  76. else
  77. {
  78. USART_GetITStatus(_MMODBUS_USART, USART_FLAG_RXNE);
  79. }
  80. mmodbus.rxTime = gettick();
  81. }
  82. // ##################################################################################################
  83. uint16_t mmodbus_receiveRaw(uint32_t timeout)
  84. {
  85. uint32_t startTime = gettick();
  86. while (1)
  87. {
  88. if (gettick() - startTime> timeout * 1000)
  89. return 0;
  90. if ((mmodbus.rxIndex > 0) && (RESET != USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_IDLE)))
  91. {
  92. USART_ReceiveData(_MMODBUS_USART);
  93. return mmodbus.rxIndex;
  94. }
  95. if (mmodbus.done == 1)
  96. {
  97. return mmodbus.rxIndex;
  98. }
  99. mmodbus_delay(1);
  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. OSIntEnter();
  111. uint32_t startTime = gettick();
  112. GPIO_WriteBit(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,1);
  113. mmodbus_delay(1);
  114. for (uint16_t i = 0; i < size; i++)
  115. {
  116. USART_SendData(_MMODBUS_USART,data[i]);
  117. while (USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_TXE) == RESET);
  118. }
  119. while (RESET == USART_GetFlagStatus(_MMODBUS_USART, USART_FLAG_TC));
  120. OSIntExit();
  121. GPIO_WriteBit(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,0);
  122. mmodbus.done=0;
  123. mmodbus.txBusy = 0;
  124. return true;
  125. }
  126. // ##################################################################################################
  127. bool mmodbus_init(uint32_t timeout)
  128. {
  129. // HAL_GPIO_WritePin(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN, GPIO_PIN_RESET); 此处初始化在485 init过
  130. GPIO_WriteBit(_MMODBUS_CTRL_GPIO, _MMODBUS_CTRL_PIN,0);
  131. memset(&mmodbus, 0, sizeof(mmodbus));
  132. mmodbus.timeout = timeout;
  133. return true;
  134. }
  135. // ##################################################################################################
  136. void mmodbus_set16bitOrder(MModBus_16bitOrder_t MModBus_16bitOrder_)
  137. {
  138. mmodbus.byteOrder16 = MModBus_16bitOrder_;
  139. }
  140. // ##################################################################################################
  141. void mmodbus_set32bitOrder(MModBus_32bitOrder_t MModBus_32bitOrder_)
  142. {
  143. mmodbus.byteOrder32 = MModBus_32bitOrder_;
  144. }
  145. // ##################################################################################################
  146. bool mmodbus_readCoil(uint8_t slaveAddress, uint16_t number, uint8_t *data)
  147. {
  148. return mmodbus_readCoils(slaveAddress, number, 1, data);
  149. }
  150. // ##################################################################################################
  151. // 读线圈
  152. bool mmodbus_readCoils(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  153. {
  154. #if (_MMODBUS_RTU == 1)
  155. uint8_t txData[8];
  156. txData[0] = slaveAddress;
  157. txData[1] = MModbusCMD_ReadCoilStatus;
  158. txData[2] = (startnumber & 0xFF00) >> 8;
  159. txData[3] = (startnumber & 0x00FF);
  160. txData[4] = (length & 0xFF00) >> 8;
  161. txData[5] = (length & 0x00FF);
  162. static uint16_t crc;
  163. crc = mmodbus_crc16(txData, 6);
  164. txData[6] = (crc & 0x00FF);
  165. txData[7] = (crc & 0xFF00) >> 8;
  166. mmodbus_sendRaw(txData, 8, 100);
  167. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  168. if (recLen == 0)
  169. return false;
  170. if (mmodbus.rxBuf[0] != slaveAddress)
  171. return false;
  172. if (mmodbus.rxBuf[1] != MModbusCMD_ReadCoilStatus)
  173. return false;
  174. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  175. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  176. return false;
  177. if (data != NULL)
  178. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  179. return true;
  180. #endif
  181. #if (_MMODBUS_ASCII == 1)
  182. #endif
  183. }
  184. // ##################################################################################################
  185. bool mmodbus_readDiscreteInput(uint8_t slaveAddress, uint16_t number, uint8_t *data)
  186. {
  187. return mmodbus_readDiscreteInputs(slaveAddress, number, 1, data);
  188. }
  189. // ##################################################################################################
  190. // 读取线圈功能码处理
  191. bool mmodbus_readDiscreteInputs(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  192. {
  193. #if (_MMODBUS_RTU == 1)
  194. uint8_t txData[8];
  195. txData[0] = slaveAddress;
  196. txData[1] = MModbusCMD_ReadDiscreteInputs;
  197. txData[2] = (startnumber & 0xFF00) >> 8;
  198. txData[3] = (startnumber & 0x00FF);
  199. txData[4] = (length & 0xFF00) >> 8;
  200. txData[5] = (length & 0x00FF);
  201. static uint16_t crc;
  202. crc = mmodbus_crc16(txData, 6);
  203. txData[6] = (crc & 0x00FF);
  204. txData[7] = (crc & 0xFF00) >> 8;
  205. mmodbus_sendRaw(txData, 8, 100);
  206. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  207. if (recLen == 0)
  208. return false;
  209. if (mmodbus.rxBuf[0] != slaveAddress)
  210. return false;
  211. if (mmodbus.rxBuf[1] != MModbusCMD_ReadDiscreteInputs)
  212. return false;
  213. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  214. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  215. return false;
  216. if (data != NULL)
  217. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  218. return true;
  219. #endif
  220. }
  221. // ##################################################################################################
  222. bool mmodbus_readInputRegisters8i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  223. {
  224. #if (_MMODBUS_RTU == 1)
  225. uint8_t txData[8];
  226. txData[0] = slaveAddress;
  227. txData[1] = MModbusCMD_ReadInputRegisters;
  228. txData[2] = (startnumber & 0xFF00) >> 8;
  229. txData[3] = (startnumber & 0x00FF);
  230. txData[4] = (length & 0xFF00) >> 8;
  231. txData[5] = (length & 0x00FF);
  232. static uint16_t crc;
  233. crc = mmodbus_crc16(txData, 6);
  234. txData[6] = (crc & 0x00FF);
  235. txData[7] = (crc & 0xFF00) >> 8;
  236. mmodbus_sendRaw(txData, 8, 100);
  237. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  238. if (recLen == 0)
  239. return false;
  240. if (mmodbus.rxBuf[0] != slaveAddress)
  241. return false;
  242. if (mmodbus.rxBuf[1] != MModbusCMD_ReadInputRegisters)
  243. return false;
  244. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  245. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  246. return false;
  247. if (data != NULL)
  248. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  249. return true;
  250. #endif
  251. }
  252. // ##################################################################################################
  253. bool mmodbus_readInputRegister32f(uint8_t slaveAddress, uint16_t number, float *data)
  254. {
  255. return mmodbus_readInputRegisters32f(slaveAddress, number, 1, data);
  256. }
  257. // ##################################################################################################
  258. bool mmodbus_readInputRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data)
  259. {
  260. bool ret = mmodbus_readInputRegisters8i(slaveAddress, startnumber, length * 2, (uint8_t *)data);
  261. if (ret == true)
  262. {
  263. for (uint16_t i = 0; i < length; i++)
  264. {
  265. uint8_t tmp1[4], tmp2[4];
  266. switch (mmodbus.byteOrder32)
  267. {
  268. case MModBus_32bitOrder_DCBA:
  269. memcpy(tmp1, &data[i], 4);
  270. tmp2[0] = tmp1[3];
  271. tmp2[1] = tmp1[2];
  272. tmp2[2] = tmp1[1];
  273. tmp2[3] = tmp1[0];
  274. memcpy(&data[i], tmp2, 4);
  275. break;
  276. case MModBus_32bitOrder_BADC:
  277. memcpy(tmp1, &data[i], 4);
  278. tmp2[0] = tmp1[1];
  279. tmp2[1] = tmp1[0];
  280. tmp2[2] = tmp1[3];
  281. tmp2[3] = tmp1[2];
  282. memcpy(&data[i], tmp2, 4);
  283. break;
  284. case MModBus_32bitOrder_CDAB:
  285. memcpy(tmp1, &data[i], 4);
  286. tmp2[0] = tmp1[2];
  287. tmp2[1] = tmp1[3];
  288. tmp2[2] = tmp1[0];
  289. tmp2[3] = tmp1[1];
  290. memcpy(&data[i], tmp2, 4);
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. return true;
  297. }
  298. else
  299. return false;
  300. }
  301. // ##################################################################################################
  302. bool mmodbus_readInputRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data)
  303. {
  304. return mmodbus_readInputRegisters32i(slaveAddress, number, 1, data);
  305. }
  306. // ##################################################################################################
  307. bool mmodbus_readInputRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data)
  308. {
  309. return mmodbus_readInputRegisters32f(slaveAddress, startnumber, length, (float *)data);
  310. }
  311. // ##################################################################################################
  312. bool mmodbus_readInputRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data)
  313. {
  314. return mmodbus_readInputRegisters16i(slaveAddress, number, 1, data);
  315. }
  316. // ##################################################################################################
  317. bool mmodbus_readInputRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data)
  318. {
  319. bool ret = mmodbus_readInputRegisters8i(slaveAddress, startnumber, length * 1, (uint8_t *)data);
  320. if (ret == true)
  321. {
  322. uint8_t tmp1[2], tmp2[2];
  323. for (uint16_t i = 0; i < length; i++)
  324. {
  325. switch (mmodbus.byteOrder16)
  326. {
  327. case MModBus_16bitOrder_AB:
  328. memcpy(tmp1, &data[i], 2);
  329. tmp2[0] = tmp1[0];
  330. tmp2[1] = tmp1[1];
  331. memcpy(&data[i], tmp2, 2);
  332. break;
  333. default:
  334. memcpy(tmp1, &data[i], 2);
  335. tmp2[0] = tmp1[1];
  336. tmp2[1] = tmp1[0];
  337. memcpy(&data[i], tmp2, 2);
  338. break;
  339. }
  340. }
  341. return true;
  342. }
  343. else
  344. return false;
  345. }
  346. // ##################################################################################################
  347. bool mmodbus_readHoldingRegisters8i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint8_t *data)
  348. {
  349. #if (_MMODBUS_RTU == 1)
  350. uint8_t txData[8];
  351. txData[0] = slaveAddress;
  352. txData[1] = MModbusCMD_ReadHoldingRegisters;
  353. txData[2] = (startnumber & 0xFF00) >> 8;
  354. txData[3] = (startnumber & 0x00FF);
  355. txData[4] = (length & 0xFF00) >> 8;
  356. txData[5] = (length & 0x00FF);
  357. static uint16_t crc;
  358. crc = mmodbus_crc16(txData, 6);
  359. txData[6] = (crc & 0x00FF);
  360. txData[7] = (crc & 0xFF00) >> 8;
  361. mmodbus_sendRaw(txData, 8, 100);
  362. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  363. if (recLen == 0)
  364. return false;
  365. if (mmodbus.rxBuf[0] != slaveAddress)
  366. return false;
  367. if (mmodbus.rxBuf[1] != MModbusCMD_ReadHoldingRegisters)
  368. return false;
  369. crc = mmodbus_crc16(mmodbus.rxBuf, mmodbus.rxBuf[2] + 3);
  370. if (((crc & 0x00FF) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 3]) || (((crc & 0xFF00) >> 8) != mmodbus.rxBuf[mmodbus.rxBuf[2] + 4]))
  371. return false;
  372. if (data != NULL)
  373. {
  374. for (uint8_t i = 0; i < mmodbus.rxBuf[2]; i += 2)
  375. {
  376. uint8_t H = mmodbus.rxBuf[i + 3];
  377. mmodbus.rxBuf[i + 3] = mmodbus.rxBuf[i + 3 + 1];
  378. mmodbus.rxBuf[i + 3 + 1] = H;
  379. }
  380. memcpy(data, &mmodbus.rxBuf[3], mmodbus.rxBuf[2]);
  381. }
  382. return true;
  383. #endif
  384. }
  385. // ##################################################################################################
  386. bool mmodbus_readHoldingRegister32f(uint8_t slaveAddress, uint16_t number, float *data)
  387. {
  388. return mmodbus_readHoldingRegisters32f(slaveAddress, number, 1, data);
  389. }
  390. // ##################################################################################################
  391. bool mmodbus_readHoldingRegisters32f(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, float *data)
  392. {
  393. bool ret = mmodbus_readHoldingRegisters8i(slaveAddress, startnumber, length * 2, (uint8_t *)data);
  394. if (ret == true)
  395. {
  396. for (uint16_t i = 0; i < length; i++)
  397. {
  398. uint8_t tmp1[4], tmp2[4];
  399. switch (mmodbus.byteOrder32)
  400. {
  401. case MModBus_32bitOrder_DCBA:
  402. memcpy(tmp1, &data[i], 4);
  403. tmp2[0] = tmp1[3];
  404. tmp2[1] = tmp1[2];
  405. tmp2[2] = tmp1[1];
  406. tmp2[3] = tmp1[0];
  407. memcpy(&data[i], tmp2, 4);
  408. break;
  409. case MModBus_32bitOrder_BADC:
  410. memcpy(tmp1, &data[i], 4);
  411. tmp2[0] = tmp1[1];
  412. tmp2[1] = tmp1[0];
  413. tmp2[2] = tmp1[3];
  414. tmp2[3] = tmp1[2];
  415. memcpy(&data[i], tmp2, 4);
  416. break;
  417. case MModBus_32bitOrder_CDAB:
  418. memcpy(tmp1, &data[i], 4);
  419. tmp2[0] = tmp1[2];
  420. tmp2[1] = tmp1[3];
  421. tmp2[2] = tmp1[0];
  422. tmp2[3] = tmp1[1];
  423. memcpy(&data[i], tmp2, 4);
  424. break;
  425. default:
  426. break;
  427. }
  428. }
  429. return true;
  430. }
  431. else
  432. return false;
  433. }
  434. // ##################################################################################################
  435. bool mmodbus_readHoldingRegister32i(uint8_t slaveAddress, uint16_t number, uint32_t *data)
  436. {
  437. return mmodbus_readHoldingRegisters32i(slaveAddress, number, 1, data);
  438. }
  439. // ##################################################################################################
  440. bool mmodbus_readHoldingRegisters32i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint32_t *data)
  441. {
  442. return mmodbus_readHoldingRegisters32f(slaveAddress, startnumber, length, (float *)data);
  443. }
  444. // ##################################################################################################
  445. bool mmodbus_readHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t *data)
  446. {
  447. return mmodbus_readHoldingRegisters16i(slaveAddress, number, 1, data);
  448. }
  449. // ##################################################################################################
  450. bool mmodbus_readHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data)
  451. {
  452. bool ret = mmodbus_readHoldingRegisters8i(slaveAddress, startnumber, length * 1, (uint8_t *)data);
  453. if (ret == true)
  454. {
  455. uint8_t tmp1[2], tmp2[2];
  456. for (uint16_t i = 0; i < length; i++)
  457. {
  458. switch (mmodbus.byteOrder16)
  459. {
  460. case MModBus_16bitOrder_AB:
  461. memcpy(tmp1, &data[i], 2);
  462. tmp2[0] = tmp1[0];
  463. tmp2[1] = tmp1[1];
  464. memcpy(&data[i], tmp2, 2);
  465. break;
  466. default:
  467. memcpy(tmp1, &data[i], 2);
  468. tmp2[0] = tmp1[1];
  469. tmp2[1] = tmp1[0];
  470. memcpy(&data[i], tmp2, 2);
  471. break;
  472. }
  473. }
  474. return true;
  475. }
  476. else
  477. return false;
  478. }
  479. // ##################################################################################################
  480. bool mmodbus_writeCoil(uint8_t slaveAddress, uint16_t number, uint8_t data)
  481. {
  482. #if (_MMODBUS_RTU == 1)
  483. uint8_t txData[8];
  484. txData[0] = slaveAddress;
  485. txData[1] = MModbusCMD_WriteSingleCoil;
  486. txData[2] = (number & 0xFF00) >> 8;
  487. txData[3] = (number & 0x00FF);
  488. txData[4] = 0;
  489. if (data == 0)
  490. txData[5] = 0;
  491. else
  492. txData[5] = 0xFF;
  493. static uint16_t crc;
  494. crc = mmodbus_crc16(txData, 6);
  495. txData[6] = (crc & 0x00FF);
  496. txData[7] = (crc & 0xFF00) >> 8;
  497. mmodbus_sendRaw(txData, 8, 100);
  498. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  499. if (recLen == 0)
  500. return false;
  501. if (memcmp(txData, mmodbus.rxBuf, 8) == 0)
  502. return true;
  503. else
  504. return false;
  505. #endif
  506. #if (_MMODBUS_ASCII == 1)
  507. #endif
  508. }
  509. // ##################################################################################################
  510. bool mmodbus_writeHoldingRegister16i(uint8_t slaveAddress, uint16_t number, uint16_t data)
  511. {
  512. #if (_MMODBUS_RTU == 1)
  513. uint8_t txData[8];
  514. txData[0] = slaveAddress;
  515. txData[1] = MModbusCMD_WriteSingleRegister;
  516. txData[2] = (number & 0xFF00) >> 8;
  517. txData[3] = (number & 0x00FF);
  518. txData[4] = (data & 0xFF00) >> 8;
  519. txData[5] = data & 0x00FF;
  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_writeHoldingRegisters16i(uint8_t slaveAddress, uint16_t startnumber, uint16_t length, uint16_t *data)
  538. {
  539. #if (_MMODBUS_RTU == 1)
  540. if (length == 1)
  541. {
  542. return mmodbus_writeHoldingRegister16i(slaveAddress, startnumber, data[0]);
  543. }
  544. else
  545. {
  546. uint8_t txData[7 + length * 2 + 2];
  547. txData[0] = slaveAddress;
  548. txData[1] = MModbusCMD_WriteMultipleRegisters;
  549. txData[2] = (startnumber & 0xFF00) >> 8;
  550. txData[3] = (startnumber & 0x00FF);
  551. txData[4] = (length & 0xFF00) >> 8;
  552. txData[5] = (length & 0x00FF);
  553. txData[6] = (length * 2);
  554. uint8_t tmp1[2], tmp2[2];
  555. for (uint16_t i = 0; i < length; i++)
  556. {
  557. switch (mmodbus.byteOrder16)
  558. {
  559. case MModBus_16bitOrder_AB:
  560. memcpy(tmp1, &data[i], 2);
  561. tmp2[0] = tmp1[1];
  562. tmp2[1] = tmp1[0];
  563. memcpy(&txData[7 + i * 2], tmp2, 2);
  564. break;
  565. default:
  566. memcpy(tmp1, &data[i], 2);
  567. tmp2[0] = tmp1[0];
  568. tmp2[1] = tmp1[1];
  569. memcpy(&txData[7 + i * 2], tmp2, 2);
  570. break;
  571. }
  572. }
  573. static uint16_t crc;
  574. crc = mmodbus_crc16(txData, 7 + length * 2);
  575. txData[7 + length * 2 + 0] = (crc & 0x00FF);
  576. txData[7 + length * 2 + 1] = (crc & 0xFF00) >> 8;
  577. mmodbus_sendRaw(txData, 7 + length * 2 + 2, 100);
  578. uint16_t recLen = mmodbus_receiveRaw(mmodbus.timeout);
  579. if (recLen == 0)
  580. return false;
  581. crc = mmodbus_crc16(txData, 6);
  582. txData[6] = (crc & 0x00FF);
  583. txData[7] = (crc & 0xFF00) >> 8;
  584. if (memcmp(txData, mmodbus.rxBuf, 8) == 0)
  585. return true;
  586. else
  587. return false;
  588. }
  589. #endif
  590. #if (_MMODBUS_ASCII == 1)
  591. #endif
  592. }
  593. // ##################################################################################################