mb_m.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. STATE_ESTABLISHED,
  66. } eMBState = STATE_NOT_INITIALIZED;
  67. /* Functions pointer which are initialized in eMBInit( ). Depending on the
  68. * mode (RTU or ASCII) the are set to the correct implementations.
  69. * Using for Modbus Master,Add by Armink 20130813
  70. */
  71. static peMBFrameSend peMBMasterFrameSendCur;
  72. static pvMBFrameStart pvMBMasterFrameStartCur;
  73. static pvMBFrameStop pvMBMasterFrameStopCur;
  74. static peMBFrameReceive peMBMasterFrameReceiveCur;
  75. static pvMBFrameClose pvMBMasterFrameCloseCur;
  76. /* Callback functions required by the porting layer. They are called when
  77. * an external event has happend which includes a timeout or the reception
  78. * or transmission of a character.
  79. * Using for Modbus Master,Add by Armink 20130813
  80. */
  81. BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
  82. BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
  83. BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
  84. BOOL( *pxMBMasterFrameCBReceiveFSMCur ) ( void );
  85. BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
  86. /* An array of Modbus functions handlers which associates Modbus function
  87. * codes with implementing functions.
  88. */
  89. static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
  90. #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
  91. //TODO Add Master function define
  92. {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
  93. #endif
  94. #if MB_FUNC_READ_INPUT_ENABLED > 0
  95. {MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
  96. #endif
  97. #if MB_FUNC_READ_HOLDING_ENABLED > 0
  98. {MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister},
  99. #endif
  100. #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
  101. {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister},
  102. #endif
  103. #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
  104. {MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister},
  105. #endif
  106. #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
  107. {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBMasterFuncReadWriteMultipleHoldingRegister},
  108. #endif
  109. #if MB_FUNC_READ_COILS_ENABLED > 0
  110. {MB_FUNC_READ_COILS, eMBMasterFuncReadCoils},
  111. #endif
  112. #if MB_FUNC_WRITE_COIL_ENABLED > 0
  113. {MB_FUNC_WRITE_SINGLE_COIL, eMBMasterFuncWriteCoil},
  114. #endif
  115. #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
  116. {MB_FUNC_WRITE_MULTIPLE_COILS, eMBMasterFuncWriteMultipleCoils},
  117. #endif
  118. #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
  119. {MB_FUNC_READ_DISCRETE_INPUTS, eMBMasterFuncReadDiscreteInputs},
  120. #endif
  121. };
  122. /* ----------------------- Start implementation -----------------------------*/
  123. eMBErrorCode
  124. eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
  125. {
  126. eMBErrorCode eStatus = MB_ENOERR;
  127. switch (eMode)
  128. {
  129. #if MB_MASTER_RTU_ENABLED > 0
  130. case MB_RTU:
  131. pvMBMasterFrameStartCur = eMBMasterRTUStart;
  132. pvMBMasterFrameStopCur = eMBMasterRTUStop;
  133. peMBMasterFrameSendCur = eMBMasterRTUSend;
  134. peMBMasterFrameReceiveCur = eMBMasterRTUReceive;
  135. pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
  136. pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM;
  137. pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM;
  138. pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired;
  139. eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity);
  140. break;
  141. #endif
  142. #if MB_MASTER_ASCII_ENABLED > 0
  143. case MB_ASCII:
  144. pvMBMasterFrameStartCur = eMBMasterASCIIStart;
  145. pvMBMasterFrameStopCur = eMBMasterASCIIStop;
  146. peMBMasterFrameSendCur = eMBMasterASCIISend;
  147. peMBMasterFrameReceiveCur = eMBMasterASCIIReceive;
  148. pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
  149. pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM;
  150. pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM;
  151. pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired;
  152. eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity );
  153. break;
  154. #endif
  155. default:
  156. eStatus = MB_EINVAL;
  157. break;
  158. }
  159. if (eStatus == MB_ENOERR)
  160. {
  161. if (!xMBMasterPortEventInit())
  162. {
  163. /* port dependent event module initalization failed. */
  164. eStatus = MB_EPORTERR;
  165. }
  166. else
  167. {
  168. eMBState = STATE_DISABLED;
  169. }
  170. /* initialize the OS resource for modbus master. */
  171. vMBMasterOsResInit();
  172. }
  173. return eStatus;
  174. }
  175. eMBErrorCode
  176. eMBMasterClose( void )
  177. {
  178. eMBErrorCode eStatus = MB_ENOERR;
  179. if( eMBState == STATE_DISABLED )
  180. {
  181. if( pvMBMasterFrameCloseCur != NULL )
  182. {
  183. pvMBMasterFrameCloseCur( );
  184. }
  185. }
  186. else
  187. {
  188. eStatus = MB_EILLSTATE;
  189. }
  190. return eStatus;
  191. }
  192. eMBErrorCode
  193. eMBMasterEnable( void )
  194. {
  195. eMBErrorCode eStatus = MB_ENOERR;
  196. if( eMBState == STATE_DISABLED )
  197. {
  198. /* Activate the protocol stack. */
  199. pvMBMasterFrameStartCur( );
  200. eMBState = STATE_ENABLED;
  201. }
  202. else
  203. {
  204. eStatus = MB_EILLSTATE;
  205. }
  206. xMBMasterPortEventPost( EV_MASTER_FRAME_RECEIVED );
  207. return eStatus;
  208. }
  209. eMBErrorCode
  210. eMBMasterDisable( void )
  211. {
  212. eMBErrorCode eStatus;
  213. if(( eMBState == STATE_ENABLED ) || ( eMBState == STATE_ESTABLISHED))
  214. {
  215. pvMBMasterFrameStopCur( );
  216. eMBState = STATE_DISABLED;
  217. eStatus = MB_ENOERR;
  218. }
  219. else if( eMBState == STATE_DISABLED )
  220. {
  221. eStatus = MB_ENOERR;
  222. }
  223. else
  224. {
  225. eStatus = MB_EILLSTATE;
  226. }
  227. return eStatus;
  228. }
  229. BOOL
  230. eMBMasterIsEstablished( void )
  231. {
  232. if(eMBState == STATE_ESTABLISHED)
  233. {
  234. return TRUE;
  235. }
  236. else
  237. {
  238. return FALSE;
  239. }
  240. }
  241. eMBErrorCode
  242. eMBMasterPoll( void )
  243. {
  244. static UCHAR *ucMBFrame;
  245. static UCHAR ucRcvAddress;
  246. static UCHAR ucFunctionCode;
  247. static USHORT usLength;
  248. static eMBException eException;
  249. int i , j;
  250. eMBErrorCode eStatus = MB_ENOERR;
  251. eMBMasterEventType eEvent;
  252. eMBMasterErrorEventType errorType;
  253. /* Check if the protocol stack is ready. */
  254. if(( eMBState != STATE_ENABLED ) && ( eMBState != STATE_ESTABLISHED))
  255. {
  256. return MB_EILLSTATE;
  257. }
  258. /* Check if there is a event available. If not return control to caller.
  259. * Otherwise we will handle the event. */
  260. if( xMBMasterPortEventGet( &eEvent ) == TRUE )
  261. {
  262. switch ( eEvent )
  263. {
  264. case EV_MASTER_READY:
  265. eMBState = STATE_ESTABLISHED;
  266. break;
  267. case EV_MASTER_FRAME_RECEIVED:
  268. eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
  269. /* Check if the frame is for us. If not ,send an error process event. */
  270. if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
  271. {
  272. ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
  273. }
  274. else
  275. {
  276. vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
  277. ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
  278. }
  279. break;
  280. case EV_MASTER_EXECUTE:
  281. ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
  282. eException = MB_EX_ILLEGAL_FUNCTION;
  283. /* If receive frame has exception .The receive function code highest bit is 1.*/
  284. if(ucFunctionCode >> 7) {
  285. eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
  286. }
  287. else
  288. {
  289. for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
  290. {
  291. /* No more function handlers registered. Abort. */
  292. if (xMasterFuncHandlers[i].ucFunctionCode == 0) {
  293. break;
  294. }
  295. else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
  296. vMBMasterSetCBRunInMasterMode(TRUE);
  297. /* If master request is broadcast,
  298. * the master need execute function for all slave.
  299. */
  300. if ( xMBMasterRequestIsBroadcast() ) {
  301. usLength = usMBMasterGetPDUSndLength();
  302. for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){
  303. vMBMasterSetDestAddress(j);
  304. eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
  305. }
  306. }
  307. else {
  308. eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
  309. }
  310. vMBMasterSetCBRunInMasterMode(FALSE);
  311. break;
  312. }
  313. }
  314. }
  315. /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
  316. if (eException != MB_EX_NONE) {
  317. vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
  318. ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
  319. }
  320. else {
  321. vMBMasterCBRequestScuuess( );
  322. vMBMasterRunResRelease( );
  323. }
  324. break;
  325. case EV_MASTER_FRAME_SENT:
  326. /* Master is busy now. */
  327. vMBMasterGetPDUSndBuf( &ucMBFrame );
  328. eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
  329. break;
  330. case EV_MASTER_ERROR_PROCESS:
  331. /* Execute specified error process callback function. */
  332. errorType = eMBMasterGetErrorType();
  333. vMBMasterGetPDUSndBuf( &ucMBFrame );
  334. switch (errorType) {
  335. case EV_ERROR_RESPOND_TIMEOUT:
  336. vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(),
  337. ucMBFrame, usMBMasterGetPDUSndLength());
  338. break;
  339. case EV_ERROR_RECEIVE_DATA:
  340. vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(),
  341. ucMBFrame, usMBMasterGetPDUSndLength());
  342. break;
  343. case EV_ERROR_EXECUTE_FUNCTION:
  344. vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(),
  345. ucMBFrame, usMBMasterGetPDUSndLength());
  346. break;
  347. }
  348. vMBMasterRunResRelease();
  349. break;
  350. default:
  351. break;
  352. }
  353. }
  354. return MB_ENOERR;
  355. }
  356. /* Get whether the Modbus Master is run in master mode.*/
  357. BOOL xMBMasterGetCBRunInMasterMode( void )
  358. {
  359. return xMBRunInMasterMode;
  360. }
  361. /* Set whether the Modbus Master is run in master mode.*/
  362. void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
  363. {
  364. xMBRunInMasterMode = IsMasterMode;
  365. }
  366. /* Get Modbus Master send destination address. */
  367. UCHAR ucMBMasterGetDestAddress( void )
  368. {
  369. return ucMBMasterDestAddress;
  370. }
  371. /* Set Modbus Master send destination address. */
  372. void vMBMasterSetDestAddress( UCHAR Address )
  373. {
  374. ucMBMasterDestAddress = Address;
  375. }
  376. /* Get Modbus Master current error event type. */
  377. eMBMasterErrorEventType eMBMasterGetErrorType( void )
  378. {
  379. return eMBMasterCurErrorType;
  380. }
  381. /* Set Modbus Master current error event type. */
  382. void vMBMasterSetErrorType( eMBMasterErrorEventType errorType )
  383. {
  384. eMBMasterCurErrorType = errorType;
  385. }
  386. #endif