port.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "Main.h"
  25. #include "string.h"
  26. #include "At24cxx.h"
  27. #include "ReadEeprom.h"
  28. #include "gd32e230x_conf.h"
  29. #include "Timer.h"
  30. #include "Uart1.h"
  31. /* ----------------------- Defines ------------------------------------------*/
  32. //输入寄存器
  33. #define REG_INPUT_START 3000
  34. #define REG_INPUT_NREGS 4
  35. //保持寄存器
  36. #define REG_HOLD_START Devid_ChalNumAddr
  37. #define REG_HOLD_NREGS 1000
  38. //线圈
  39. #define REG_COILS_START 0
  40. #define REG_COILS_NREGS 4
  41. //开关寄存器
  42. #define REG_DISCRETE_START 1000
  43. #define REG_DISCRETE_NREGS 4
  44. /* ----------------------- Static variables ---------------------------------*/
  45. static USHORT usRegInputStart = REG_INPUT_START;
  46. static USHORT usRegInputBuf[REG_INPUT_NREGS];
  47. static USHORT usRegHoldStart = REG_HOLD_START;
  48. static USHORT usRegHoldBuf[REG_HOLD_NREGS];
  49. static USHORT usRegCoilsStart = REG_COILS_START;
  50. static uint8_t usRegCoilsBuf[REG_COILS_NREGS];
  51. static USHORT usRegDiscreteStart = REG_DISCRETE_START;
  52. static uint8_t usRegDiscreteBuf[REG_DISCRETE_NREGS];
  53. uint8_t cmd_cnt = 0;
  54. uint32_t Modbus_Wr_buff[Mb_Wcmd_Width][Mb_Wcmd_len] = {0};
  55. extern uint32_t dev_info;
  56. extern bool RL_Allon_flag;
  57. extern bool RL_Alloff_flag;
  58. extern unsigned long int Delay_Ms;
  59. extern uint8_t RL_now_state;
  60. extern uint8_t AC3PPDU_SlaveAddress;
  61. extern uint32_t AC3PPDU_Modbus_Reg[AC3PPDU_Modbus_Reg_len];
  62. /****************************************************************************
  63. * 名 称:eMBRegInputCB
  64. * 功 能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
  65. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  66. * usAddress: 寄存器地址
  67. * usNRegs: 要读取的寄存器个数
  68. * 出口参数:
  69. * 注 意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte)
  70. * +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)
  71. * +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
  72. * +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
  73. * 3 区
  74. ****************************************************************************/
  75. eMBErrorCode
  76. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  77. {
  78. eMBErrorCode eStatus = MB_ENOERR;
  79. int iRegIndex;
  80. usAddress = usAddress - 1;
  81. if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
  82. {
  83. iRegIndex = ( int )( usAddress - usRegInputStart );
  84. while( usNRegs > 0 )
  85. {
  86. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
  87. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
  88. iRegIndex++;
  89. usNRegs--;
  90. }
  91. }
  92. else
  93. {
  94. eStatus = MB_ENOREG;
  95. }
  96. return eStatus;
  97. }
  98. /****************************************************************************
  99. * 名 称:eMBRegHoldingCB
  100. * 功 能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister
  101. * 16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister
  102. * 03 读保持寄存器 eMBFuncReadHoldingRegister
  103. * 23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
  104. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  105. * usAddress: 寄存器地址
  106. * usNRegs: 要读写的寄存器个数
  107. * eMode: 功能码
  108. * 出口参数:
  109. * 注 意:4 区
  110. ****************************************************************************/
  111. unsigned char WusNRegs;
  112. unsigned int WiRegIndex;
  113. extern void LimiT_enable_check(uint8_t ch_x,uint32_t Lt_v);
  114. extern AC3PPDU_RELAY_Para_Reg AC3PPDU_RL_time[Output_ChN_default + 2];
  115. extern AC3PPDU_RELAY_Para_Reg AC3PPDU_RL_N_time;
  116. extern uint8_t OutUnit_control_flag[Output_ChN_default +1];
  117. extern uint32_t hlw8110_base[Output_ChN_default + 1];
  118. extern void channel_reset(uint8_t ch_x);
  119. extern void hlw8110_init(uint8_t ch_x);
  120. //extern uint8_t reset_hlw8110_flag[Output_ChN_default + 1];
  121. eMBErrorCode
  122. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
  123. {
  124. uint8_t temp0[4];
  125. uint32_t T_cnt = 0;
  126. uint32_t temp1;
  127. eMBErrorCode eStatus = MB_ENOERR;
  128. int iRegIndex;
  129. usAddress = usAddress - 1;
  130. if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
  131. {
  132. iRegIndex = (int)(usAddress - usRegHoldStart);
  133. WiRegIndex = iRegIndex;
  134. switch(eMode)
  135. {
  136. case MB_REG_READ://读寄存器
  137. {
  138. if((usAddress == Devid_ChalNumAddr)&&(usNRegs == Devid_ChalNumQua)) //读取设备类型和通道
  139. {
  140. AC3PPDU_Modbus_Reg[Mb_Dbuff_DevAddr] = dev_info;
  141. usRegHoldBuf[iRegIndex]= AC3PPDU_Modbus_Reg[Mb_Dbuff_DevAddr];
  142. //memcpy(&usRegHoldBuf[iRegIndex],&dev_info,usNRegs * 2);
  143. //usRegHoldBuf[iRegIndex] = dev_info;
  144. }
  145. else if((usAddress >= AC3PPDU_VAP_Addr)&&(usAddress <= AC3PPDU_RL_Over_Ch9_Addr))//读取参数
  146. {
  147. T_cnt = usAddress - AC3PPDU_Modbus_RegStAd;
  148. memcpy(&usRegHoldBuf[iRegIndex],(AC3PPDU_Modbus_Reg + T_cnt),usNRegs * 2);
  149. }
  150. else//错误
  151. {
  152. eStatus = MB_ENOREG;
  153. }
  154. while(usNRegs > 0)
  155. {
  156. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
  157. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
  158. iRegIndex++;
  159. usNRegs--;
  160. }
  161. break;
  162. }
  163. case MB_REG_WRITE://写寄存器
  164. {
  165. WusNRegs = usNRegs;
  166. while(usNRegs > 0)
  167. {
  168. usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
  169. usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
  170. iRegIndex++;
  171. usNRegs--;
  172. }
  173. /*
  174. if(usAddress == Devid_ChalNumAddr)//写入通道
  175. {
  176. T_cnt = Devid_ChalNumAddr - AC3PPDU_Modbus_RegStAd;
  177. memcpy((AC3PPDU_Modbus_Reg + T_cnt),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
  178. temp0[0] = AC3PPDU_Modbus_Reg[T_cnt];
  179. AT24CxxWrite(ERom_RL_Ch_N_addr,temp0,1);
  180. temp1 = AC3PPDU_Device_ID << 8;
  181. AC3PPDU_Modbus_Reg[T_cnt] += temp1;
  182. }
  183. else */
  184. if((usAddress >= AC3PPDU_ViLit_max_Addr)&&(usAddress <= AC3PPDU_CLEAR_CHN_ALL_CONSUMER))
  185. {
  186. {
  187. T_cnt = usAddress - AC3PPDU_Modbus_RegStAd;
  188. memcpy((Modbus_Wr_buff[cmd_cnt]+2),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
  189. Modbus_Wr_buff[cmd_cnt][0] = 1;
  190. Modbus_Wr_buff[cmd_cnt][1] = T_cnt;
  191. cmd_cnt++;
  192. if(cmd_cnt >= Mb_Wcmd_Width)
  193. {
  194. cmd_cnt = 0;
  195. }
  196. }
  197. }
  198. else//错误
  199. {
  200. eStatus = MB_ENOREG;
  201. }
  202. break;
  203. }
  204. }
  205. }
  206. else//错误
  207. {
  208. eStatus = MB_ENOREG;
  209. }
  210. return eStatus;
  211. }
  212. /****************************************************************************
  213. * 名 称:eMBRegCoilsCB
  214. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  215. * 05 写线圈 eMBFuncWriteCoil
  216. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  217. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  218. * usAddress: 线圈地址
  219. * usNCoils: 要读写的线圈个数
  220. * eMode: 功能码
  221. * 出口参数:
  222. * 注 意:如继电器
  223. * 0 区
  224. ****************************************************************************/
  225. eMBErrorCode
  226. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  227. {
  228. eMBErrorCode eStatus = MB_ENOERR;
  229. USHORT iRegIndex;
  230. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  231. UCHAR ucStatus = 0;
  232. UCHAR ucBits = 0;
  233. UCHAR ucDisp = 0;
  234. usAddress = usAddress - 1;
  235. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  236. {
  237. iRegIndex = (int)(usAddress - usRegCoilsStart);
  238. switch(eMode)
  239. {
  240. case MB_REG_READ://读线圈
  241. while(usCoilGroups--)
  242. {
  243. ucDisp = 0;
  244. ucBits = 8;
  245. while((usNCoils--) != 0 && (ucBits--) != 0)
  246. {
  247. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  248. }
  249. *pucRegBuffer++ = ucStatus;
  250. }
  251. break;
  252. case MB_REG_WRITE://写线圈
  253. while(usCoilGroups--)
  254. {
  255. ucStatus = *pucRegBuffer++;
  256. ucBits = 8;
  257. while((usNCoils--) != 0 && (ucBits--) != 0)
  258. {
  259. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  260. ucStatus >>= 1;
  261. }
  262. }
  263. }
  264. }
  265. else//错误
  266. {
  267. eStatus = MB_ENOREG;
  268. }
  269. return eStatus;
  270. }
  271. /****************************************************************************
  272. * 名 称:eMBRegDiscreteCB
  273. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  274. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  275. * usAddress: 寄存器地址
  276. * usNDiscrete: 要读取的寄存器个数
  277. * 出口参数:
  278. * 注 意:1 区
  279. ****************************************************************************/
  280. eMBErrorCode
  281. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  282. {
  283. eMBErrorCode eStatus = MB_ENOERR;
  284. USHORT iRegIndex;
  285. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  286. UCHAR ucStatus = 0;
  287. UCHAR ucBits = 0;
  288. UCHAR ucDisp = 0;
  289. usAddress = usAddress - 1;
  290. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  291. {
  292. iRegIndex = (int)(usAddress - usRegDiscreteStart);
  293. while(usDiscreteGroups--)
  294. {
  295. ucDisp = 0;
  296. ucBits = 8;
  297. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  298. {
  299. if(usRegDiscreteBuf[iRegIndex])
  300. {
  301. ucStatus |= (1 << ucDisp);
  302. }
  303. ucDisp++;
  304. }
  305. *pucRegBuffer++ = ucStatus;
  306. }
  307. }
  308. else//错误
  309. {
  310. eStatus = MB_ENOREG;
  311. }
  312. return eStatus;
  313. }