user_mb_app.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ///*
  2. // * FreeModbus Libary: user callback functions and buffer define in slave mode
  3. // * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. // *
  5. // * This library is free software; you can redistribute it and/or
  6. // * modify it under the terms of the GNU Lesser General Public
  7. // * License as published by the Free Software Foundation; either
  8. // * version 2.1 of the License, or (at your option) any later version.
  9. // *
  10. // * This library is distributed in the hope that it will be useful,
  11. // * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // * Lesser General Public License for more details.
  14. // *
  15. // * You should have received a copy of the GNU Lesser General Public
  16. // * License along with this library; if not, write to the Free Software
  17. // * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. // *
  19. // * File: $Id: user_mb_app.c,v 1.60 2013/11/23 11:49:05 Armink $
  20. // */
  21. //#include "user_mb_app.h"
  22. //
  23. ///*------------------------Slave mode use these variables----------------------*/
  24. ////Slave mode:DiscreteInputs variables
  25. ////USHORT usSDiscInStart = S_DISCRETE_INPUT_START;
  26. ////#if S_DISCRETE_INPUT_NDISCRETES%8
  27. ////UCHAR ucSDiscInBuf[S_DISCRETE_INPUT_NDISCRETES/8+1];
  28. ////#else
  29. ////UCHAR ucSDiscInBuf[S_DISCRETE_INPUT_NDISCRETES/8] ;
  30. ////#endif
  31. //////Slave mode:Coils variables
  32. ////USHORT usSCoilStart = S_COIL_START;
  33. ////#if S_COIL_NCOILS%8
  34. ////UCHAR ucSCoilBuf[S_COIL_NCOILS/8+1] ;
  35. ////#else
  36. ////UCHAR ucSCoilBuf[S_COIL_NCOILS/8] ;
  37. ////#endif
  38. //////Slave mode:InputRegister variables
  39. ////USHORT usSRegInStart = S_REG_INPUT_START;
  40. ////USHORT usSRegInBuf[S_REG_INPUT_NREGS] ;
  41. //////Slave mode:HoldingRegister variables
  42. //USHORT usSRegHoldStart = S_REG_HOLDING_START;
  43. //USHORT usSRegHoldBuf[S_REG_HOLDING_NREGS] ;
  44. //
  45. ///**
  46. // * Modbus slave input register callback function.
  47. // *
  48. // * @param pucRegBuffer input register buffer
  49. // * @param usAddress input register address
  50. // * @param usNRegs input register number
  51. // *
  52. // * @return result
  53. // */
  54. //eMBErrorCode eMBRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  55. //{
  56. // eMBErrorCode eStatus = MB_ENOERR;
  57. //// USHORT iRegIndex;
  58. //// USHORT * pusRegInputBuf;
  59. //// USHORT REG_INPUT_START;
  60. //// USHORT REG_INPUT_NREGS;
  61. //// USHORT usRegInStart;
  62. ////
  63. //// pusRegInputBuf = usSRegInBuf;
  64. //// REG_INPUT_START = S_REG_INPUT_START;
  65. //// REG_INPUT_NREGS = S_REG_INPUT_NREGS;
  66. //// usRegInStart = usSRegInStart;
  67. ////
  68. //// /* it already plus one in modbus function method. */
  69. //// usAddress--;
  70. ////
  71. //// if ((usAddress >= REG_INPUT_START)
  72. //// && (usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS))
  73. //// {
  74. //// iRegIndex = usAddress - usRegInStart;
  75. //// while (usNRegs > 0)
  76. //// {
  77. //// *pucRegBuffer++ = (UCHAR) (pusRegInputBuf[iRegIndex] >> 8);
  78. //// *pucRegBuffer++ = (UCHAR) (pusRegInputBuf[iRegIndex] & 0xFF);
  79. //// iRegIndex++;
  80. //// usNRegs--;
  81. //// }
  82. //// }
  83. //// else
  84. //// {
  85. //// eStatus = MB_ENOREG;
  86. //// }
  87. //
  88. // return eStatus;
  89. //}
  90. //
  91. ///**
  92. // * Modbus slave holding register callback function.
  93. // *
  94. // * @param pucRegBuffer holding register buffer
  95. // * @param usAddress holding register address
  96. // * @param usNRegs holding register number
  97. // * @param eMode read or write
  98. // *
  99. // * @return result
  100. // */
  101. //eMBErrorCode eMBRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
  102. // USHORT usNRegs, eMBRegisterMode eMode)
  103. //{
  104. // eMBErrorCode eStatus = MB_ENOERR;
  105. // USHORT iRegIndex;
  106. // USHORT * pusRegHoldingBuf;
  107. // USHORT REG_HOLDING_START;
  108. // USHORT REG_HOLDING_NREGS;
  109. // USHORT usRegHoldStart;
  110. // pusRegHoldingBuf = usSRegHoldBuf;
  111. // REG_HOLDING_START = S_REG_HOLDING_START;
  112. // REG_HOLDING_NREGS = S_REG_HOLDING_NREGS;
  113. // usRegHoldStart = usSRegHoldStart;
  114. //
  115. // /* it already plus one in modbus function method. */
  116. // usAddress--;
  117. //
  118. // if ((usAddress >= REG_HOLDING_START)
  119. // && (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS))
  120. // {
  121. // iRegIndex = usAddress - usRegHoldStart;
  122. // switch (eMode)
  123. // {
  124. // /* read current register values from the protocol stack. */
  125. // case MB_REG_READ:
  126. // while (usNRegs > 0)
  127. // {
  128. // *pucRegBuffer++ = (UCHAR) (pusRegHoldingBuf[iRegIndex] >> 8);
  129. // *pucRegBuffer++ = (UCHAR) (pusRegHoldingBuf[iRegIndex] & 0xFF);
  130. // iRegIndex++;
  131. // usNRegs--;
  132. // }
  133. // break;
  134. //
  135. // /* write current register values with new values from the protocol stack. */
  136. // case MB_REG_WRITE:
  137. // while (usNRegs > 0)
  138. // {
  139. // pusRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
  140. // pusRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
  141. // iRegIndex++;
  142. // usNRegs--;
  143. // }
  144. // break;
  145. // }
  146. // }
  147. // else
  148. // {
  149. // eStatus = MB_ENOREG;
  150. // }
  151. // return eStatus;
  152. //}
  153. //
  154. ///**
  155. // * Modbus slave coils callback function.
  156. // *
  157. // * @param pucRegBuffer coils buffer
  158. // * @param usAddress coils address
  159. // * @param usNCoils coils number
  160. // * @param eMode read or write
  161. // *
  162. // * @return result
  163. // */
  164. //eMBErrorCode eMBRegCoilsCB(UCHAR * pucRegBuffer, USHORT usAddress,
  165. // USHORT usNCoils, eMBRegisterMode eMode)
  166. //{
  167. // eMBErrorCode eStatus = MB_ENOERR;
  168. //// USHORT iRegIndex , iRegBitIndex , iNReg;
  169. //// UCHAR * pucCoilBuf;
  170. //// USHORT COIL_START;
  171. //// USHORT COIL_NCOILS;
  172. //// USHORT usCoilStart;
  173. //// iNReg = usNCoils / 8 + 1;
  174. ////
  175. //// pucCoilBuf = ucSCoilBuf;
  176. //// COIL_START = S_COIL_START;
  177. //// COIL_NCOILS = S_COIL_NCOILS;
  178. //// usCoilStart = usSCoilStart;
  179. ////
  180. //// /* it already plus one in modbus function method. */
  181. //// usAddress--;
  182. ////
  183. //// if( ( usAddress >= COIL_START ) &&
  184. //// ( usAddress + usNCoils <= COIL_START + COIL_NCOILS ) )
  185. //// {
  186. //// iRegIndex = (USHORT) (usAddress - usCoilStart) / 8;
  187. //// iRegBitIndex = (USHORT) (usAddress - usCoilStart) % 8;
  188. //// switch ( eMode )
  189. //// {
  190. //// /* read current coil values from the protocol stack. */
  191. //// case MB_REG_READ:
  192. //// while (iNReg > 0)
  193. //// {
  194. //// *pucRegBuffer++ = xMBUtilGetBits(&pucCoilBuf[iRegIndex++],
  195. //// iRegBitIndex, 8);
  196. //// iNReg--;
  197. //// }
  198. //// pucRegBuffer--;
  199. //// /* last coils */
  200. //// usNCoils = usNCoils % 8;
  201. //// /* filling zero to high bit */
  202. //// *pucRegBuffer = *pucRegBuffer << (8 - usNCoils);
  203. //// *pucRegBuffer = *pucRegBuffer >> (8 - usNCoils);
  204. //// break;
  205. ////
  206. //// /* write current coil values with new values from the protocol stack. */
  207. //// case MB_REG_WRITE:
  208. //// while (iNReg > 1)
  209. //// {
  210. //// xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, 8,
  211. //// *pucRegBuffer++);
  212. //// iNReg--;
  213. //// }
  214. //// /* last coils */
  215. //// usNCoils = usNCoils % 8;
  216. //// /* xMBUtilSetBits has bug when ucNBits is zero */
  217. //// if (usNCoils != 0)
  218. //// {
  219. //// xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils,
  220. //// *pucRegBuffer++);
  221. //// }
  222. //// break;
  223. //// }
  224. //// }
  225. //// else
  226. //// {
  227. //// eStatus = MB_ENOREG;
  228. //// }
  229. // return eStatus;
  230. //}
  231. //
  232. ///**
  233. // * Modbus slave discrete callback function.
  234. // *
  235. // * @param pucRegBuffer discrete buffer
  236. // * @param usAddress discrete address
  237. // * @param usNDiscrete discrete number
  238. // *
  239. // * @return result
  240. // */
  241. //eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  242. //{
  243. // eMBErrorCode eStatus = MB_ENOERR;
  244. //// USHORT iRegIndex , iRegBitIndex , iNReg;
  245. //// UCHAR * pucDiscreteInputBuf;
  246. //// USHORT DISCRETE_INPUT_START;
  247. //// USHORT DISCRETE_INPUT_NDISCRETES;
  248. //// USHORT usDiscreteInputStart;
  249. //// iNReg = usNDiscrete / 8 + 1;
  250. ////
  251. //// pucDiscreteInputBuf = ucSDiscInBuf;
  252. //// DISCRETE_INPUT_START = S_DISCRETE_INPUT_START;
  253. //// DISCRETE_INPUT_NDISCRETES = S_DISCRETE_INPUT_NDISCRETES;
  254. //// usDiscreteInputStart = usSDiscInStart;
  255. ////
  256. //// /* it already plus one in modbus function method. */
  257. //// usAddress--;
  258. ////
  259. //// if ((usAddress >= DISCRETE_INPUT_START)
  260. //// && (usAddress + usNDiscrete <= DISCRETE_INPUT_START + DISCRETE_INPUT_NDISCRETES))
  261. //// {
  262. //// iRegIndex = (USHORT) (usAddress - usDiscreteInputStart) / 8;
  263. //// iRegBitIndex = (USHORT) (usAddress - usDiscreteInputStart) % 8;
  264. ////
  265. //// while (iNReg > 0)
  266. //// {
  267. //// *pucRegBuffer++ = xMBUtilGetBits(&pucDiscreteInputBuf[iRegIndex++],
  268. //// iRegBitIndex, 8);
  269. //// iNReg--;
  270. //// }
  271. //// pucRegBuffer--;
  272. //// /* last discrete */
  273. //// usNDiscrete = usNDiscrete % 8;
  274. //// /* filling zero to high bit */
  275. //// *pucRegBuffer = *pucRegBuffer << (8 - usNDiscrete);
  276. //// *pucRegBuffer = *pucRegBuffer >> (8 - usNDiscrete);
  277. //// }
  278. //// else
  279. //// {
  280. //// eStatus = MB_ENOREG;
  281. //// }
  282. //
  283. // return eStatus;
  284. //}
  285. //