port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "main.h"
  26. /* ----------------------- Defines ------------------------------------------*/
  27. //输入寄存器
  28. #define REG_INPUT_START 3000
  29. #define REG_INPUT_NREGS 1
  30. //保持寄存器
  31. #define REG_HOLD_START STS_R_START
  32. #define REG_HOLD_NREGS 500
  33. //线圈
  34. #define REG_COILS_START 0
  35. #define REG_COILS_NREGS 1
  36. //开关寄存器
  37. #define REG_DISCRETE_START 0
  38. #define REG_DISCRETE_NREGS 1
  39. /* ----------------------- Static variables ---------------------------------*/
  40. static USHORT usRegInputStart = REG_INPUT_START;
  41. static USHORT usRegInputBuf[REG_INPUT_NREGS];
  42. static USHORT usRegHoldStart = REG_HOLD_START;
  43. static USHORT usRegHoldBuf[REG_HOLD_NREGS];
  44. static USHORT usRegCoilsStart = REG_COILS_START;
  45. static uint8_t usRegCoilsBuf[REG_COILS_NREGS];
  46. static USHORT usRegDiscreteStart = REG_DISCRETE_START;
  47. static uint8_t usRegDiscreteBuf[REG_DISCRETE_NREGS];
  48. extern unsigned long int Delay_Ms;
  49. extern unsigned char detection_value;
  50. /* ----------------------- Start implementation -----------------------------*/
  51. //int
  52. //main( void )
  53. //{
  54. // eMBErrorCode eStatus;
  55. // eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );
  56. // /* Enable the Modbus Protocol Stack. */
  57. // eStatus = eMBEnable( );
  58. // for( ;; )
  59. // {
  60. // ( void )eMBPoll( );
  61. // /* Here we simply count the number of poll cycles. */
  62. // usRegInputBuf[0]++;
  63. // }
  64. //}
  65. /****************************************************************************
  66. * 名 称:eMBRegInputCB
  67. * 功 能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
  68. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  69. * usAddress: 寄存器地址
  70. * usNRegs: 要读取的寄存器个数
  71. * 出口参数:
  72. * 注 意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte)
  73. * +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)
  74. * +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
  75. * +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
  76. * 3 区
  77. ****************************************************************************/
  78. eMBErrorCode
  79. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  80. {
  81. eMBErrorCode eStatus = MB_ENOERR;
  82. int iRegIndex;
  83. usAddress = usAddress - 1;
  84. if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
  85. {
  86. iRegIndex = ( int )( usAddress - usRegInputStart );
  87. while( usNRegs > 0 )
  88. {
  89. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
  90. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
  91. iRegIndex++;
  92. usNRegs--;
  93. }
  94. {
  95. }
  96. }
  97. else
  98. {
  99. eStatus = MB_ENOREG;
  100. }
  101. return eStatus;
  102. }
  103. /****************************************************************************
  104. * 名 称:eMBRegHoldingCB
  105. * 功 能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister
  106. * 16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister
  107. * 03 读保持寄存器 eMBFuncReadHoldingRegister
  108. * 23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
  109. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  110. * usAddress: 寄存器地址
  111. * usNRegs: 要读写的寄存器个数
  112. * eMode: 功能码
  113. * 出口参数:
  114. * 注 意:4 区
  115. ****************************************************************************/
  116. unsigned char Chal_num;
  117. uint16_t WusNRegs;
  118. uint16_t WiRegIndex;
  119. unsigned int WeeiRegIndex;
  120. eMBErrorCode
  121. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
  122. {
  123. eMBErrorCode eStatus = MB_ENOERR;
  124. uint16_t iRegIndex;
  125. usAddress = usAddress - 1;
  126. sts_data_t *t_data = get_sts();
  127. md_data_t *data = &t_data->m_data;
  128. if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
  129. {
  130. iRegIndex = (uint16_t)(usAddress - usRegHoldStart);
  131. WiRegIndex = iRegIndex;
  132. switch(eMode)
  133. {
  134. case MB_REG_READ://读寄存器
  135. {
  136. uint16_t *start = &data->dev;
  137. memcpy(&usRegHoldBuf[iRegIndex],start+iRegIndex,usNRegs*2);
  138. }
  139. while(usNRegs > 0)
  140. {
  141. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
  142. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
  143. iRegIndex++;
  144. usNRegs--;
  145. }
  146. break;
  147. case MB_REG_WRITE://写寄存器
  148. WusNRegs = usNRegs;
  149. while(usNRegs > 0)
  150. {
  151. usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
  152. usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
  153. iRegIndex++;
  154. usNRegs--;
  155. }
  156. {
  157. if(usAddress == STS_OUTPUT_STATUS)
  158. {
  159. uint16_t recive = 0;
  160. memcpy(&recive,&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  161. if(recive == 1)
  162. {
  163. t_data->write_at24(IIC_OUT_CHANNEL,&recive,sizeof(recive));
  164. t_data->open_output();
  165. }else if(recive == 0)
  166. {
  167. t_data->close_output();
  168. t_data->write_at24(IIC_OUT_CHANNEL,&recive,sizeof(recive));
  169. }else
  170. {
  171. eStatus = MB_ENOREG;
  172. }
  173. }else if(usAddress == STS_CHOISE_INPUT)
  174. {
  175. uint16_t recive = 0;
  176. memcpy(&recive,&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  177. if(recive == 0)
  178. {
  179. t_data->choise_input(INPUT_CHANNEL_1);
  180. }else
  181. {
  182. t_data->choise_input(INPUT_CHANNEL_2);
  183. }
  184. }else if(usAddress >= STS_INPUT_THRESHOLD && usAddress < STS_OUTPUT_THRESHOLD)
  185. {
  186. WeeiRegIndex = (uint16_t)(usAddress - STS_INPUT_THRESHOLD);
  187. uint16_t *p_start = &(data->input_th[0].voltage_max);
  188. memcpy(p_start+WeeiRegIndex,&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  189. t_data->write_at24(IIC_INPUT_THRESHOLD_START+(WeeiRegIndex*2),&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  190. for(int i =0 ; i < 2; i++) {
  191. if(data->input_th[i].voltage_max <= SMALL_VALUE)
  192. UNSET_VOLTAGE_MAX_EN(t_data->input_wanning_en[i]);
  193. else
  194. SET_VOLTAGE_MAX_EN(t_data->input_wanning_en[i]);
  195. if(data->input_th[i].voltage_min <= SMALL_VALUE)
  196. UNSET_VOLTAGE_MIN_EN(t_data->input_wanning_en[i]);
  197. else
  198. SET_VOLTAGE_MIN_EN(t_data->input_wanning_en[i]);
  199. if(data->input_th[i].freq_max <= SMALL_VALUE)
  200. UNSET_FREQ_MAX_EN(t_data->input_wanning_en[i]);
  201. else
  202. SET_FREQ_MAX_EN(t_data->input_wanning_en[i]);
  203. if(data->input_th[i].freq_min <= SMALL_VALUE)
  204. UNSET_FREQ_MIN_EN(t_data->input_wanning_en[i]);
  205. else
  206. SET_FREQ_MIN_EN(t_data->input_wanning_en[i]);
  207. }
  208. }else if(usAddress >= STS_OUTPUT_THRESHOLD && usAddress < STS_TEMP_THRESHOLD)
  209. {
  210. WeeiRegIndex = (uint16_t)(usAddress - STS_OUTPUT_THRESHOLD);
  211. uint16_t *p_start = &data->output_th;
  212. memcpy(p_start+WeeiRegIndex,&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  213. t_data->write_at24(IIC_OUTPUT_THRESHOLD_START+(WeeiRegIndex*2),&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  214. if(data->output_th.voltage_max <= SMALL_VALUE)
  215. UNSET_VOLTAGE_MAX_EN(t_data ->output_wanning_en);
  216. else
  217. SET_VOLTAGE_MAX_EN(t_data ->output_wanning_en);
  218. if(data->output_th.voltage_min <= SMALL_VALUE)
  219. UNSET_VOLTAGE_MIN_EN(t_data ->output_wanning_en);
  220. else
  221. SET_VOLTAGE_MIN_EN(t_data ->output_wanning_en);
  222. if(data->output_th.current_max <= SMALL_VALUE)
  223. UNSET_CURRENT_MAX_EN(t_data ->output_wanning_en);
  224. else
  225. SET_CURRENT_MAX_EN(t_data ->output_wanning_en);
  226. if(data->output_th.power_max <= SMALL_VALUE)
  227. UNSET_POWER_MAX_EN(t_data ->output_wanning_en);
  228. else
  229. SET_POWER_MAX_EN(t_data ->output_wanning_en);
  230. if(data->output_th.consumer_max <= SMALL_VALUE)
  231. UNSET_CONSUMER_MAX_EN(t_data ->output_wanning_en);
  232. else
  233. SET_CONSUMER_MAX_EN(t_data ->output_wanning_en);
  234. }else if(usAddress >= STS_TEMP_THRESHOLD && usAddress < STS_OVER_FUNC)
  235. {
  236. WeeiRegIndex = (uint16_t)(usAddress - STS_TEMP_THRESHOLD);
  237. uint16_t *p_start = &data->temprature_th;
  238. memcpy(p_start+WeeiRegIndex,&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  239. t_data->write_at24(IIC_TEMPRATURE_THRESHOLD_START+(WeeiRegIndex*2),&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  240. }else if(usAddress == STS_OVER_FUNC)
  241. {
  242. uint16_t recive = 0;
  243. if(recive > 0x003F)
  244. {
  245. eStatus = MB_ENOREG;
  246. }else{
  247. memcpy(&recive,&usRegHoldBuf[WiRegIndex],WusNRegs*2);
  248. t_data->m_data.over_func = recive;
  249. t_data->write_at24(IIC_OVER_FUNC_START,&recive,sizeof(recive));
  250. }
  251. }else
  252. {
  253. eStatus = MB_ENOREG;
  254. }
  255. }
  256. }
  257. }
  258. else//错误
  259. {
  260. eStatus = MB_ENOREG;
  261. }
  262. return eStatus;
  263. }
  264. /****************************************************************************
  265. * 名 称:eMBRegCoilsCB
  266. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  267. * 05 写线圈 eMBFuncWriteCoil
  268. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  269. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  270. * usAddress: 线圈地址
  271. * usNCoils: 要读写的线圈个数
  272. * eMode: 功能码
  273. * 出口参数:
  274. * 注 意:如继电器
  275. * 0 区
  276. ****************************************************************************/
  277. eMBErrorCode
  278. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  279. {
  280. eMBErrorCode eStatus = MB_ENOERR;
  281. USHORT iRegIndex;
  282. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  283. UCHAR ucStatus = 0;
  284. UCHAR ucBits = 0;
  285. UCHAR ucDisp = 0;
  286. usAddress = usAddress - 1;
  287. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  288. {
  289. iRegIndex = (int)(usAddress - usRegCoilsStart);
  290. switch(eMode)
  291. {
  292. case MB_REG_READ://读线圈
  293. while(usCoilGroups--)
  294. {
  295. ucDisp = 0;
  296. ucBits = 8;
  297. while((usNCoils--) != 0 && (ucBits--) != 0)
  298. {
  299. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  300. }
  301. *pucRegBuffer++ = ucStatus;
  302. }
  303. break;
  304. case MB_REG_WRITE://写线圈
  305. while(usCoilGroups--)
  306. {
  307. ucStatus = *pucRegBuffer++;
  308. ucBits = 8;
  309. while((usNCoils--) != 0 && (ucBits--) != 0)
  310. {
  311. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  312. ucStatus >>= 1;
  313. }
  314. }
  315. }
  316. }
  317. else//错误
  318. {
  319. eStatus = MB_ENOREG;
  320. }
  321. return eStatus;
  322. }
  323. /****************************************************************************
  324. * 名 称:eMBRegDiscreteCB
  325. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  326. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  327. * usAddress: 寄存器地址
  328. * usNDiscrete: 要读取的寄存器个数
  329. * 出口参数:
  330. * 注 意:1 区
  331. ****************************************************************************/
  332. eMBErrorCode
  333. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  334. {
  335. eMBErrorCode eStatus = MB_ENOERR;
  336. USHORT iRegIndex;
  337. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  338. UCHAR ucStatus = 0;
  339. UCHAR ucBits = 0;
  340. UCHAR ucDisp = 0;
  341. usAddress = usAddress - 1;
  342. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  343. {
  344. iRegIndex = (int)(usAddress - usRegDiscreteStart);
  345. while(usDiscreteGroups--)
  346. {
  347. ucDisp = 0;
  348. ucBits = 8;
  349. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  350. {
  351. if(usRegDiscreteBuf[iRegIndex])
  352. {
  353. ucStatus |= (1 << ucDisp);
  354. }
  355. ucDisp++;
  356. }
  357. *pucRegBuffer++ = ucStatus;
  358. }
  359. }
  360. else//错误
  361. {
  362. eStatus = MB_ENOREG;
  363. }
  364. return eStatus;
  365. }