mb_m.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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: mbrtu_m.c,v 1.60 2013/08/20 11:18:10 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 "mbconfig.h"
  39. #include "mbframe.h"
  40. #include "mbproto.h"
  41. #include "mbfunc.h"
  42. #include "mbport.h"
  43. #if MB_MASTER_RTU_ENABLED == 1
  44. #include "mbrtu.h"
  45. #endif
  46. #if MB_MASTER_ASCII_ENABLED == 1
  47. #include "mbascii.h"
  48. #endif
  49. #if MB_MASTER_TCP_ENABLED == 1
  50. #include "mbtcp.h"
  51. #endif
  52. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
  53. #ifndef MB_PORT_HAS_CLOSE
  54. #define MB_PORT_HAS_CLOSE 0
  55. #endif
  56. /* ----------------------- Static variables ---------------------------------*/
  57. static UCHAR ucMBMasterDestAddress;
  58. static BOOL xMBRunInMasterMode = FALSE;
  59. static BOOL xMasterIsBusy = FALSE;
  60. static enum
  61. {
  62. STATE_ENABLED,
  63. STATE_DISABLED,
  64. STATE_NOT_INITIALIZED
  65. } eMBState = STATE_NOT_INITIALIZED;
  66. /* Functions pointer which are initialized in eMBInit( ). Depending on the
  67. * mode (RTU or ASCII) the are set to the correct implementations.
  68. * Using for Modbus Master,Add by Armink 20130813
  69. */
  70. static peMBFrameSend peMBMasterFrameSendCur;
  71. static pvMBFrameStart pvMBMasterFrameStartCur;
  72. static pvMBFrameStop pvMBMasterFrameStopCur;
  73. static peMBFrameReceive peMBMasterFrameReceiveCur;
  74. static pvMBFrameClose pvMBMasterFrameCloseCur;
  75. /* Callback functions required by the porting layer. They are called when
  76. * an external event has happend which includes a timeout or the reception
  77. * or transmission of a character.
  78. * Using for Modbus Master,Add by Armink 20130813
  79. */
  80. BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
  81. BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
  82. BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
  83. BOOL( *pxMBMasterFrameCBReceiveFSMCur ) ( void );
  84. BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
  85. /* An array of Modbus functions handlers which associates Modbus function
  86. * codes with implementing functions.
  87. */
  88. static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
  89. #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
  90. //TODO Add Master function define
  91. {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
  92. #endif
  93. #if MB_FUNC_READ_INPUT_ENABLED > 0
  94. {MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
  95. #endif
  96. #if MB_FUNC_READ_HOLDING_ENABLED > 0
  97. {MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister},
  98. #endif
  99. #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
  100. {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister},
  101. #endif
  102. #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
  103. {MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister},
  104. #endif
  105. #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
  106. {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBMasterFuncReadWriteMultipleHoldingRegister},
  107. #endif
  108. #if MB_FUNC_READ_COILS_ENABLED > 0
  109. {MB_FUNC_READ_COILS, eMBMasterFuncReadCoils},
  110. #endif
  111. #if MB_FUNC_WRITE_COIL_ENABLED > 0
  112. {MB_FUNC_WRITE_SINGLE_COIL, eMBMasterFuncWriteCoil},
  113. #endif
  114. #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
  115. {MB_FUNC_WRITE_MULTIPLE_COILS, eMBMasterFuncWriteMultipleCoils},
  116. #endif
  117. #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
  118. {MB_FUNC_READ_DISCRETE_INPUTS, eMBMasterFuncReadDiscreteInputs},
  119. #endif
  120. };
  121. /* ----------------------- Start implementation -----------------------------*/
  122. eMBErrorCode
  123. eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
  124. {
  125. eMBErrorCode eStatus = MB_ENOERR;
  126. switch (eMode)
  127. {
  128. #if MB_MASTER_RTU_ENABLED > 0
  129. case MB_RTU:
  130. pvMBMasterFrameStartCur = eMBMasterRTUStart;
  131. pvMBMasterFrameStopCur = eMBMasterRTUStop;
  132. peMBMasterFrameSendCur = eMBMasterRTUSend;
  133. peMBMasterFrameReceiveCur = eMBMasterRTUReceive;
  134. pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
  135. pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM;
  136. pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM;
  137. pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired;
  138. eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity);
  139. break;
  140. #endif
  141. #if MB_MASTER_ASCII_ENABLED > 0
  142. case MB_ASCII:
  143. pvMBMasterFrameStartCur = eMBMasterASCIIStart;
  144. pvMBMasterFrameStopCur = eMBMasterASCIIStop;
  145. peMBMasterFrameSendCur = eMBMasterASCIISend;
  146. peMBMasterFrameReceiveCur = eMBMasterASCIIReceive;
  147. pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
  148. pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM;
  149. pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM;
  150. pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired;
  151. eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity );
  152. break;
  153. #endif
  154. default:
  155. eStatus = MB_EINVAL;
  156. break;
  157. }
  158. if (eStatus == MB_ENOERR)
  159. {
  160. if (!xMBMasterPortEventInit())
  161. {
  162. /* port dependent event module initalization failed. */
  163. eStatus = MB_EPORTERR;
  164. }
  165. else
  166. {
  167. eMBState = STATE_DISABLED;
  168. }
  169. }
  170. return eStatus;
  171. }
  172. eMBErrorCode
  173. eMBMasterClose( void )
  174. {
  175. eMBErrorCode eStatus = MB_ENOERR;
  176. if( eMBState == STATE_DISABLED )
  177. {
  178. if( pvMBMasterFrameCloseCur != NULL )
  179. {
  180. pvMBMasterFrameCloseCur( );
  181. }
  182. }
  183. else
  184. {
  185. eStatus = MB_EILLSTATE;
  186. }
  187. return eStatus;
  188. }
  189. eMBErrorCode
  190. eMBMasterEnable( void )
  191. {
  192. eMBErrorCode eStatus = MB_ENOERR;
  193. if( eMBState == STATE_DISABLED )
  194. {
  195. /* Activate the protocol stack. */
  196. pvMBMasterFrameStartCur( );
  197. eMBState = STATE_ENABLED;
  198. }
  199. else
  200. {
  201. eStatus = MB_EILLSTATE;
  202. }
  203. return eStatus;
  204. }
  205. eMBErrorCode
  206. eMBMasterDisable( void )
  207. {
  208. eMBErrorCode eStatus;
  209. if( eMBState == STATE_ENABLED )
  210. {
  211. pvMBMasterFrameStopCur( );
  212. eMBState = STATE_DISABLED;
  213. eStatus = MB_ENOERR;
  214. }
  215. else if( eMBState == STATE_DISABLED )
  216. {
  217. eStatus = MB_ENOERR;
  218. }
  219. else
  220. {
  221. eStatus = MB_EILLSTATE;
  222. }
  223. return eStatus;
  224. }
  225. eMBErrorCode
  226. eMBMasterPoll( void )
  227. {
  228. static UCHAR *ucMBFrame;
  229. static UCHAR ucRcvAddress;
  230. static UCHAR ucFunctionCode;
  231. static USHORT usLength;
  232. static eMBException eException;
  233. int i;
  234. eMBErrorCode eStatus = MB_ENOERR;
  235. eMBMasterEventType eEvent;
  236. /* Check if the protocol stack is ready. */
  237. if( eMBState != STATE_ENABLED )
  238. {
  239. return MB_EILLSTATE;
  240. }
  241. /* Check if there is a event available. If not return control to caller.
  242. * Otherwise we will handle the event. */
  243. if( xMBMasterPortEventGet( &eEvent ) == TRUE )
  244. {
  245. switch ( eEvent )
  246. {
  247. case EV_MASTER_READY:
  248. break;
  249. case EV_MASTER_FRAME_RECEIVED:
  250. eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
  251. /* Check if the frame is for us. If not ,send an error process event. */
  252. if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
  253. {
  254. ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
  255. }
  256. else
  257. {
  258. ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
  259. }
  260. break;
  261. case EV_MASTER_EXECUTE:
  262. ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
  263. eException = MB_EX_ILLEGAL_FUNCTION;
  264. for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
  265. {
  266. /* No more function handlers registered. Abort. */
  267. if( xMasterFuncHandlers[i].ucFunctionCode == 0 )
  268. {
  269. break;
  270. }
  271. else if( xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode )
  272. {
  273. vMBMasterSetCBRunInMasterMode(TRUE);
  274. eException = xMasterFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
  275. vMBMasterSetCBRunInMasterMode(FALSE);
  276. break;
  277. }
  278. }
  279. /* If receive frame has exception .The receive function code highest bit is 1.*/
  280. if(ucFunctionCode >> 7) eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
  281. /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
  282. if (eException != MB_EX_NONE) ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
  283. else vMBMasterSetIsBusy( FALSE );
  284. break;
  285. case EV_MASTER_FRAME_SENT:
  286. /* Master is busy now. */
  287. vMBMasterSetIsBusy( TRUE );
  288. vMBMasterGetPDUSndBuf( &ucMBFrame );
  289. eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, ucMBMasterGetPDUSndLength() );
  290. break;
  291. case EV_MASTER_ERROR_PROCESS:
  292. vMBMasterSetIsBusy( FALSE );
  293. break;
  294. }
  295. }
  296. return MB_ENOERR;
  297. }
  298. /* Get whether the Modbus Master is busy.*/
  299. BOOL xMBMasterGetIsBusy( void )
  300. {
  301. return xMasterIsBusy;
  302. }
  303. /* Set whether the Modbus Master is busy.*/
  304. void vMBMasterSetIsBusy( BOOL IsBusy )
  305. {
  306. xMasterIsBusy = IsBusy;
  307. }
  308. /* Get whether the Modbus Master is run in master mode.*/
  309. BOOL xMBMasterGetCBRunInMasterMode( void )
  310. {
  311. return xMBRunInMasterMode;
  312. }
  313. /* Set whether the Modbus Master is run in master mode.*/
  314. void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
  315. {
  316. xMBRunInMasterMode = IsMasterMode;
  317. }
  318. /* Get Modbus Master send destination address*/
  319. UCHAR ucMBMasterGetDestAddress( void )
  320. {
  321. return ucMBMasterDestAddress;
  322. }
  323. /* Set Modbus Master send destination address*/
  324. void vMBMasterSetDestAddress( UCHAR Address )
  325. {
  326. ucMBMasterDestAddress = Address;
  327. }
  328. #endif