mb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (c) 2006 Christian Walter <wolti@sil.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. };
  117. /* ----------------------- Start implementation -----------------------------*/
  118. eMBErrorCode
  119. eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
  120. {
  121. eMBErrorCode eStatus = MB_ENOERR;
  122. /* check preconditions */
  123. if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
  124. ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
  125. {
  126. eStatus = MB_EINVAL;
  127. }
  128. else
  129. {
  130. ucMBAddress = ucSlaveAddress;
  131. switch ( eMode )
  132. {
  133. #if MB_SLAVE_RTU_ENABLED > 0
  134. case MB_RTU:
  135. pvMBFrameStartCur = eMBRTUStart;
  136. pvMBFrameStopCur = eMBRTUStop;
  137. peMBFrameSendCur = eMBRTUSend;
  138. peMBFrameReceiveCur = eMBRTUReceive;
  139. pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
  140. pxMBFrameCBByteReceived = xMBRTUReceiveFSM;
  141. pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
  142. pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
  143. eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
  144. break;
  145. #endif
  146. #if MB_SLAVE_ASCII_ENABLED > 0
  147. case MB_ASCII:
  148. pvMBFrameStartCur = eMBASCIIStart;
  149. pvMBFrameStopCur = eMBASCIIStop;
  150. peMBFrameSendCur = eMBASCIISend;
  151. peMBFrameReceiveCur = eMBASCIIReceive;
  152. pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
  153. pxMBFrameCBByteReceived = xMBASCIIReceiveFSM;
  154. pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
  155. pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
  156. eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity );
  157. break;
  158. #endif
  159. default:
  160. eStatus = MB_EINVAL;
  161. break;
  162. }
  163. if( eStatus == MB_ENOERR )
  164. {
  165. if( !xMBPortEventInit( ) )
  166. {
  167. /* port dependent event module initalization failed. */
  168. eStatus = MB_EPORTERR;
  169. }
  170. else
  171. {
  172. eMBCurrentMode = eMode;
  173. eMBState = STATE_DISABLED;
  174. }
  175. }
  176. }
  177. return eStatus;
  178. }
  179. #if MB_SLAVE_TCP_ENABLED > 0
  180. eMBErrorCode
  181. eMBTCPInit( USHORT ucTCPPort )
  182. {
  183. eMBErrorCode eStatus = MB_ENOERR;
  184. if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR )
  185. {
  186. eMBState = STATE_DISABLED;
  187. }
  188. else if( !xMBPortEventInit( ) )
  189. {
  190. /* Port dependent event module initalization failed. */
  191. eStatus = MB_EPORTERR;
  192. }
  193. else
  194. {
  195. pvMBFrameStartCur = eMBTCPStart;
  196. pvMBFrameStopCur = eMBTCPStop;
  197. peMBFrameReceiveCur = eMBTCPReceive;
  198. peMBFrameSendCur = eMBTCPSend;
  199. pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
  200. ucMBAddress = MB_TCP_PSEUDO_ADDRESS;
  201. eMBCurrentMode = MB_TCP;
  202. eMBState = STATE_DISABLED;
  203. }
  204. return eStatus;
  205. }
  206. #endif
  207. eMBErrorCode
  208. eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
  209. {
  210. int i;
  211. eMBErrorCode eStatus;
  212. if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) )
  213. {
  214. ENTER_CRITICAL_SECTION( );
  215. if( pxHandler != NULL )
  216. {
  217. for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
  218. {
  219. if( ( xFuncHandlers[i].pxHandler == NULL ) ||
  220. ( xFuncHandlers[i].pxHandler == pxHandler ) )
  221. {
  222. xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
  223. xFuncHandlers[i].pxHandler = pxHandler;
  224. break;
  225. }
  226. }
  227. eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
  228. }
  229. else
  230. {
  231. for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
  232. {
  233. if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
  234. {
  235. xFuncHandlers[i].ucFunctionCode = 0;
  236. xFuncHandlers[i].pxHandler = NULL;
  237. break;
  238. }
  239. }
  240. /* Remove can't fail. */
  241. eStatus = MB_ENOERR;
  242. }
  243. EXIT_CRITICAL_SECTION( );
  244. }
  245. else
  246. {
  247. eStatus = MB_EINVAL;
  248. }
  249. return eStatus;
  250. }
  251. eMBErrorCode
  252. eMBClose( void )
  253. {
  254. eMBErrorCode eStatus = MB_ENOERR;
  255. if( eMBState == STATE_DISABLED )
  256. {
  257. if( pvMBFrameCloseCur != NULL )
  258. {
  259. pvMBFrameCloseCur( );
  260. }
  261. }
  262. else
  263. {
  264. eStatus = MB_EILLSTATE;
  265. }
  266. return eStatus;
  267. }
  268. eMBErrorCode
  269. eMBEnable( void )
  270. {
  271. eMBErrorCode eStatus = MB_ENOERR;
  272. if( eMBState == STATE_DISABLED )
  273. {
  274. /* Activate the protocol stack. */
  275. pvMBFrameStartCur( );
  276. eMBState = STATE_ENABLED;
  277. }
  278. else
  279. {
  280. eStatus = MB_EILLSTATE;
  281. }
  282. return eStatus;
  283. }
  284. eMBErrorCode
  285. eMBDisable( void )
  286. {
  287. eMBErrorCode eStatus;
  288. if( eMBState == STATE_ENABLED )
  289. {
  290. pvMBFrameStopCur( );
  291. eMBState = STATE_DISABLED;
  292. eStatus = MB_ENOERR;
  293. }
  294. else if( eMBState == STATE_DISABLED )
  295. {
  296. eStatus = MB_ENOERR;
  297. }
  298. else
  299. {
  300. eStatus = MB_EILLSTATE;
  301. }
  302. return eStatus;
  303. }
  304. eMBErrorCode eMBPoll( void )
  305. {
  306. static UCHAR *ucMBFrame;
  307. static UCHAR ucRcvAddress;
  308. static UCHAR ucFunctionCode;
  309. static USHORT usLength;
  310. static eMBException eException;
  311. int i;
  312. eMBErrorCode eStatus = MB_ENOERR;
  313. eMBEventType eEvent;
  314. /* Check if the protocol stack is ready. */
  315. if( eMBState != STATE_ENABLED )
  316. {
  317. return MB_EILLSTATE;
  318. }
  319. /* Check if there is a event available. If not return control to caller.
  320. * Otherwise we will handle the event. */
  321. if( xMBPortEventGet( &eEvent ) == TRUE )
  322. {
  323. switch ( eEvent )
  324. {
  325. case EV_READY:
  326. break;
  327. case EV_FRAME_RECEIVED:
  328. eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
  329. if( eStatus == MB_ENOERR )
  330. {
  331. /* Check if the frame is for us. If not ignore the frame. */
  332. if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
  333. {
  334. ( void )xMBPortEventPost( EV_EXECUTE );
  335. }
  336. }
  337. break;
  338. case EV_EXECUTE:
  339. ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
  340. eException = MB_EX_ILLEGAL_FUNCTION;
  341. for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
  342. {
  343. /* No more function handlers registered. Abort. */
  344. if( xFuncHandlers[i].ucFunctionCode == 0 )
  345. {
  346. break;
  347. }
  348. else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
  349. {
  350. eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
  351. break;
  352. }
  353. }
  354. /* If the request was not sent to the broadcast address we
  355. * return a reply. */
  356. if( ucRcvAddress != MB_ADDRESS_BROADCAST )
  357. {
  358. if( eException != MB_EX_NONE )
  359. {
  360. /* An exception occured. Build an error frame. */
  361. usLength = 0;
  362. ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
  363. ucMBFrame[usLength++] = eException;
  364. }
  365. eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
  366. }
  367. break;
  368. case EV_FRAME_SENT:
  369. break;
  370. }
  371. }
  372. return MB_ENOERR;
  373. }