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 bool RL_Allon_flag;
  55. extern bool RL_Alloff_flag;
  56. extern unsigned long int Delay_Ms;
  57. extern uint8_t Active_state_bit;
  58. extern uint8_t DCPDU_active_chn;
  59. extern uint8_t DCPDU_SlaveAddress;
  60. extern uint32_t DCPDU_Modbus_Reg[DCPDU_Modbus_Reg_len];
  61. extern DCPDU_RELAY_Para_Reg DCPDU_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. T_cnt = Devid_ChalNumAddr - DCPDU_Modbus_RegStAd;
  133. usRegHoldBuf[iRegIndex]= DCPDU_Modbus_Reg[T_cnt];
  134. }
  135. else if((usAddress >= DCPDU_VInput_Addr)&&(usAddress < (DCPDU_RL_Alloff_Addr + DCPDU_RL_Alloff_ByteN)))//读取参数
  136. {
  137. T_cnt = usAddress - DCPDU_Modbus_RegStAd;
  138. memcpy(&usRegHoldBuf[iRegIndex],(DCPDU_Modbus_Reg + T_cnt),usNRegs * 2);
  139. }
  140. else//错误
  141. {
  142. eStatus = MB_ENOREG;
  143. }
  144. while(usNRegs > 0)
  145. {
  146. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
  147. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
  148. iRegIndex++;
  149. usNRegs--;
  150. }
  151. break;
  152. }
  153. case MB_REG_WRITE://写寄存器
  154. {
  155. WusNRegs = usNRegs;
  156. while(usNRegs > 0)
  157. {
  158. usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
  159. usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
  160. iRegIndex++;
  161. usNRegs--;
  162. }
  163. if(usAddress == Devid_ChalNumAddr)//写入通道
  164. {
  165. T_cnt = Devid_ChalNumAddr - DCPDU_Modbus_RegStAd;
  166. memcpy((DCPDU_Modbus_Reg + T_cnt),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
  167. if((DCPDU_Modbus_Reg[T_cnt] < 1)||(DCPDU_Modbus_Reg[T_cnt] > RELAY_ChN_default))
  168. {
  169. DCPDU_Modbus_Reg[T_cnt] = RELAY_ChN_default;
  170. temp0[0] = DCPDU_Modbus_Reg[T_cnt];
  171. }
  172. temp0[0] = DCPDU_Modbus_Reg[T_cnt];
  173. AT24CxxWrite(ERom_RL_Ch_N_addr,temp0,1);
  174. DCPDU_active_chn = temp0[0];
  175. Active_state_bit = 0xff >> (8 - DCPDU_active_chn);
  176. //设置完通道后,通道状态也要变
  177. AT24CxxRead(ERom_RL_ST_Addr,temp0,1);
  178. temp1 = DCPDU_Device_ID << 8;
  179. DCPDU_Modbus_Reg[T_cnt] += temp1;
  180. }
  181. else if((usAddress >= DCPDU_VLit1_max_Addr)&&(usAddress < (DCPDU_RL_Over_Ch8_Addr + DCPDU_RL_Clear_Consumer_ALL_ByteN)))
  182. {
  183. T_cnt = usAddress - DCPDU_Modbus_RegStAd;
  184. memcpy((Modbus_Wr_buff[cmd_cnt]+2),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
  185. Modbus_Wr_buff[cmd_cnt][0] = 1;
  186. Modbus_Wr_buff[cmd_cnt][1] = T_cnt;
  187. cmd_cnt++;
  188. if(cmd_cnt >= Mb_Wcmd_Width)
  189. {
  190. cmd_cnt = 0;
  191. }
  192. }
  193. else//错误
  194. {
  195. eStatus = MB_ENOREG;
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. else//错误
  202. {
  203. eStatus = MB_ENOREG;
  204. }
  205. return eStatus;
  206. }
  207. /****************************************************************************
  208. * 名 称:eMBRegCoilsCB
  209. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  210. * 05 写线圈 eMBFuncWriteCoil
  211. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  212. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  213. * usAddress: 线圈地址
  214. * usNCoils: 要读写的线圈个数
  215. * eMode: 功能码
  216. * 出口参数:
  217. * 注 意:如继电器
  218. * 0 区
  219. ****************************************************************************/
  220. eMBErrorCode
  221. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  222. {
  223. eMBErrorCode eStatus = MB_ENOERR;
  224. USHORT iRegIndex;
  225. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  226. UCHAR ucStatus = 0;
  227. UCHAR ucBits = 0;
  228. UCHAR ucDisp = 0;
  229. usAddress = usAddress - 1;
  230. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  231. {
  232. iRegIndex = (int)(usAddress - usRegCoilsStart);
  233. switch(eMode)
  234. {
  235. case MB_REG_READ://读线圈
  236. while(usCoilGroups--)
  237. {
  238. ucDisp = 0;
  239. ucBits = 8;
  240. while((usNCoils--) != 0 && (ucBits--) != 0)
  241. {
  242. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  243. }
  244. *pucRegBuffer++ = ucStatus;
  245. }
  246. break;
  247. case MB_REG_WRITE://写线圈
  248. while(usCoilGroups--)
  249. {
  250. ucStatus = *pucRegBuffer++;
  251. ucBits = 8;
  252. while((usNCoils--) != 0 && (ucBits--) != 0)
  253. {
  254. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  255. ucStatus >>= 1;
  256. }
  257. }
  258. }
  259. }
  260. else//错误
  261. {
  262. eStatus = MB_ENOREG;
  263. }
  264. return eStatus;
  265. }
  266. /****************************************************************************
  267. * 名 称:eMBRegDiscreteCB
  268. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  269. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  270. * usAddress: 寄存器地址
  271. * usNDiscrete: 要读取的寄存器个数
  272. * 出口参数:
  273. * 注 意:1 区
  274. ****************************************************************************/
  275. eMBErrorCode
  276. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  277. {
  278. eMBErrorCode eStatus = MB_ENOERR;
  279. USHORT iRegIndex;
  280. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  281. UCHAR ucStatus = 0;
  282. UCHAR ucBits = 0;
  283. UCHAR ucDisp = 0;
  284. usAddress = usAddress - 1;
  285. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  286. {
  287. iRegIndex = (int)(usAddress - usRegDiscreteStart);
  288. while(usDiscreteGroups--)
  289. {
  290. ucDisp = 0;
  291. ucBits = 8;
  292. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  293. {
  294. if(usRegDiscreteBuf[iRegIndex])
  295. {
  296. ucStatus |= (1 << ucDisp);
  297. }
  298. ucDisp++;
  299. }
  300. *pucRegBuffer++ = ucStatus;
  301. }
  302. }
  303. else//错误
  304. {
  305. eStatus = MB_ENOREG;
  306. }
  307. return eStatus;
  308. }