mb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. 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. * Using for Modbus Slave
  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. * Using for Modbus Slave
  75. */
  76. BOOL ( *pxMBFrameCBByteReceived )(eMBRTUSlaveObj *pObj);
  77. BOOL ( *pxMBFrameCBTransmitterEmpty )(eMBRTUSlaveObj *pObj);
  78. BOOL ( *pxMBPortCBTimerExpired )(eMBRTUSlaveObj *pObj);
  79. BOOL ( *pxMBFrameCBReceiveFSMCur )(void);
  80. BOOL ( *pxMBFrameCBTransmitFSMCur )(void);
  81. /* An array of Modbus functions handlers which associates Modbus function
  82. * codes with implementing functions.
  83. */
  84. static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
  85. #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
  86. {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
  87. #endif
  88. #if MB_FUNC_READ_INPUT_ENABLED > 0
  89. {MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
  90. #endif
  91. #if MB_FUNC_READ_HOLDING_ENABLED > 0
  92. {MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
  93. #endif
  94. #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
  95. {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
  96. #endif
  97. #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
  98. {MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
  99. #endif
  100. #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
  101. {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
  102. #endif
  103. #if MB_FUNC_WRITE_FILE_ENABLE > 0
  104. {MB_FUNC_WRITE_FILE, eMBFuncWriteFile}
  105. #endif
  106. #if MB_FUNC_WRITE_DEVICE_SERIAL_ENABLE > 0
  107. {MB_FUNC_WRITE_DEVICE_SERIAL_NUMBER, eMBFuncWriteDeviceSerial}
  108. #endif
  109. #if MB_FUNC_READ_COILS_ENABLED > 0
  110. {MB_FUNC_READ_COILS, eMBFuncReadCoils},
  111. #endif
  112. #if MB_FUNC_WRITE_COIL_ENABLED > 0
  113. {MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
  114. #endif
  115. #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
  116. {MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
  117. #endif
  118. #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
  119. {MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
  120. #endif
  121. };
  122. /* ----------------------- Start implementation -----------------------------*/
  123. eMBErrorCode
  124. eMBInit(eMBRTUSlaveObj *obj) {
  125. eMBErrorCode eStatus = MB_ENOERR;
  126. /* check preconditions */
  127. if ((obj->ucSlaveAddress == MB_ADDRESS_BROADCAST) ||
  128. (obj->ucSlaveAddress < MB_ADDRESS_MIN) || (obj->ucSlaveAddress > MB_ADDRESS_MAX)) {
  129. eStatus = MB_EINVAL;
  130. } else {
  131. ucMBAddress = obj->ucSlaveAddress;
  132. switch (obj->eMode) {
  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(obj);
  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. if (!xMBPortEventInit(obj)) {
  165. /* port dependent event module initalization failed. */
  166. eStatus = MB_EPORTERR;
  167. } else {
  168. eMBCurrentMode = obj->eMode;
  169. eMBState = STATE_DISABLED;
  170. }
  171. }
  172. }
  173. return eStatus;
  174. }
  175. #if MB_SLAVE_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. int i;
  206. eMBErrorCode eStatus;
  207. if ((0 < ucFunctionCode) && (ucFunctionCode <= 127)) {
  208. ENTER_CRITICAL_SECTION();
  209. if (pxHandler != NULL) {
  210. for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) {
  211. if ((xFuncHandlers[i].pxHandler == NULL) ||
  212. (xFuncHandlers[i].pxHandler == pxHandler)) {
  213. xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
  214. xFuncHandlers[i].pxHandler = pxHandler;
  215. break;
  216. }
  217. }
  218. eStatus = (i != MB_FUNC_HANDLERS_MAX) ? MB_ENOERR : MB_ENORES;
  219. } else {
  220. for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) {
  221. if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
  222. xFuncHandlers[i].ucFunctionCode = 0;
  223. xFuncHandlers[i].pxHandler = NULL;
  224. break;
  225. }
  226. }
  227. /* Remove can't fail. */
  228. eStatus = MB_ENOERR;
  229. }
  230. EXIT_CRITICAL_SECTION();
  231. } else {
  232. eStatus = MB_EINVAL;
  233. }
  234. return eStatus;
  235. }
  236. eMBErrorCode
  237. eMBClose(eMBRTUSlaveObj *pObj) {
  238. eMBErrorCode eStatus = MB_ENOERR;
  239. if (eMBState == STATE_DISABLED) {
  240. if (pvMBFrameCloseCur != NULL) {
  241. pvMBFrameCloseCur(pObj);
  242. }
  243. } else {
  244. eStatus = MB_EILLSTATE;
  245. }
  246. return eStatus;
  247. }
  248. eMBErrorCode
  249. eMBEnable(eMBRTUSlaveObj *obj) {
  250. eMBErrorCode eStatus = MB_ENOERR;
  251. if (eMBState == STATE_DISABLED) {
  252. /* Activate the protocol stack. */
  253. pvMBFrameStartCur(obj);
  254. eMBState = STATE_ENABLED;
  255. } else {
  256. eStatus = MB_EILLSTATE;
  257. }
  258. return eStatus;
  259. }
  260. eMBErrorCode
  261. eMBDisable(eMBRTUSlaveObj *pObj) {
  262. eMBErrorCode eStatus;
  263. if (eMBState == STATE_ENABLED) {
  264. pvMBFrameStopCur(pObj);
  265. eMBState = STATE_DISABLED;
  266. eStatus = MB_ENOERR;
  267. } else if (eMBState == STATE_DISABLED) {
  268. eStatus = MB_ENOERR;
  269. } else {
  270. eStatus = MB_EILLSTATE;
  271. }
  272. return eStatus;
  273. }
  274. eMBErrorCode eMBPoll(eMBRTUSlaveObj *obj) {
  275. static UCHAR ucRcvAddress;
  276. static UCHAR ucFunctionCode;
  277. static eMBException eException;
  278. int i;
  279. eMBErrorCode eStatus = MB_ENOERR;
  280. eMBEventType eEvent;
  281. /* Check if the protocol stack is ready. */
  282. if (eMBState != STATE_ENABLED) {
  283. return MB_EILLSTATE;
  284. }
  285. /* Check if there is a event available. If not return control to caller.
  286. * Otherwise we will handle the event. */
  287. if (xMBPortEventGet(obj, &eEvent) == TRUE) {
  288. switch (eEvent) {
  289. case EV_READY:
  290. break;
  291. case EV_FRAME_RECEIVED:
  292. eStatus = peMBFrameReceiveCur(&ucRcvAddress, &obj->ucMBFrame, &obj->usLength, obj);
  293. if (eStatus == MB_ENOERR) {
  294. /* Check if the frame is for us. If not ignore the frame. */
  295. if ((ucRcvAddress == obj->ucSlaveAddress) || (ucRcvAddress == MB_ADDRESS_BROADCAST)) {
  296. (void) xMBPortEventPost(obj, EV_EXECUTE);
  297. }
  298. }
  299. break;
  300. case EV_EXECUTE:
  301. ucFunctionCode = obj->ucMBFrame[MB_PDU_FUNC_OFF];
  302. eException = MB_EX_ILLEGAL_FUNCTION;
  303. for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) {
  304. /* No more function handlers registered. Abort. */
  305. if (xFuncHandlers[i].ucFunctionCode == 0) {
  306. break;
  307. } else if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
  308. eException = xFuncHandlers[i].pxHandler(obj, obj->ucMBFrame, &obj->usLength);
  309. break;
  310. }
  311. }
  312. /* If the request was not sent to the broadcast address we
  313. * return a reply. */
  314. if (eException != MB_EX_NONE) {
  315. /* An exception occured. Build an error frame. */
  316. obj->usLength = 0;
  317. obj->ucMBFrame[obj->usLength++] = (UCHAR) (ucFunctionCode | MB_FUNC_ERROR);
  318. obj->ucMBFrame[obj->usLength++] = eException;
  319. }
  320. eStatus = peMBFrameSendCur(obj, obj->ucMBFrame, obj->usLength);
  321. break;
  322. case EV_FRAME_SENT:
  323. break;
  324. }
  325. }
  326. return MB_ENOERR;
  327. }