mbfuncholding_m.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * File: $Id: mbfuncholding_m.c,v 1.60 2013/09/02 14:13:40 Armink Add Master Functions Exp $
  29. */
  30. /* ----------------------- System includes ----------------------------------*/
  31. #include "stdlib.h"
  32. #include "string.h"
  33. /* ----------------------- Platform includes --------------------------------*/
  34. #include "port.h"
  35. /* ----------------------- Modbus includes ----------------------------------*/
  36. #include "mb.h"
  37. #include "mb_m.h"
  38. #include "mbframe.h"
  39. #include "mbproto.h"
  40. #include "mbconfig.h"
  41. /* ----------------------- Defines ------------------------------------------*/
  42. #define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
  43. #define MB_PDU_REQ_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  44. #define MB_PDU_REQ_READ_SIZE ( 4 )
  45. #define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
  46. #define MB_PDU_FUNC_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
  47. #define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
  48. #define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
  49. #define MB_PDU_REQ_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
  50. #define MB_PDU_REQ_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
  51. #define MB_PDU_REQ_WRITE_SIZE ( 4 )
  52. #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
  53. #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
  54. #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
  55. #define MB_PDU_REQ_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
  56. #define MB_PDU_REQ_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  57. #define MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
  58. #define MB_PDU_REQ_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
  59. #define MB_PDU_REQ_WRITE_MUL_SIZE_MIN ( 5 )
  60. #define MB_PDU_REQ_WRITE_MUL_REGCNT_MAX ( 0x0078 )
  61. #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
  62. #define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  63. #define MB_PDU_FUNC_WRITE_MUL_SIZE ( 4 )
  64. #define MB_PDU_REQ_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
  65. #define MB_PDU_REQ_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  66. #define MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
  67. #define MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
  68. #define MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
  69. #define MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
  70. #define MB_PDU_REQ_READWRITE_SIZE_MIN ( 9 )
  71. #define MB_PDU_FUNC_READWRITE_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
  72. #define MB_PDU_FUNC_READWRITE_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
  73. #define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 1 )
  74. /* ----------------------- Static functions ---------------------------------*/
  75. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  76. /* ----------------------- Start implementation -----------------------------*/
  77. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
  78. #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
  79. /**
  80. * This function will request write holding register.
  81. *
  82. * @param ucSndAddr salve address
  83. * @param usRegAddr register start address
  84. * @param usRegData register data to be written
  85. * @param lTimeOut timeout (-1 will waiting forever)
  86. *
  87. * @return error code
  88. */
  89. eMBMasterReqErrCode
  90. eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut )
  91. {
  92. UCHAR *ucMBFrame;
  93. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  94. if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  95. else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
  96. else
  97. {
  98. vMBMasterGetPDUSndBuf(&ucMBFrame);
  99. vMBMasterSetDestAddress(ucSndAddr);
  100. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
  101. ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8;
  102. ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr;
  103. ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8;
  104. ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ;
  105. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
  106. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  107. eErrStatus = eMBMasterWaitRequestFinish( );
  108. }
  109. return eErrStatus;
  110. }
  111. eMBException
  112. eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  113. {
  114. USHORT usRegAddress;
  115. eMBException eStatus = MB_EX_NONE;
  116. eMBErrorCode eRegStatus;
  117. if( *usLen == ( MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_SIZE ) )
  118. {
  119. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
  120. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
  121. usRegAddress++;
  122. /* Make callback to update the value. */
  123. eRegStatus = eMBMasterRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
  124. usRegAddress, 1, MB_REG_WRITE );
  125. /* If an error occured convert it into a Modbus exception. */
  126. if( eRegStatus != MB_ENOERR )
  127. {
  128. eStatus = prveMBError2Exception( eRegStatus );
  129. }
  130. }
  131. else
  132. {
  133. /* Can't be a valid request because the length is incorrect. */
  134. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  135. }
  136. return eStatus;
  137. }
  138. #endif
  139. #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
  140. /**
  141. * This function will request write multiple holding register.
  142. *
  143. * @param ucSndAddr salve address
  144. * @param usRegAddr register start address
  145. * @param usNRegs register total number
  146. * @param pusDataBuffer data to be written
  147. * @param lTimeOut timeout (-1 will waiting forever)
  148. *
  149. * @return error code
  150. */
  151. eMBMasterReqErrCode
  152. eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
  153. USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut )
  154. {
  155. UCHAR *ucMBFrame;
  156. USHORT usRegIndex = 0;
  157. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  158. if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  159. else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
  160. else
  161. {
  162. vMBMasterGetPDUSndBuf(&ucMBFrame);
  163. vMBMasterSetDestAddress(ucSndAddr);
  164. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
  165. ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8;
  166. ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr;
  167. ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8;
  168. ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ;
  169. ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2;
  170. ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
  171. while( usNRegs > usRegIndex)
  172. {
  173. *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
  174. *ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
  175. }
  176. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs );
  177. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  178. eErrStatus = eMBMasterWaitRequestFinish( );
  179. }
  180. return eErrStatus;
  181. }
  182. eMBException
  183. eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  184. {
  185. UCHAR *ucMBFrame;
  186. USHORT usRegAddress;
  187. USHORT usRegCount;
  188. UCHAR ucRegByteCount;
  189. eMBException eStatus = MB_EX_NONE;
  190. eMBErrorCode eRegStatus;
  191. /* If this request is broadcast, the *usLen is not need check. */
  192. if( ( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() )
  193. {
  194. vMBMasterGetPDUSndBuf(&ucMBFrame);
  195. usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 );
  196. usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] );
  197. usRegAddress++;
  198. usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] << 8 );
  199. usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] );
  200. ucRegByteCount = ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF];
  201. if( ucRegByteCount == 2 * usRegCount )
  202. {
  203. /* Make callback to update the register values. */
  204. eRegStatus =
  205. eMBMasterRegHoldingCB( &ucMBFrame[MB_PDU_REQ_WRITE_MUL_VALUES_OFF],
  206. usRegAddress, usRegCount, MB_REG_WRITE );
  207. /* If an error occured convert it into a Modbus exception. */
  208. if( eRegStatus != MB_ENOERR )
  209. {
  210. eStatus = prveMBError2Exception( eRegStatus );
  211. }
  212. }
  213. else
  214. {
  215. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  216. }
  217. }
  218. else
  219. {
  220. /* Can't be a valid request because the length is incorrect. */
  221. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  222. }
  223. return eStatus;
  224. }
  225. #endif
  226. #if MB_FUNC_READ_HOLDING_ENABLED > 0
  227. /**
  228. * This function will request read holding register.
  229. *
  230. * @param ucSndAddr salve address
  231. * @param usRegAddr register start address
  232. * @param usNRegs register total number
  233. * @param lTimeOut timeout (-1 will waiting forever)
  234. *
  235. * @return error code
  236. */
  237. eMBMasterReqErrCode
  238. eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut )
  239. {
  240. UCHAR *ucMBFrame;
  241. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  242. if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  243. else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
  244. else
  245. {
  246. vMBMasterGetPDUSndBuf(&ucMBFrame);
  247. vMBMasterSetDestAddress(ucSndAddr);
  248. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
  249. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
  250. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
  251. ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
  252. ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
  253. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
  254. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  255. eErrStatus = eMBMasterWaitRequestFinish( );
  256. }
  257. return eErrStatus;
  258. }
  259. eMBException
  260. eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  261. {
  262. UCHAR *ucMBFrame;
  263. USHORT usRegAddress;
  264. USHORT usRegCount;
  265. eMBException eStatus = MB_EX_NONE;
  266. eMBErrorCode eRegStatus;
  267. /* If this request is broadcast, and it's read mode. This request don't need execute. */
  268. if ( xMBMasterRequestIsBroadcast() )
  269. {
  270. eStatus = MB_EX_NONE;
  271. }
  272. else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
  273. {
  274. vMBMasterGetPDUSndBuf(&ucMBFrame);
  275. usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
  276. usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
  277. usRegAddress++;
  278. usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 );
  279. usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] );
  280. /* Check if the number of registers to read is valid. If not
  281. * return Modbus illegal data value exception.
  282. */
  283. if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) )
  284. {
  285. /* Make callback to fill the buffer. */
  286. eRegStatus = eMBMasterRegHoldingCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount, MB_REG_READ );
  287. /* If an error occured convert it into a Modbus exception. */
  288. if( eRegStatus != MB_ENOERR )
  289. {
  290. eStatus = prveMBError2Exception( eRegStatus );
  291. }
  292. }
  293. else
  294. {
  295. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  296. }
  297. }
  298. else
  299. {
  300. /* Can't be a valid request because the length is incorrect. */
  301. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  302. }
  303. return eStatus;
  304. }
  305. #endif
  306. #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
  307. /**
  308. * This function will request read and write holding register.
  309. *
  310. * @param ucSndAddr salve address
  311. * @param usReadRegAddr read register start address
  312. * @param usNReadRegs read register total number
  313. * @param pusDataBuffer data to be written
  314. * @param usWriteRegAddr write register start address
  315. * @param usNWriteRegs write register total number
  316. * @param lTimeOut timeout (-1 will waiting forever)
  317. *
  318. * @return error code
  319. */
  320. eMBMasterReqErrCode
  321. eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
  322. USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
  323. USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut )
  324. {
  325. UCHAR *ucMBFrame;
  326. USHORT usRegIndex = 0;
  327. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  328. if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  329. else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
  330. else
  331. {
  332. vMBMasterGetPDUSndBuf(&ucMBFrame);
  333. vMBMasterSetDestAddress(ucSndAddr);
  334. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
  335. ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8;
  336. ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr;
  337. ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8;
  338. ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ;
  339. ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8;
  340. ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr;
  341. ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8;
  342. ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ;
  343. ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2;
  344. ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF;
  345. while( usNWriteRegs > usRegIndex)
  346. {
  347. *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
  348. *ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
  349. }
  350. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs );
  351. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  352. eErrStatus = eMBMasterWaitRequestFinish( );
  353. }
  354. return eErrStatus;
  355. }
  356. eMBException
  357. eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  358. {
  359. USHORT usRegReadAddress;
  360. USHORT usRegReadCount;
  361. USHORT usRegWriteAddress;
  362. USHORT usRegWriteCount;
  363. UCHAR *ucMBFrame;
  364. eMBException eStatus = MB_EX_NONE;
  365. eMBErrorCode eRegStatus;
  366. /* If this request is broadcast, and it's read mode. This request don't need execute. */
  367. if ( xMBMasterRequestIsBroadcast() )
  368. {
  369. eStatus = MB_EX_NONE;
  370. }
  371. else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN )
  372. {
  373. vMBMasterGetPDUSndBuf(&ucMBFrame);
  374. usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U );
  375. usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] );
  376. usRegReadAddress++;
  377. usRegReadCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] << 8U );
  378. usRegReadCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] );
  379. usRegWriteAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] << 8U );
  380. usRegWriteAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] );
  381. usRegWriteAddress++;
  382. usRegWriteCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] << 8U );
  383. usRegWriteCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] );
  384. if( ( 2 * usRegReadCount ) == pucFrame[MB_PDU_FUNC_READWRITE_READ_BYTECNT_OFF] )
  385. {
  386. /* Make callback to update the register values. */
  387. eRegStatus = eMBMasterRegHoldingCB( &ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF],
  388. usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
  389. if( eRegStatus == MB_ENOERR )
  390. {
  391. /* Make the read callback. */
  392. eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF],
  393. usRegReadAddress, usRegReadCount, MB_REG_READ);
  394. }
  395. if( eRegStatus != MB_ENOERR )
  396. {
  397. eStatus = prveMBError2Exception( eRegStatus );
  398. }
  399. }
  400. else
  401. {
  402. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  403. }
  404. }
  405. return eStatus;
  406. }
  407. #endif
  408. #endif