port.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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[128];
  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 + 200)))
  129. {
  130. iRegIndex = (uint16_t)(usAddress - usRegHoldStart);
  131. WiRegIndex = iRegIndex;
  132. switch(eMode)
  133. {
  134. case MB_REG_READ://读寄存器
  135. {
  136. {
  137. uint16_t *start = (uint16_t*)data;
  138. memcpy(&usRegHoldBuf[0],(uint8_t*)(start+iRegIndex),usNRegs*2);
  139. }
  140. int i = 0;
  141. while(usNRegs > 0)
  142. {
  143. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[i] >> 8);
  144. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[i] & 0xFF);
  145. i++;
  146. usNRegs--;
  147. }
  148. }
  149. break;
  150. case MB_REG_WRITE://写寄存器
  151. {
  152. WusNRegs = usNRegs;
  153. int i = 0;
  154. while(usNRegs > 0)
  155. {
  156. usRegHoldBuf[i] = *pucRegBuffer++ << 8;
  157. usRegHoldBuf[i] |= *pucRegBuffer++;
  158. i++;
  159. usNRegs--;
  160. }
  161. {
  162. if(usAddress == STS_RW_CHIOSE_START)
  163. {
  164. (usRegHoldBuf[0]== 0) ? t_data->choise_input(INPUT_CHANNEL_1) : t_data->choise_input(INPUT_CHANNEL_2);
  165. }else if(usAddress >= STS_RW_EEP_STATUS_START && usAddress < STS_RW_EEP_ELE_CLEAR_CONSUMER)
  166. {
  167. WeeiRegIndex = (uint16_t)(usAddress - STS_RW_EEP_STATUS_START);
  168. uint16_t *p_start = (uint16_t *)(&(data->m_rw.rw_data.md_eep));
  169. memcpy(p_start+WeeiRegIndex,&usRegHoldBuf[0],WusNRegs*2);
  170. if(usAddress == STS_RW_EEP_STATUS_START)
  171. {
  172. usRegHoldBuf[0] ? t_data->open_output():t_data->close_output();
  173. }else if(usAddress > STS_RW_EEP_INPUT_TH_START && usAddress <=STS_RW_EEP_OUTPUT_TH_START)
  174. {
  175. for(int i =0 ; i < 2; i++) {
  176. if(data->m_rw.rw_data.md_eep.input_th[i].voltage_max_1 <= SMALL_VALUE)
  177. t_data->input_wanning_en[i].vmax_en = 0;
  178. else
  179. t_data->input_wanning_en[i].vmax_en = 1;
  180. if(data->m_rw.rw_data.md_eep.input_th[i].voltage_min_1 <= SMALL_VALUE)
  181. t_data->input_wanning_en[i].vmin_en = 0;
  182. else
  183. t_data->input_wanning_en[i].vmin_en = 1;
  184. if(data->m_rw.rw_data.md_eep.input_th[i].freq_max_1 <= SMALL_VALUE)
  185. t_data->input_wanning_en[i].freqmax_en = 0;
  186. else
  187. t_data->input_wanning_en[i].freqmax_en = 1;
  188. if(data->m_rw.rw_data.md_eep.input_th[i].freq_min_1 <= SMALL_VALUE)
  189. t_data->input_wanning_en[i].freqmin_en = 0;
  190. else
  191. t_data->input_wanning_en[i].freqmin_en = 1;
  192. }
  193. }else if(usAddress >= STS_RW_EEP_OUTPUT_TH_START && usAddress < STS_RW_EEP_TEMP_TH_START)
  194. {
  195. if(t_data->m_data.m_rw.rw_data.md_eep.output_th.voltage_max_1 <= SMALL_VALUE)
  196. t_data->output_wanning_en.vmax_en = 0;
  197. else
  198. t_data->output_wanning_en.vmax_en = 1;
  199. if(t_data->m_data.m_rw.rw_data.md_eep.output_th.voltage_min_1 <= SMALL_VALUE)
  200. t_data->output_wanning_en.vmin_en = 0;
  201. else
  202. t_data->output_wanning_en.vmin_en = 1;
  203. if(t_data->m_data.m_rw.rw_data.md_eep.output_th.current_max_1 <= SMALL_VALUE)
  204. t_data->output_wanning_en.imax_en = 0;
  205. else
  206. t_data->output_wanning_en.imax_en = 1;
  207. if(t_data->m_data.m_rw.rw_data.md_eep.output_th.power_max_1 <= SMALL_VALUE)
  208. t_data->output_wanning_en.pmax_en = 0;
  209. else
  210. t_data->output_wanning_en.pmax_en = 1;
  211. if(t_data->m_data.m_rw.rw_data.md_eep.output_th.consumer_max_1 <= SMALL_VALUE)
  212. t_data->output_wanning_en.cmax_en = 0;
  213. else
  214. t_data->output_wanning_en.cmax_en = 1;
  215. }else if(usAddress >= STS_RW_EEP_TEMP_TH_START && usAddress < STS_RW_EEP_ELE_V_I_ARGV)
  216. {
  217. if(t_data->m_data.m_rw.rw_data.md_eep.temp_th.max_temp > 0x7fff) t_data->m_data.m_rw.rw_data.md_eep.temp_th.max_temp = 0;
  218. if(t_data->m_data.m_rw.rw_data.md_eep.temp_th.min_temp > 0x7fff) t_data->m_data.m_rw.rw_data.md_eep.temp_th.min_temp = 0;
  219. }
  220. t_data->write_eeprom_page(EEP_MD_START+(WeeiRegIndex*2),&usRegHoldBuf[0],WusNRegs*2);
  221. }else if(usAddress == STS_RW_EEP_ELE_CLEAR_CONSUMER)
  222. {
  223. t_data->iic_consumer = 0;
  224. }else if(usAddress == STS_RW_EEP_ELE_CLEAR_BEEP)
  225. {
  226. t_data->beep_flag = 0;
  227. t_data->i_beep_flag[0].beep_waning = 0;
  228. t_data->i_beep_flag[1].beep_waning = 0;
  229. t_data->o_beep_flag.beep_waning = 0;
  230. beep_timer_stop();
  231. t_data->clear_beep();
  232. }
  233. else
  234. {
  235. eStatus = MB_ENOREG;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. else//错误
  242. {
  243. eStatus = MB_ENOREG;
  244. }
  245. return eStatus;
  246. }
  247. /****************************************************************************
  248. * 名 称:eMBRegCoilsCB
  249. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  250. * 05 写线圈 eMBFuncWriteCoil
  251. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  252. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  253. * usAddress: 线圈地址
  254. * usNCoils: 要读写的线圈个数
  255. * eMode: 功能码
  256. * 出口参数:
  257. * 注 意:如继电器
  258. * 0 区
  259. ****************************************************************************/
  260. eMBErrorCode
  261. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  262. {
  263. eMBErrorCode eStatus = MB_ENOERR;
  264. USHORT iRegIndex;
  265. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  266. UCHAR ucStatus = 0;
  267. UCHAR ucBits = 0;
  268. UCHAR ucDisp = 0;
  269. usAddress = usAddress - 1;
  270. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  271. {
  272. iRegIndex = (int)(usAddress - usRegCoilsStart);
  273. switch(eMode)
  274. {
  275. case MB_REG_READ://读线圈
  276. while(usCoilGroups--)
  277. {
  278. ucDisp = 0;
  279. ucBits = 8;
  280. while((usNCoils--) != 0 && (ucBits--) != 0)
  281. {
  282. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  283. }
  284. *pucRegBuffer++ = ucStatus;
  285. }
  286. break;
  287. case MB_REG_WRITE://写线圈
  288. while(usCoilGroups--)
  289. {
  290. ucStatus = *pucRegBuffer++;
  291. ucBits = 8;
  292. while((usNCoils--) != 0 && (ucBits--) != 0)
  293. {
  294. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  295. ucStatus >>= 1;
  296. }
  297. }
  298. }
  299. }
  300. else//错误
  301. {
  302. eStatus = MB_ENOREG;
  303. }
  304. return eStatus;
  305. }
  306. /****************************************************************************
  307. * 名 称:eMBRegDiscreteCB
  308. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  309. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  310. * usAddress: 寄存器地址
  311. * usNDiscrete: 要读取的寄存器个数
  312. * 出口参数:
  313. * 注 意:1 区
  314. ****************************************************************************/
  315. eMBErrorCode
  316. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  317. {
  318. eMBErrorCode eStatus = MB_ENOERR;
  319. USHORT iRegIndex;
  320. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  321. UCHAR ucStatus = 0;
  322. UCHAR ucBits = 0;
  323. UCHAR ucDisp = 0;
  324. usAddress = usAddress - 1;
  325. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  326. {
  327. iRegIndex = (int)(usAddress - usRegDiscreteStart);
  328. while(usDiscreteGroups--)
  329. {
  330. ucDisp = 0;
  331. ucBits = 8;
  332. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  333. {
  334. if(usRegDiscreteBuf[iRegIndex])
  335. {
  336. ucStatus |= (1 << ucDisp);
  337. }
  338. ucDisp++;
  339. }
  340. *pucRegBuffer++ = ucStatus;
  341. }
  342. }
  343. else//错误
  344. {
  345. eStatus = MB_ENOREG;
  346. }
  347. return eStatus;
  348. }