mbfunccoils.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 )
  41. #define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  42. #define MB_PDU_FUNC_READ_SIZE ( 4 )
  43. #define MB_PDU_FUNC_READ_COILCNT_MAX ( 0x07D0 )
  44. #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
  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 )
  48. #define MB_PDU_FUNC_WRITE_MUL_COILCNT_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_COILCNT_MAX ( 0x07B0 )
  53. /* ----------------------- Static functions ---------------------------------*/
  54. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  55. /* ----------------------- Start implementation -----------------------------*/
  56. #if MB_FUNC_READ_COILS_ENABLED > 0
  57. eMBException
  58. eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
  59. {
  60. USHORT usRegAddress;
  61. USHORT usCoilCount;
  62. UCHAR ucNBytes;
  63. UCHAR *pucFrameCur;
  64. eMBException eStatus = MB_EX_NONE;
  65. eMBErrorCode eRegStatus;
  66. if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
  67. {
  68. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
  69. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
  70. usRegAddress++;
  71. usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
  72. usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
  73. /* Check if the number of registers to read is valid. If not
  74. * return Modbus illegal data value exception.
  75. */
  76. if( ( usCoilCount >= 1 ) &&
  77. ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
  78. {
  79. /* Set the current PDU data pointer to the beginning. */
  80. pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
  81. *usLen = MB_PDU_FUNC_OFF;
  82. /* First byte contains the function code. */
  83. *pucFrameCur++ = MB_FUNC_READ_COILS;
  84. *usLen += 1;
  85. /* Test if the quantity of coils is a multiple of 8. If not last
  86. * byte is only partially field with unused coils set to zero. */
  87. if( ( usCoilCount & 0x0007 ) != 0 )
  88. {
  89. ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
  90. }
  91. else
  92. {
  93. ucNBytes = ( UCHAR )( usCoilCount / 8 );
  94. }
  95. *pucFrameCur++ = ucNBytes;
  96. *usLen += 1;
  97. eRegStatus =
  98. eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
  99. MB_REG_READ );
  100. /* If an error occured convert it into a Modbus exception. */
  101. if( eRegStatus != MB_ENOERR )
  102. {
  103. eStatus = prveMBError2Exception( eRegStatus );
  104. }
  105. else
  106. {
  107. /* The response contains the function code, the starting address
  108. * and the quantity of registers. We reuse the old values in the
  109. * buffer because they are still valid. */
  110. *usLen += ucNBytes;;
  111. }
  112. }
  113. else
  114. {
  115. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  116. }
  117. }
  118. else
  119. {
  120. /* Can't be a valid read coil register request because the length
  121. * is incorrect. */
  122. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  123. }
  124. return eStatus;
  125. }
  126. #if MB_FUNC_WRITE_COIL_ENABLED > 0
  127. eMBException
  128. eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
  129. {
  130. USHORT usRegAddress;
  131. UCHAR ucBuf[2];
  132. eMBException eStatus = MB_EX_NONE;
  133. eMBErrorCode eRegStatus;
  134. if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
  135. {
  136. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
  137. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
  138. usRegAddress++;
  139. if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
  140. ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
  141. ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
  142. {
  143. ucBuf[1] = 0;
  144. if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
  145. {
  146. ucBuf[0] = 1;
  147. }
  148. else
  149. {
  150. ucBuf[0] = 0;
  151. }
  152. eRegStatus =
  153. eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
  154. /* If an error occured convert it into a Modbus exception. */
  155. if( eRegStatus != MB_ENOERR )
  156. {
  157. eStatus = prveMBError2Exception( eRegStatus );
  158. }
  159. }
  160. else
  161. {
  162. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  163. }
  164. }
  165. else
  166. {
  167. /* Can't be a valid write coil register request because the length
  168. * is incorrect. */
  169. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  170. }
  171. return eStatus;
  172. }
  173. #endif
  174. #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
  175. eMBException
  176. eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
  177. {
  178. USHORT usRegAddress;
  179. USHORT usCoilCnt;
  180. UCHAR ucByteCount;
  181. UCHAR ucByteCountVerify;
  182. eMBException eStatus = MB_EX_NONE;
  183. eMBErrorCode eRegStatus;
  184. if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
  185. {
  186. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
  187. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
  188. usRegAddress++;
  189. usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
  190. usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
  191. ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
  192. /* Compute the number of expected bytes in the request. */
  193. if( ( usCoilCnt & 0x0007 ) != 0 )
  194. {
  195. ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
  196. }
  197. else
  198. {
  199. ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
  200. }
  201. if( ( usCoilCnt >= 1 ) &&
  202. ( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
  203. ( ucByteCountVerify == ucByteCount ) )
  204. {
  205. eRegStatus =
  206. eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
  207. usRegAddress, usCoilCnt, MB_REG_WRITE );
  208. /* If an error occured convert it into a Modbus exception. */
  209. if( eRegStatus != MB_ENOERR )
  210. {
  211. eStatus = prveMBError2Exception( eRegStatus );
  212. }
  213. else
  214. {
  215. /* The response contains the function code, the starting address
  216. * and the quantity of registers. We reuse the old values in the
  217. * buffer because they are still valid. */
  218. *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
  219. }
  220. }
  221. else
  222. {
  223. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  224. }
  225. }
  226. else
  227. {
  228. /* Can't be a valid write coil register request because the length
  229. * is incorrect. */
  230. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  231. }
  232. return eStatus;
  233. }
  234. #endif
  235. #endif