mbfuncholding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
  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. */
  29. /* ----------------------- System includes ----------------------------------*/
  30. #include "stdlib.h"
  31. #include "string.h"
  32. /* ----------------------- Platform includes --------------------------------*/
  33. #include "port.h"
  34. /* ----------------------- Modbus includes ----------------------------------*/
  35. #include "mb.h"
  36. #include "mbframe.h"
  37. #include "mbproto.h"
  38. #include "mbconfig.h"
  39. /* ----------------------- Defines ------------------------------------------*/
  40. #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
  41. #define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  42. #define MB_PDU_FUNC_READ_SIZE ( 4 )
  43. #define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
  44. #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
  45. #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
  46. #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
  47. #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
  48. #define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  49. #define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
  50. #define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
  51. #define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
  52. #define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ( 0x0078 )
  53. #define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
  54. #define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  55. #define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
  56. #define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
  57. #define MB_PDU_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
  58. #define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
  59. #define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 )
  60. /* ----------------------- Static functions ---------------------------------*/
  61. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  62. /* ----------------------- Start implementation -----------------------------*/
  63. #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
  64. eMBException
  65. eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  66. {
  67. USHORT usRegAddress;
  68. eMBException eStatus = MB_EX_NONE;
  69. eMBErrorCode eRegStatus;
  70. if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
  71. {
  72. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
  73. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
  74. usRegAddress++;
  75. /* Make callback to update the value. */
  76. eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
  77. usRegAddress, 1, MB_REG_WRITE );
  78. /* If an error occured convert it into a Modbus exception. */
  79. if( eRegStatus != MB_ENOERR )
  80. {
  81. eStatus = prveMBError2Exception( eRegStatus );
  82. }
  83. }
  84. else
  85. {
  86. /* Can't be a valid request because the length is incorrect. */
  87. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  88. }
  89. return eStatus;
  90. }
  91. #endif
  92. #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
  93. eMBException
  94. eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  95. {
  96. USHORT usRegAddress;
  97. USHORT usRegCount;
  98. UCHAR ucRegByteCount;
  99. eMBException eStatus = MB_EX_NONE;
  100. eMBErrorCode eRegStatus;
  101. if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) )
  102. {
  103. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
  104. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
  105. usRegAddress++;
  106. usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
  107. usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
  108. ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
  109. if( ( usRegCount >= 1 ) &&
  110. ( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
  111. ( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
  112. {
  113. /* Make callback to update the register values. */
  114. eRegStatus =
  115. eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
  116. usRegAddress, usRegCount, MB_REG_WRITE );
  117. /* If an error occured convert it into a Modbus exception. */
  118. if( eRegStatus != MB_ENOERR )
  119. {
  120. eStatus = prveMBError2Exception( eRegStatus );
  121. }
  122. else
  123. {
  124. /* The response contains the function code, the starting
  125. * address and the quantity of registers. We reuse the
  126. * old values in the buffer because they are still valid.
  127. */
  128. *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
  129. }
  130. }
  131. else
  132. {
  133. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  134. }
  135. }
  136. else
  137. {
  138. /* Can't be a valid request because the length is incorrect. */
  139. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  140. }
  141. return eStatus;
  142. }
  143. #endif
  144. #if MB_FUNC_READ_HOLDING_ENABLED > 0
  145. eMBException
  146. eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  147. {
  148. USHORT usRegAddress;
  149. USHORT usRegCount;
  150. UCHAR *pucFrameCur;
  151. eMBException eStatus = MB_EX_NONE;
  152. eMBErrorCode eRegStatus;
  153. if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
  154. {
  155. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
  156. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
  157. usRegAddress++;
  158. usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
  159. usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
  160. /* Check if the number of registers to read is valid. If not
  161. * return Modbus illegal data value exception.
  162. */
  163. if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
  164. {
  165. /* Set the current PDU data pointer to the beginning. */
  166. pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
  167. *usLen = MB_PDU_FUNC_OFF;
  168. /* First byte contains the function code. */
  169. *pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
  170. *usLen += 1;
  171. /* Second byte in the response contain the number of bytes. */
  172. *pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
  173. *usLen += 1;
  174. /* Make callback to fill the buffer. */
  175. eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
  176. /* If an error occured convert it into a Modbus exception. */
  177. if( eRegStatus != MB_ENOERR )
  178. {
  179. eStatus = prveMBError2Exception( eRegStatus );
  180. }
  181. else
  182. {
  183. *usLen += usRegCount * 2;
  184. }
  185. }
  186. else
  187. {
  188. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  189. }
  190. }
  191. else
  192. {
  193. /* Can't be a valid request because the length is incorrect. */
  194. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  195. }
  196. return eStatus;
  197. }
  198. #endif
  199. #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
  200. eMBException
  201. eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
  202. {
  203. USHORT usRegReadAddress;
  204. USHORT usRegReadCount;
  205. USHORT usRegWriteAddress;
  206. USHORT usRegWriteCount;
  207. UCHAR ucRegWriteByteCount;
  208. UCHAR *pucFrameCur;
  209. eMBException eStatus = MB_EX_NONE;
  210. eMBErrorCode eRegStatus;
  211. if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
  212. {
  213. usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
  214. usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
  215. usRegReadAddress++;
  216. usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
  217. usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
  218. usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
  219. usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
  220. usRegWriteAddress++;
  221. usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
  222. usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
  223. ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
  224. if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
  225. ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
  226. ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
  227. {
  228. /* Make callback to update the register values. */
  229. eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
  230. usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
  231. if( eRegStatus == MB_ENOERR )
  232. {
  233. /* Set the current PDU data pointer to the beginning. */
  234. pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
  235. *usLen = MB_PDU_FUNC_OFF;
  236. /* First byte contains the function code. */
  237. *pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
  238. *usLen += 1;
  239. /* Second byte in the response contain the number of bytes. */
  240. *pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
  241. *usLen += 1;
  242. /* Make the read callback. */
  243. eRegStatus =
  244. eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
  245. if( eRegStatus == MB_ENOERR )
  246. {
  247. *usLen += 2 * usRegReadCount;
  248. }
  249. }
  250. if( eRegStatus != MB_ENOERR )
  251. {
  252. eStatus = prveMBError2Exception( eRegStatus );
  253. }
  254. }
  255. else
  256. {
  257. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  258. }
  259. }
  260. return eStatus;
  261. }
  262. #endif