mbfuncholding.c 12 KB

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