mbfunccoils.c 9.7 KB

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