port.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. /* ----------------------- Defines ------------------------------------------*/
  31. //输入寄存器
  32. #define REG_INPUT_START 3000
  33. #define REG_INPUT_NREGS 4
  34. //保持寄存器
  35. #define REG_HOLD_START Devid_ChalNumAddr
  36. #define REG_HOLD_NREGS 1000
  37. //线圈
  38. #define REG_COILS_START 0
  39. #define REG_COILS_NREGS 4
  40. //开关寄存器
  41. #define REG_DISCRETE_START 1000
  42. #define REG_DISCRETE_NREGS 4
  43. /* ----------------------- Static variables ---------------------------------*/
  44. static USHORT usRegInputStart = REG_INPUT_START;
  45. static USHORT usRegInputBuf[REG_INPUT_NREGS];
  46. static USHORT usRegHoldStart = REG_HOLD_START;
  47. static USHORT usRegHoldBuf[REG_HOLD_NREGS];
  48. static USHORT usRegCoilsStart = REG_COILS_START;
  49. static uint8_t usRegCoilsBuf[REG_COILS_NREGS];
  50. static USHORT usRegDiscreteStart = REG_DISCRETE_START;
  51. static uint8_t usRegDiscreteBuf[REG_DISCRETE_NREGS];
  52. uint8_t cmd_cnt = 0;
  53. uint32_t Modbus_Wr_buff[Mb_Wcmd_Width][Mb_Wcmd_len] = {0};
  54. extern uint32_t dev_info;
  55. extern bool RL_Allon_flag;
  56. extern bool RL_Alloff_flag;
  57. extern unsigned long int Delay_Ms;
  58. extern uint8_t RL_now_state;
  59. extern uint8_t AC3PPDU_SlaveAddress;
  60. extern uint32_t AC3PPDU_Modbus_Reg[AC3PPDU_Modbus_Reg_len];
  61. extern AC3PPDU_RELAY_Para_Reg AC3PPDU_RL_time;
  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. eMBErrorCode
  114. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
  115. {
  116. uint8_t temp0[4];
  117. uint32_t T_cnt = 0;
  118. uint32_t temp1;
  119. eMBErrorCode eStatus = MB_ENOERR;
  120. int iRegIndex;
  121. usAddress = usAddress - 1;
  122. if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
  123. {
  124. iRegIndex = (int)(usAddress - usRegHoldStart);
  125. WiRegIndex = iRegIndex;
  126. switch(eMode)
  127. {
  128. case MB_REG_READ://读寄存器
  129. {
  130. if((usAddress == Devid_ChalNumAddr)&&(usNRegs == Devid_ChalNumQua)) //读取设备类型和通道
  131. {
  132. AC3PPDU_Modbus_Reg[Mb_Dbuff_DevAddr] = dev_info;
  133. usRegHoldBuf[iRegIndex]= AC3PPDU_Modbus_Reg[Mb_Dbuff_DevAddr];
  134. //memcpy(&usRegHoldBuf[iRegIndex],&dev_info,usNRegs * 2);
  135. //usRegHoldBuf[iRegIndex] = dev_info;
  136. }
  137. else if((usAddress >= AC3PPDU_VAP_Addr)&&(usAddress <= AC3PPDU_RL_Over_Ch9_Addr))//读取参数
  138. {
  139. T_cnt = usAddress - AC3PPDU_Modbus_RegStAd;
  140. memcpy(&usRegHoldBuf[iRegIndex],(AC3PPDU_Modbus_Reg + T_cnt),usNRegs * 2);
  141. }
  142. else//错误
  143. {
  144. eStatus = MB_ENOREG;
  145. }
  146. while(usNRegs > 0)
  147. {
  148. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
  149. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
  150. iRegIndex++;
  151. usNRegs--;
  152. }
  153. break;
  154. }
  155. case MB_REG_WRITE://写寄存器
  156. {
  157. WusNRegs = usNRegs;
  158. while(usNRegs > 0)
  159. {
  160. usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
  161. usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
  162. iRegIndex++;
  163. usNRegs--;
  164. }
  165. /*
  166. if(usAddress == Devid_ChalNumAddr)//写入通道
  167. {
  168. T_cnt = Devid_ChalNumAddr - AC3PPDU_Modbus_RegStAd;
  169. memcpy((AC3PPDU_Modbus_Reg + T_cnt),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
  170. temp0[0] = AC3PPDU_Modbus_Reg[T_cnt];
  171. AT24CxxWrite(ERom_RL_Ch_N_addr,temp0,1);
  172. temp1 = AC3PPDU_Device_ID << 8;
  173. AC3PPDU_Modbus_Reg[T_cnt] += temp1;
  174. }
  175. else */
  176. if((usAddress >= AC3PPDU_ViLit_max_Addr)&&(usAddress <= AC3PPDU_CLEAR_CHN_ALL_CONSUMER))
  177. {
  178. {
  179. T_cnt = usAddress - AC3PPDU_Modbus_RegStAd;
  180. memcpy((Modbus_Wr_buff[cmd_cnt]+2),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
  181. Modbus_Wr_buff[cmd_cnt][0] = 1;
  182. Modbus_Wr_buff[cmd_cnt][1] = T_cnt;
  183. cmd_cnt++;
  184. if(cmd_cnt >= Mb_Wcmd_Width)
  185. {
  186. cmd_cnt = 0;
  187. }
  188. }
  189. }
  190. else//错误
  191. {
  192. eStatus = MB_ENOREG;
  193. }
  194. break;
  195. }
  196. }
  197. }
  198. else//错误
  199. {
  200. eStatus = MB_ENOREG;
  201. }
  202. return eStatus;
  203. }
  204. /****************************************************************************
  205. * 名 称:eMBRegCoilsCB
  206. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  207. * 05 写线圈 eMBFuncWriteCoil
  208. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  209. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  210. * usAddress: 线圈地址
  211. * usNCoils: 要读写的线圈个数
  212. * eMode: 功能码
  213. * 出口参数:
  214. * 注 意:如继电器
  215. * 0 区
  216. ****************************************************************************/
  217. eMBErrorCode
  218. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  219. {
  220. eMBErrorCode eStatus = MB_ENOERR;
  221. USHORT iRegIndex;
  222. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  223. UCHAR ucStatus = 0;
  224. UCHAR ucBits = 0;
  225. UCHAR ucDisp = 0;
  226. usAddress = usAddress - 1;
  227. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  228. {
  229. iRegIndex = (int)(usAddress - usRegCoilsStart);
  230. switch(eMode)
  231. {
  232. case MB_REG_READ://读线圈
  233. while(usCoilGroups--)
  234. {
  235. ucDisp = 0;
  236. ucBits = 8;
  237. while((usNCoils--) != 0 && (ucBits--) != 0)
  238. {
  239. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  240. }
  241. *pucRegBuffer++ = ucStatus;
  242. }
  243. break;
  244. case MB_REG_WRITE://写线圈
  245. while(usCoilGroups--)
  246. {
  247. ucStatus = *pucRegBuffer++;
  248. ucBits = 8;
  249. while((usNCoils--) != 0 && (ucBits--) != 0)
  250. {
  251. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  252. ucStatus >>= 1;
  253. }
  254. }
  255. }
  256. }
  257. else//错误
  258. {
  259. eStatus = MB_ENOREG;
  260. }
  261. return eStatus;
  262. }
  263. /****************************************************************************
  264. * 名 称:eMBRegDiscreteCB
  265. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  266. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  267. * usAddress: 寄存器地址
  268. * usNDiscrete: 要读取的寄存器个数
  269. * 出口参数:
  270. * 注 意:1 区
  271. ****************************************************************************/
  272. eMBErrorCode
  273. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  274. {
  275. eMBErrorCode eStatus = MB_ENOERR;
  276. USHORT iRegIndex;
  277. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  278. UCHAR ucStatus = 0;
  279. UCHAR ucBits = 0;
  280. UCHAR ucDisp = 0;
  281. usAddress = usAddress - 1;
  282. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  283. {
  284. iRegIndex = (int)(usAddress - usRegDiscreteStart);
  285. while(usDiscreteGroups--)
  286. {
  287. ucDisp = 0;
  288. ucBits = 8;
  289. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  290. {
  291. if(usRegDiscreteBuf[iRegIndex])
  292. {
  293. ucStatus |= (1 << ucDisp);
  294. }
  295. ucDisp++;
  296. }
  297. *pucRegBuffer++ = ucStatus;
  298. }
  299. }
  300. else//错误
  301. {
  302. eStatus = MB_ENOREG;
  303. }
  304. return eStatus;
  305. }