mb.c 13 KB

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