mb.c 12 KB

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