port.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * FreeModbus Libary: BARE Demo Application
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id$
  20. */
  21. /* ----------------------- Modbus includes ----------------------------------*/
  22. #include "mb.h"
  23. #include "mbport.h"
  24. #include "string.h"
  25. #include "cfg.h"
  26. #include "main.h"
  27. /* ----------------------- Defines ------------------------------------------*/
  28. //输入寄存器
  29. #define REG_INPUT_START 1000
  30. #define REG_INPUT_NREGS 500
  31. //保持寄存器
  32. #define REG_HOLD_START 1500
  33. #define REG_HOLD_NREGS 500
  34. //线圈
  35. #define REG_COILS_START 0
  36. #define REG_COILS_NREGS 4
  37. //开关寄存器
  38. #define REG_DISCRETE_START 1000
  39. #define REG_DISCRETE_NREGS 4
  40. /* ----------------------- Static variables ---------------------------------*/
  41. static USHORT usRegInputStart = REG_INPUT_START;
  42. static USHORT usRegInputBuf[REG_INPUT_NREGS];
  43. static USHORT usRegHoldStart = REG_HOLD_START;
  44. static USHORT usRegHoldBuf[REG_HOLD_NREGS];
  45. static USHORT usRegCoilsStart = REG_COILS_START;
  46. static uint8_t usRegCoilsBuf[REG_COILS_NREGS];
  47. static USHORT usRegDiscreteStart = REG_DISCRETE_START;
  48. static uint8_t usRegDiscreteBuf[REG_DISCRETE_NREGS];
  49. /****************************************************************************
  50. * 名 称:eMBRegInputCB
  51. * 功 能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
  52. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  53. * usAddress: 寄存器地址
  54. * usNRegs: 要读取的寄存器个数
  55. * 出口参数:
  56. * 注 意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte)
  57. * +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)
  58. * +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
  59. * +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
  60. * 3 区
  61. ****************************************************************************/
  62. eMBErrorCode
  63. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  64. {
  65. eMBErrorCode eStatus = MB_ENOERR;
  66. int iRegIndex;
  67. usAddress = usAddress - 1;
  68. if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
  69. {
  70. iRegIndex = ( int )( usAddress - usRegInputStart );
  71. while( usNRegs > 0 )
  72. {
  73. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
  74. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
  75. iRegIndex++;
  76. usNRegs--;
  77. }
  78. }
  79. else
  80. {
  81. eStatus = MB_ENOREG;
  82. }
  83. return eStatus;
  84. }
  85. /****************************************************************************
  86. * 名 称:eMBRegHoldingCB
  87. * 功 能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister
  88. * 16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister
  89. * 03 读保持寄存器 eMBFuncReadHoldingRegister
  90. * 23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
  91. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  92. * usAddress: 寄存器地址
  93. * usNRegs: 要读写的寄存器个数
  94. * eMode: 功能码
  95. * 出口参数:
  96. * 注 意:4 区
  97. ****************************************************************************/
  98. static uint8_t cmd_cnt = 0;
  99. extern uint8_t md_status;
  100. //uint16_t swap_buff[MAX_LENTH] = {0};
  101. eMBErrorCode
  102. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
  103. {
  104. eMBErrorCode eStatus = MB_ENOERR;
  105. int iRegIndex;
  106. usAddress = usAddress - 1;
  107. uint32_t WiRegIndex = 0;
  108. uint32_t WusNRegs = 0;
  109. //Upgrade *upgrade = get_upgrade();
  110. g_MD_Data *g_data = get_md_data();
  111. if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
  112. {
  113. iRegIndex = (int)(usAddress - usRegHoldStart);
  114. WiRegIndex = iRegIndex;
  115. switch(eMode)
  116. {
  117. case MB_REG_READ://读寄存器
  118. {
  119. if(usAddress == MD_READ_STATUS)
  120. {
  121. usRegHoldBuf[iRegIndex] = md_status;
  122. }
  123. while(usNRegs > 0)
  124. {
  125. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
  126. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
  127. iRegIndex++;
  128. usNRegs--;
  129. }
  130. break;
  131. }
  132. case MB_REG_WRITE://写寄存器
  133. {
  134. WusNRegs = usNRegs;
  135. while(usNRegs > 0)
  136. {
  137. usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
  138. usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
  139. iRegIndex++;
  140. usNRegs--;
  141. }
  142. {
  143. switch(usAddress)
  144. {
  145. case MD_WRIE_DATA:
  146. {
  147. g_data->data[cmd_cnt].flag = 1;
  148. g_data->data[cmd_cnt].base_addr = usAddress-MD_BASE_Addr;
  149. g_data->data[cmd_cnt].flash_Data.index = usRegHoldBuf[WiRegIndex];
  150. g_data->data[cmd_cnt].flash_Data.lenth = usRegHoldBuf[WiRegIndex+1];
  151. memcpy(g_data->data[cmd_cnt].flash_Data.recive,&usRegHoldBuf[WiRegIndex+2],(WusNRegs-2) * 2);
  152. cmd_cnt++;
  153. if(cmd_cnt >= g_data->count)
  154. {
  155. cmd_cnt = 0;
  156. }
  157. }
  158. break;
  159. default:
  160. eStatus = MB_ENOREG;
  161. break;
  162. }
  163. }
  164. break;
  165. }
  166. }
  167. }
  168. else//错误
  169. {
  170. eStatus = MB_ENOREG;
  171. }
  172. return eStatus;
  173. }
  174. /****************************************************************************
  175. * 名 称:eMBRegCoilsCB
  176. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  177. * 05 写线圈 eMBFuncWriteCoil
  178. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  179. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  180. * usAddress: 线圈地址
  181. * usNCoils: 要读写的线圈个数
  182. * eMode: 功能码
  183. * 出口参数:
  184. * 注 意:如继电器
  185. * 0 区
  186. ****************************************************************************/
  187. eMBErrorCode
  188. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  189. {
  190. eMBErrorCode eStatus = MB_ENOERR;
  191. USHORT iRegIndex;
  192. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  193. UCHAR ucStatus = 0;
  194. UCHAR ucBits = 0;
  195. UCHAR ucDisp = 0;
  196. usAddress = usAddress - 1;
  197. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  198. {
  199. iRegIndex = (USHORT)(usAddress - usRegCoilsStart);
  200. switch(eMode)
  201. {
  202. case MB_REG_READ://读线圈
  203. while(usCoilGroups--)
  204. {
  205. ucDisp = 0;
  206. ucBits = 8;
  207. while((usNCoils--) != 0 && (ucBits--) != 0)
  208. {
  209. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  210. }
  211. *pucRegBuffer++ = ucStatus;
  212. }
  213. break;
  214. case MB_REG_WRITE://写线圈
  215. while(usCoilGroups--)
  216. {
  217. ucStatus = *pucRegBuffer++;
  218. ucBits = 8;
  219. while((usNCoils--) != 0 && (ucBits--) != 0)
  220. {
  221. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  222. ucStatus >>= 1;
  223. }
  224. }
  225. }
  226. }
  227. else//错误
  228. {
  229. eStatus = MB_ENOREG;
  230. }
  231. return eStatus;
  232. }
  233. /****************************************************************************
  234. * 名 称:eMBRegDiscreteCB
  235. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  236. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  237. * usAddress: 寄存器地址
  238. * usNDiscrete: 要读取的寄存器个数
  239. * 出口参数:
  240. * 注 意:1 区
  241. ****************************************************************************/
  242. eMBErrorCode
  243. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  244. {
  245. eMBErrorCode eStatus = MB_ENOERR;
  246. USHORT iRegIndex;
  247. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  248. UCHAR ucStatus = 0;
  249. UCHAR ucBits = 0;
  250. UCHAR ucDisp = 0;
  251. usAddress = usAddress - 1;
  252. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  253. {
  254. iRegIndex = (USHORT)(usAddress - usRegDiscreteStart);
  255. while(usDiscreteGroups--)
  256. {
  257. ucDisp = 0;
  258. ucBits = 8;
  259. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  260. {
  261. if(usRegDiscreteBuf[iRegIndex])
  262. {
  263. ucStatus |= (1 << ucDisp);
  264. }
  265. ucDisp++;
  266. }
  267. *pucRegBuffer++ = ucStatus;
  268. }
  269. }
  270. else//错误
  271. {
  272. eStatus = MB_ENOREG;
  273. }
  274. return eStatus;
  275. }