mb_m.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 eMBMasterErrorEventType eMBMasterCurErrorType;
  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. /* initialize the OS resource for modbus master. */
  170. vMBMasterOsResInit();
  171. }
  172. return eStatus;
  173. }
  174. eMBErrorCode
  175. eMBMasterClose( void )
  176. {
  177. eMBErrorCode eStatus = MB_ENOERR;
  178. if( eMBState == STATE_DISABLED )
  179. {
  180. if( pvMBMasterFrameCloseCur != NULL )
  181. {
  182. pvMBMasterFrameCloseCur( );
  183. }
  184. }
  185. else
  186. {
  187. eStatus = MB_EILLSTATE;
  188. }
  189. return eStatus;
  190. }
  191. eMBErrorCode
  192. eMBMasterEnable( void )
  193. {
  194. eMBErrorCode eStatus = MB_ENOERR;
  195. if( eMBState == STATE_DISABLED )
  196. {
  197. /* Activate the protocol stack. */
  198. pvMBMasterFrameStartCur( );
  199. eMBState = STATE_ENABLED;
  200. }
  201. else
  202. {
  203. eStatus = MB_EILLSTATE;
  204. }
  205. return eStatus;
  206. }
  207. eMBErrorCode
  208. eMBMasterDisable( void )
  209. {
  210. eMBErrorCode eStatus;
  211. if( eMBState == STATE_ENABLED )
  212. {
  213. pvMBMasterFrameStopCur( );
  214. eMBState = STATE_DISABLED;
  215. eStatus = MB_ENOERR;
  216. }
  217. else if( eMBState == STATE_DISABLED )
  218. {
  219. eStatus = MB_ENOERR;
  220. }
  221. else
  222. {
  223. eStatus = MB_EILLSTATE;
  224. }
  225. return eStatus;
  226. }
  227. eMBErrorCode
  228. eMBMasterPoll( void )
  229. {
  230. static UCHAR *ucMBFrame;
  231. static UCHAR ucRcvAddress;
  232. static UCHAR ucFunctionCode;
  233. static USHORT usLength;
  234. static eMBException eException;
  235. int i , j;
  236. eMBErrorCode eStatus = MB_ENOERR;
  237. eMBMasterEventType eEvent;
  238. eMBMasterErrorEventType errorType;
  239. /* Check if the protocol stack is ready. */
  240. if( eMBState != STATE_ENABLED )
  241. {
  242. return MB_EILLSTATE;
  243. }
  244. /* Check if there is a event available. If not return control to caller.
  245. * Otherwise we will handle the event. */
  246. if( xMBMasterPortEventGet( &eEvent ) == TRUE )
  247. {
  248. switch ( eEvent )
  249. {
  250. case EV_MASTER_READY:
  251. break;
  252. case EV_MASTER_FRAME_RECEIVED:
  253. eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
  254. /* Check if the frame is for us. If not ,send an error process event. */
  255. if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
  256. {
  257. ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
  258. }
  259. else
  260. {
  261. vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
  262. ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
  263. }
  264. break;
  265. case EV_MASTER_EXECUTE:
  266. ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
  267. eException = MB_EX_ILLEGAL_FUNCTION;
  268. /* If receive frame has exception .The receive function code highest bit is 1.*/
  269. if(ucFunctionCode >> 7) {
  270. eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
  271. }
  272. else
  273. {
  274. for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
  275. {
  276. /* No more function handlers registered. Abort. */
  277. if (xMasterFuncHandlers[i].ucFunctionCode == 0) {
  278. break;
  279. }
  280. else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
  281. vMBMasterSetCBRunInMasterMode(TRUE);
  282. /* If master request is broadcast,
  283. * the master need execute function for all slave.
  284. */
  285. if ( xMBMasterRequestIsBroadcast() ) {
  286. usLength = usMBMasterGetPDUSndLength();
  287. for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){
  288. vMBMasterSetDestAddress(j);
  289. eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
  290. }
  291. }
  292. else {
  293. eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
  294. }
  295. vMBMasterSetCBRunInMasterMode(FALSE);
  296. break;
  297. }
  298. }
  299. }
  300. /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
  301. if (eException != MB_EX_NONE) {
  302. vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
  303. ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
  304. }
  305. else {
  306. vMBMasterCBRequestScuuess( );
  307. vMBMasterRunResRelease( );
  308. }
  309. break;
  310. case EV_MASTER_FRAME_SENT:
  311. /* Master is busy now. */
  312. vMBMasterGetPDUSndBuf( &ucMBFrame );
  313. eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
  314. break;
  315. case EV_MASTER_ERROR_PROCESS:
  316. /* Execute specified error process callback function. */
  317. errorType = eMBMasterGetErrorType();
  318. vMBMasterGetPDUSndBuf( &ucMBFrame );
  319. switch (errorType) {
  320. case EV_ERROR_RESPOND_TIMEOUT:
  321. vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(),
  322. ucMBFrame, usMBMasterGetPDUSndLength());
  323. break;
  324. case EV_ERROR_RECEIVE_DATA:
  325. vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(),
  326. ucMBFrame, usMBMasterGetPDUSndLength());
  327. break;
  328. case EV_ERROR_EXECUTE_FUNCTION:
  329. vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(),
  330. ucMBFrame, usMBMasterGetPDUSndLength());
  331. break;
  332. }
  333. vMBMasterRunResRelease();
  334. break;
  335. }
  336. }
  337. return MB_ENOERR;
  338. }
  339. /* Get whether the Modbus Master is run in master mode.*/
  340. BOOL xMBMasterGetCBRunInMasterMode( void )
  341. {
  342. return xMBRunInMasterMode;
  343. }
  344. /* Set whether the Modbus Master is run in master mode.*/
  345. void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
  346. {
  347. xMBRunInMasterMode = IsMasterMode;
  348. }
  349. /* Get Modbus Master send destination address. */
  350. UCHAR ucMBMasterGetDestAddress( void )
  351. {
  352. return ucMBMasterDestAddress;
  353. }
  354. /* Set Modbus Master send destination address. */
  355. void vMBMasterSetDestAddress( UCHAR Address )
  356. {
  357. ucMBMasterDestAddress = Address;
  358. }
  359. /* Get Modbus Master current error event type. */
  360. eMBMasterErrorEventType eMBMasterGetErrorType( void )
  361. {
  362. return eMBMasterCurErrorType;
  363. }
  364. /* Set Modbus Master current error event type. */
  365. void vMBMasterSetErrorType( eMBMasterErrorEventType errorType )
  366. {
  367. eMBMasterCurErrorType = errorType;
  368. }
  369. #endif