port.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. extern uint8_t programe_erase_flag ;
  102. extern uint8_t programe_write_complete_flag ;
  103. extern uint8_t meta_erase_flag ;
  104. extern uint8_t meta_write_complete_flag ;
  105. extern uint8_t get_head_over_flag ;
  106. eMBErrorCode
  107. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
  108. {
  109. eMBErrorCode eStatus = MB_ENOERR;
  110. int iRegIndex;
  111. usAddress = usAddress - 1;
  112. uint32_t WiRegIndex = 0;
  113. uint32_t WusNRegs = 0;
  114. //Upgrade *upgrade = get_upgrade();
  115. g_MD_Data *g_data = get_md_data();
  116. if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
  117. {
  118. iRegIndex = (int)(usAddress - usRegHoldStart);
  119. WiRegIndex = iRegIndex;
  120. switch(eMode)
  121. {
  122. case MB_REG_READ://读寄存器
  123. {
  124. if(usAddress == MD_READ_STATUS)
  125. {
  126. usRegHoldBuf[iRegIndex] = md_status;
  127. }
  128. while(usNRegs > 0)
  129. {
  130. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
  131. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
  132. iRegIndex++;
  133. usNRegs--;
  134. }
  135. break;
  136. }
  137. case MB_REG_WRITE://写寄存器
  138. {
  139. WusNRegs = usNRegs;
  140. while(usNRegs > 0)
  141. {
  142. usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
  143. usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
  144. iRegIndex++;
  145. usNRegs--;
  146. }
  147. {
  148. switch(usAddress)
  149. {
  150. case MD_WRIE_DATA:
  151. {
  152. g_data->data[cmd_cnt].flag = 1;
  153. g_data->data[cmd_cnt].base_addr = usAddress-MD_BASE_Addr;
  154. g_data->data[cmd_cnt].flash_Data.index = usRegHoldBuf[WiRegIndex];
  155. g_data->data[cmd_cnt].flash_Data.lenth = usRegHoldBuf[WiRegIndex+1];
  156. memcpy(g_data->data[cmd_cnt].flash_Data.recive,&usRegHoldBuf[WiRegIndex+2],(WusNRegs-2) * 2);
  157. cmd_cnt++;
  158. if(cmd_cnt >= g_data->count)
  159. {
  160. cmd_cnt = 0;
  161. }
  162. }
  163. break;
  164. case MD_WRITE_Reset:
  165. {
  166. md_status = UPGRADE_STATUS_IDE;
  167. meta_write_complete_flag = 0; //
  168. meta_erase_flag = 0;
  169. get_head_over_flag = 0;
  170. programe_erase_flag = 0;
  171. programe_write_complete_flag = 0;
  172. }
  173. default:
  174. eStatus = MB_ENOREG;
  175. break;
  176. }
  177. }
  178. break;
  179. }
  180. }
  181. }
  182. else//错误
  183. {
  184. eStatus = MB_ENOREG;
  185. }
  186. return eStatus;
  187. }
  188. /****************************************************************************
  189. * 名 称:eMBRegCoilsCB
  190. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  191. * 05 写线圈 eMBFuncWriteCoil
  192. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  193. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  194. * usAddress: 线圈地址
  195. * usNCoils: 要读写的线圈个数
  196. * eMode: 功能码
  197. * 出口参数:
  198. * 注 意:如继电器
  199. * 0 区
  200. ****************************************************************************/
  201. eMBErrorCode
  202. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  203. {
  204. eMBErrorCode eStatus = MB_ENOERR;
  205. USHORT iRegIndex;
  206. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  207. UCHAR ucStatus = 0;
  208. UCHAR ucBits = 0;
  209. UCHAR ucDisp = 0;
  210. usAddress = usAddress - 1;
  211. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  212. {
  213. iRegIndex = (USHORT)(usAddress - usRegCoilsStart);
  214. switch(eMode)
  215. {
  216. case MB_REG_READ://读线圈
  217. while(usCoilGroups--)
  218. {
  219. ucDisp = 0;
  220. ucBits = 8;
  221. while((usNCoils--) != 0 && (ucBits--) != 0)
  222. {
  223. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  224. }
  225. *pucRegBuffer++ = ucStatus;
  226. }
  227. break;
  228. case MB_REG_WRITE://写线圈
  229. while(usCoilGroups--)
  230. {
  231. ucStatus = *pucRegBuffer++;
  232. ucBits = 8;
  233. while((usNCoils--) != 0 && (ucBits--) != 0)
  234. {
  235. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  236. ucStatus >>= 1;
  237. }
  238. }
  239. }
  240. }
  241. else//错误
  242. {
  243. eStatus = MB_ENOREG;
  244. }
  245. return eStatus;
  246. }
  247. /****************************************************************************
  248. * 名 称:eMBRegDiscreteCB
  249. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  250. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  251. * usAddress: 寄存器地址
  252. * usNDiscrete: 要读取的寄存器个数
  253. * 出口参数:
  254. * 注 意:1 区
  255. ****************************************************************************/
  256. eMBErrorCode
  257. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  258. {
  259. eMBErrorCode eStatus = MB_ENOERR;
  260. USHORT iRegIndex;
  261. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  262. UCHAR ucStatus = 0;
  263. UCHAR ucBits = 0;
  264. UCHAR ucDisp = 0;
  265. usAddress = usAddress - 1;
  266. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  267. {
  268. iRegIndex = (USHORT)(usAddress - usRegDiscreteStart);
  269. while(usDiscreteGroups--)
  270. {
  271. ucDisp = 0;
  272. ucBits = 8;
  273. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  274. {
  275. if(usRegDiscreteBuf[iRegIndex])
  276. {
  277. ucStatus |= (1 << ucDisp);
  278. }
  279. ucDisp++;
  280. }
  281. *pucRegBuffer++ = ucStatus;
  282. }
  283. }
  284. else//错误
  285. {
  286. eStatus = MB_ENOREG;
  287. }
  288. return eStatus;
  289. }