mb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.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. */
  29. /* ----------------------- System includes ----------------------------------*/
  30. #include "stdlib.h"
  31. #include "string.h"
  32. /* ----------------------- Platform includes --------------------------------*/
  33. #include "port.h"
  34. /* ----------------------- Modbus includes ----------------------------------*/
  35. #include "mb.h"
  36. #include "mbconfig.h"
  37. #include "mbframe.h"
  38. #include "mbproto.h"
  39. #include "mbfunc.h"
  40. #include "mbport.h"
  41. #if MB_RTU_ENABLED == 1
  42. #include "mbrtu.h"
  43. #endif
  44. #if MB_ASCII_ENABLED == 1
  45. #include "mbascii.h"
  46. #endif
  47. #if MB_TCP_ENABLED == 1
  48. #include "mbtcp.h"
  49. #endif
  50. #ifndef MB_PORT_HAS_CLOSE
  51. #define MB_PORT_HAS_CLOSE 0
  52. #endif
  53. /* ----------------------- Static variables ---------------------------------*/
  54. static UCHAR ucMBAddress;
  55. static eMBMode eMBCurrentMode;
  56. static enum
  57. {
  58. STATE_ENABLED,
  59. STATE_DISABLED,
  60. STATE_NOT_INITIALIZED
  61. } eMBState = STATE_NOT_INITIALIZED;
  62. /* Functions pointer which are initialized in eMBInit( ). Depending on the
  63. * mode (RTU or ASCII) the are set to the correct implementations.
  64. */
  65. static peMBFrameSend peMBFrameSendCur;
  66. static pvMBFrameStart pvMBFrameStartCur;
  67. static pvMBFrameStop pvMBFrameStopCur;
  68. static peMBFrameReceive peMBFrameReceiveCur;
  69. static pvMBFrameClose pvMBFrameCloseCur;
  70. /* Callback functions required by the porting layer. They are called when
  71. * an external event has happend which includes a timeout or the reception
  72. * or transmission of a character.
  73. */
  74. BOOL( *pxMBFrameCBByteReceived ) ( void );
  75. BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
  76. BOOL( *pxMBPortCBTimerExpired ) ( void );
  77. BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
  78. BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
  79. /* An array of Modbus functions handlers which associates Modbus function
  80. * codes with implementing functions.
  81. */
  82. static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
  83. #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
  84. {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
  85. #endif
  86. #if MB_FUNC_READ_INPUT_ENABLED > 0
  87. {MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
  88. #endif
  89. #if MB_FUNC_READ_HOLDING_ENABLED > 0
  90. {MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
  91. #endif
  92. #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
  93. {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
  94. #endif
  95. #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
  96. {MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
  97. #endif
  98. #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
  99. {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
  100. #endif
  101. #if MB_FUNC_READ_COILS_ENABLED > 0
  102. {MB_FUNC_READ_COILS, eMBFuncReadCoils},
  103. #endif
  104. #if MB_FUNC_WRITE_COIL_ENABLED > 0
  105. {MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
  106. #endif
  107. #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
  108. {MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
  109. #endif
  110. #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
  111. {MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
  112. #endif
  113. };
  114. /* ----------------------- Start implementation -----------------------------*/
  115. eMBErrorCode
  116. eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
  117. {
  118. eMBErrorCode eStatus = MB_ENOERR;
  119. /* check preconditions */
  120. if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
  121. ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
  122. {
  123. eStatus = MB_EINVAL;
  124. }
  125. else
  126. {
  127. ucMBAddress = ucSlaveAddress;
  128. switch ( eMode )
  129. {
  130. #if MB_RTU_ENABLED > 0
  131. case MB_RTU:
  132. pvMBFrameStartCur = eMBRTUStart;
  133. pvMBFrameStopCur = eMBRTUStop;
  134. peMBFrameSendCur = eMBRTUSend;
  135. peMBFrameReceiveCur = eMBRTUReceive;
  136. pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
  137. pxMBFrameCBByteReceived = xMBRTUReceiveFSM;
  138. pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
  139. pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
  140. eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
  141. break;
  142. #endif
  143. #if MB_ASCII_ENABLED > 0
  144. case MB_ASCII:
  145. pvMBFrameStartCur = eMBASCIIStart;
  146. pvMBFrameStopCur = eMBASCIIStop;
  147. peMBFrameSendCur = eMBASCIISend;
  148. peMBFrameReceiveCur = eMBASCIIReceive;
  149. pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
  150. pxMBFrameCBByteReceived = xMBASCIIReceiveFSM;
  151. pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
  152. pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
  153. eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity );
  154. break;
  155. #endif
  156. default:
  157. eStatus = MB_EINVAL;
  158. }
  159. if( eStatus == MB_ENOERR )
  160. {
  161. if( !xMBPortEventInit( ) )
  162. {
  163. /* port dependent event module initalization failed. */
  164. eStatus = MB_EPORTERR;
  165. }
  166. else
  167. {
  168. eMBCurrentMode = eMode;
  169. eMBState = STATE_DISABLED;
  170. }
  171. }
  172. }
  173. return eStatus;
  174. }
  175. #if MB_TCP_ENABLED > 0
  176. eMBErrorCode
  177. eMBTCPInit( USHORT ucTCPPort )
  178. {
  179. eMBErrorCode eStatus = MB_ENOERR;
  180. if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR )
  181. {
  182. eMBState = STATE_DISABLED;
  183. }
  184. else if( !xMBPortEventInit( ) )
  185. {
  186. /* Port dependent event module initalization failed. */
  187. eStatus = MB_EPORTERR;
  188. }
  189. else
  190. {
  191. pvMBFrameStartCur = eMBTCPStart;
  192. pvMBFrameStopCur = eMBTCPStop;
  193. peMBFrameReceiveCur = eMBTCPReceive;
  194. peMBFrameSendCur = eMBTCPSend;
  195. pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
  196. ucMBAddress = MB_TCP_PSEUDO_ADDRESS;
  197. eMBCurrentMode = MB_TCP;
  198. eMBState = STATE_DISABLED;
  199. }
  200. return eStatus;
  201. }
  202. #endif
  203. eMBErrorCode
  204. eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
  205. {
  206. int i;
  207. eMBErrorCode eStatus;
  208. if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) )
  209. {
  210. ENTER_CRITICAL_SECTION( );
  211. if( pxHandler != NULL )
  212. {
  213. for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
  214. {
  215. if( ( xFuncHandlers[i].pxHandler == NULL ) ||
  216. ( xFuncHandlers[i].pxHandler == pxHandler ) )
  217. {
  218. xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
  219. xFuncHandlers[i].pxHandler = pxHandler;
  220. break;
  221. }
  222. }
  223. eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
  224. }
  225. else
  226. {
  227. for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
  228. {
  229. if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
  230. {
  231. xFuncHandlers[i].ucFunctionCode = 0;
  232. xFuncHandlers[i].pxHandler = NULL;
  233. break;
  234. }
  235. }
  236. /* Remove can't fail. */
  237. eStatus = MB_ENOERR;
  238. }
  239. EXIT_CRITICAL_SECTION( );
  240. }
  241. else
  242. {
  243. eStatus = MB_EINVAL;
  244. }
  245. return eStatus;
  246. }
  247. eMBErrorCode
  248. eMBClose( void )
  249. {
  250. eMBErrorCode eStatus = MB_ENOERR;
  251. if( eMBState == STATE_DISABLED )
  252. {
  253. if( pvMBFrameCloseCur != NULL )
  254. {
  255. pvMBFrameCloseCur( );
  256. }
  257. }
  258. else
  259. {
  260. eStatus = MB_EILLSTATE;
  261. }
  262. return eStatus;
  263. }
  264. eMBErrorCode
  265. eMBEnable( void )
  266. {
  267. eMBErrorCode eStatus = MB_ENOERR;
  268. if( eMBState == STATE_DISABLED )
  269. {
  270. /* Activate the protocol stack. */
  271. pvMBFrameStartCur( );
  272. eMBState = STATE_ENABLED;
  273. }
  274. else
  275. {
  276. eStatus = MB_EILLSTATE;
  277. }
  278. return eStatus;
  279. }
  280. eMBErrorCode
  281. eMBDisable( void )
  282. {
  283. eMBErrorCode eStatus;
  284. if( eMBState == STATE_ENABLED )
  285. {
  286. pvMBFrameStopCur( );
  287. eMBState = STATE_DISABLED;
  288. eStatus = MB_ENOERR;
  289. }
  290. else if( eMBState == STATE_DISABLED )
  291. {
  292. eStatus = MB_ENOERR;
  293. }
  294. else
  295. {
  296. eStatus = MB_EILLSTATE;
  297. }
  298. return eStatus;
  299. }
  300. eMBErrorCode
  301. eMBPoll( void )
  302. {
  303. static UCHAR *ucMBFrame;
  304. static UCHAR ucRcvAddress;
  305. static UCHAR ucFunctionCode;
  306. static USHORT usLength;
  307. static eMBException eException;
  308. int i;
  309. eMBErrorCode eStatus = MB_ENOERR;
  310. eMBEventType eEvent;
  311. /* Check if the protocol stack is ready. */
  312. if( eMBState != STATE_ENABLED )
  313. {
  314. return MB_EILLSTATE;
  315. }
  316. /* Check if there is a event available. If not return control to caller.
  317. * Otherwise we will handle the event. */
  318. if( xMBPortEventGet( &eEvent ) == TRUE )
  319. {
  320. switch ( eEvent )
  321. {
  322. case EV_READY:
  323. break;
  324. case EV_FRAME_RECEIVED:
  325. eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
  326. if( eStatus == MB_ENOERR )
  327. {
  328. /* Check if the frame is for us. If not ignore the frame. */
  329. if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
  330. {
  331. ( void )xMBPortEventPost( EV_EXECUTE );
  332. }
  333. }
  334. break;
  335. case EV_EXECUTE:
  336. ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
  337. eException = MB_EX_ILLEGAL_FUNCTION;
  338. for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
  339. {
  340. /* No more function handlers registered. Abort. */
  341. if( xFuncHandlers[i].ucFunctionCode == 0 )
  342. {
  343. break;
  344. }
  345. else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
  346. {
  347. eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
  348. break;
  349. }
  350. }
  351. /* If the request was not sent to the broadcast address we
  352. * return a reply. */
  353. if( ucRcvAddress != MB_ADDRESS_BROADCAST )
  354. {
  355. if( eException != MB_EX_NONE )
  356. {
  357. /* An exception occured. Build an error frame. */
  358. usLength = 0;
  359. ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
  360. ucMBFrame[usLength++] = eException;
  361. }
  362. if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
  363. {
  364. // vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS );
  365. }
  366. eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
  367. }
  368. break;
  369. case EV_FRAME_SENT:
  370. break;
  371. }
  372. }
  373. return MB_ENOERR;
  374. }