mbfunccoils_m.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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: mbfunccoils_m.c,v 1.60 2013/10/12 15:10:12 Armink Add Master Functions
  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_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  44. #define MB_PDU_REQ_READ_SIZE ( 4 )
  45. #define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 0 )
  46. #define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
  47. #define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
  48. #define MB_PDU_REQ_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
  49. #define MB_PDU_REQ_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
  50. #define MB_PDU_REQ_WRITE_SIZE ( 4 )
  51. #define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
  52. #define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
  53. #define MB_PDU_FUNC_WRITE_SIZE ( 4 )
  54. #define MB_PDU_REQ_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
  55. #define MB_PDU_REQ_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  56. #define MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
  57. #define MB_PDU_REQ_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
  58. #define MB_PDU_REQ_WRITE_MUL_SIZE_MIN ( 5 )
  59. #define MB_PDU_REQ_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
  60. #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
  61. #define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  62. #define MB_PDU_FUNC_WRITE_MUL_SIZE ( 5 )
  63. /* ----------------------- Static functions ---------------------------------*/
  64. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  65. /* ----------------------- Start implementation -----------------------------*/
  66. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
  67. #if MB_FUNC_READ_COILS_ENABLED > 0
  68. eMBMasterReqErrCode
  69. eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils )
  70. {
  71. UCHAR *ucMBFrame;
  72. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  73. if ( xMBMasterGetIsBusy() ) eErrStatus = MB_MRE_MASTER_BUSY;
  74. else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  75. else
  76. {
  77. vMBMasterGetPDUSndBuf(&ucMBFrame);
  78. vMBMasterSetDestAddress(ucSndAddr);
  79. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_COILS;
  80. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usCoilAddr >> 8;
  81. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usCoilAddr;
  82. ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8;
  83. ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils;
  84. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
  85. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  86. }
  87. return eErrStatus;
  88. }
  89. eMBException
  90. eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
  91. {
  92. UCHAR *ucMBFrame;
  93. USHORT usRegAddress;
  94. USHORT usCoilCount;
  95. UCHAR ucByteCount;
  96. eMBException eStatus = MB_EX_NONE;
  97. eMBErrorCode eRegStatus;
  98. if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
  99. {
  100. vMBMasterGetPDUSndBuf(&ucMBFrame);
  101. usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
  102. usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
  103. usRegAddress++;
  104. usCoilCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF] << 8 );
  105. usCoilCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] );
  106. /* Test if the quantity of coils is a multiple of 8. If not last
  107. * byte is only partially field with unused coils set to zero. */
  108. if( ( usCoilCount & 0x0007 ) != 0 )
  109. {
  110. ucByteCount = ( UCHAR )( usCoilCount / 8 + 1 );
  111. }
  112. else
  113. {
  114. ucByteCount = ( UCHAR )( usCoilCount / 8 );
  115. }
  116. /* Check if the number of registers to read is valid. If not
  117. * return Modbus illegal data value exception.
  118. */
  119. if( ( usCoilCount >= 1 ) &&
  120. ( ucByteCount == pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] ) )
  121. {
  122. /* Make callback to fill the buffer. */
  123. eRegStatus = eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usCoilCount, MB_REG_READ );
  124. /* If an error occured convert it into a Modbus exception. */
  125. if( eRegStatus != MB_ENOERR )
  126. {
  127. eStatus = prveMBError2Exception( eRegStatus );
  128. }
  129. }
  130. else
  131. {
  132. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  133. }
  134. }
  135. else
  136. {
  137. /* Can't be a valid read coil register request because the length
  138. * is incorrect. */
  139. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  140. }
  141. return eStatus;
  142. }
  143. #endif
  144. #if MB_FUNC_WRITE_COIL_ENABLED > 0
  145. eMBMasterReqErrCode
  146. eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData )
  147. {
  148. UCHAR *ucMBFrame;
  149. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  150. if ( xMBMasterGetIsBusy() ) eErrStatus = MB_MRE_MASTER_BUSY;
  151. else if ( usCoilAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  152. else if ( ( usCoilData != 0xFF00 ) && ( usCoilData != 0x0000 ) ) eErrStatus = MB_MRE_ILL_ARG;
  153. else
  154. {
  155. vMBMasterGetPDUSndBuf(&ucMBFrame);
  156. vMBMasterSetDestAddress(ucSndAddr);
  157. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_SINGLE_COIL;
  158. ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usCoilAddr >> 8;
  159. ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usCoilAddr;
  160. ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8;
  161. ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData;
  162. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
  163. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  164. }
  165. return eErrStatus;
  166. }
  167. eMBException
  168. eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
  169. {
  170. USHORT usRegAddress;
  171. UCHAR ucBuf[2];
  172. eMBException eStatus = MB_EX_NONE;
  173. eMBErrorCode eRegStatus;
  174. if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
  175. {
  176. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
  177. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
  178. usRegAddress++;
  179. if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
  180. ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
  181. ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
  182. {
  183. ucBuf[1] = 0;
  184. if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
  185. {
  186. ucBuf[0] = 1;
  187. }
  188. else
  189. {
  190. ucBuf[0] = 0;
  191. }
  192. eRegStatus =
  193. eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
  194. /* If an error occured convert it into a Modbus exception. */
  195. if( eRegStatus != MB_ENOERR )
  196. {
  197. eStatus = prveMBError2Exception( eRegStatus );
  198. }
  199. }
  200. else
  201. {
  202. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  203. }
  204. }
  205. else
  206. {
  207. /* Can't be a valid write coil register request because the length
  208. * is incorrect. */
  209. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  210. }
  211. return eStatus;
  212. }
  213. #endif
  214. #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
  215. eMBMasterReqErrCode
  216. eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
  217. USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer )
  218. {
  219. UCHAR *ucMBFrame;
  220. USHORT usRegIndex = 0;
  221. UCHAR ucByteCount;
  222. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  223. if ( xMBMasterGetIsBusy() ) eErrStatus = MB_MRE_MASTER_BUSY;
  224. else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  225. else if ( usNCoils > MB_PDU_REQ_WRITE_MUL_COILCNT_MAX ) eErrStatus = MB_MRE_ILL_ARG;
  226. else
  227. {
  228. vMBMasterGetPDUSndBuf(&ucMBFrame);
  229. vMBMasterSetDestAddress(ucSndAddr);
  230. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_COILS;
  231. ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usCoilAddr >> 8;
  232. ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usCoilAddr;
  233. ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF] = usNCoils >> 8;
  234. ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF + 1] = usNCoils ;
  235. if( ( usNCoils & 0x0007 ) != 0 )
  236. {
  237. ucByteCount = ( UCHAR )( usNCoils / 8 + 1 );
  238. }
  239. else
  240. {
  241. ucByteCount = ( UCHAR )( usNCoils / 8 );
  242. }
  243. ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = ucByteCount;
  244. ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
  245. while( ucByteCount > usRegIndex)
  246. {
  247. *ucMBFrame++ = pucDataBuffer[usRegIndex++];
  248. }
  249. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount );
  250. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  251. }
  252. return eErrStatus;
  253. }
  254. eMBException
  255. eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
  256. {
  257. USHORT usRegAddress;
  258. USHORT usCoilCnt;
  259. UCHAR ucByteCount;
  260. UCHAR ucByteCountVerify;
  261. UCHAR *ucMBFrame;
  262. eMBException eStatus = MB_EX_NONE;
  263. eMBErrorCode eRegStatus;
  264. if( *usLen == MB_PDU_FUNC_WRITE_MUL_SIZE )
  265. {
  266. vMBMasterGetPDUSndBuf(&ucMBFrame);
  267. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
  268. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
  269. usRegAddress++;
  270. usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
  271. usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
  272. ucByteCount = ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF];
  273. /* Compute the number of expected bytes in the request. */
  274. if( ( usCoilCnt & 0x0007 ) != 0 )
  275. {
  276. ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
  277. }
  278. else
  279. {
  280. ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
  281. }
  282. if( ( usCoilCnt >= 1 ) && ( ucByteCountVerify == ucByteCount ) )
  283. {
  284. eRegStatus =
  285. eMBRegCoilsCB( &ucMBFrame[MB_PDU_REQ_WRITE_MUL_VALUES_OFF],
  286. usRegAddress, usCoilCnt, MB_REG_WRITE );
  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 write coil register request because the length
  301. * is incorrect. */
  302. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  303. }
  304. return eStatus;
  305. }
  306. #endif
  307. #endif