modbus.c 20 KB

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