mb.c 12 KB

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