sample_mb_master.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-06-21 flybreak first version
  9. */
  10. #include <rtthread.h>
  11. #include <user_mb_app.h>
  12. #include "control.h"
  13. #include "hmi.h"
  14. #include "math.h"
  15. #define DBG_ENABLE
  16. #define DBG_COLOR
  17. #define DBG_TAG "mb"
  18. #define DBG_LEVEL DBG_LOG
  19. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  20. #ifdef PKG_MODBUS_MASTER_SAMPLE
  21. #define SLAVE_ADDR MB_SAMPLE_TEST_SLAVE_ADDR
  22. #define PORT_NUM MB_MASTER_USING_PORT_NUM
  23. #define PORT_BAUDRATE MB_MASTER_USING_PORT_BAUDRATE
  24. #else
  25. #define SLAVE_ADDR 0x01
  26. #define PORT_NUM 5
  27. #define PORT_BAUDRATE 9600
  28. #endif
  29. #define PORT_PARITY MB_PAR_NONE
  30. #define MB_POLL_THREAD_PRIORITY 30
  31. #define MB_SEND_THREAD_PRIORITY 31
  32. //#define MB_SEND_REG_START 1034
  33. #define MB_SEND_REG_START 0
  34. #define MB_SEND_REG_NUM 10
  35. #define MB_POLL_CYCLE_MS 500
  36. Device_Status_typedef device_status;
  37. CheckResult check_results;
  38. extern USHORT usMRegInBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_INPUT_NREGS];
  39. extern SHORT usMRegHoldBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_HOLDING_NREGS];
  40. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS)
  41. {
  42. UCHAR SLAVE_ADDRESS = 0x10;
  43. USHORT REGISTER_ADDRESS = 0x9c41;
  44. eMBMasterReqErrCode _code_ret = eMBMasterReqWriteMultipleHoldingRegister(SLAVE_ADDRESS, /* salve address */
  45. REGISTER_ADDRESS, /* register start address */
  46. NREGS, /* register total number */
  47. Buffer, /* data to be written */
  48. RT_WAITING_FOREVER); /* timeout */
  49. return _code_ret;
  50. }
  51. extern eMBMasterReqErrCode write_single_holding_register(UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut)
  52. {
  53. eMBMasterReqErrCode _code_ret = eMBMasterReqWriteHoldingRegister(ucSndAddr,
  54. usRegAddr,
  55. usRegData,
  56. lTimeOut );
  57. return _code_ret;
  58. }
  59. int float_equal(float a, float b, float epsilon) {
  60. return fabsf(a - b) <= epsilon;
  61. }
  62. static void send_thread_entry(void *parameter)
  63. {
  64. BOOL IN_3A = 0; //数据解析方式设置
  65. eMBMasterReqErrCode error_code = MB_MRE_NO_ERR;
  66. rt_uint16_t error_count = 0;
  67. check_results.UART_485_STA = 1;
  68. while (1)
  69. {
  70. #if 1
  71. error_code = eMBMasterReqReadInputRegister(SLAVE_ADDR, MB_SEND_REG_START,3,1000);
  72. rt_kprintf("eMBMasterReqReadInputRegister: %d \n",error_code);//一直出错,把错误返回值打印出来
  73. if(error_code==0)
  74. {
  75. if(IN_3A==0){
  76. error_code = eMBMasterReqWriteHoldingRegister(0,0x3A,1,1000);
  77. IN_3A = 1;
  78. rt_kprintf("数据解析方式设置:%d\n",error_code);
  79. }
  80. device_status.sensor_485_sta = 200;
  81. error_count = 0;
  82. for(int i=0;i<3;i++){
  83. rt_kprintf("usMRegInBuf:%d-%u\n",i,usMRegInBuf[0][i]);
  84. }
  85. check_results.VOLTAGE_5V = (float)usMRegInBuf[0][0]/100;
  86. check_results.VOLTAGE_12V = (float)usMRegInBuf[0][1]/100;
  87. check_results.VOLTAGE_3_3V = (float)usMRegInBuf[0][2]/100;
  88. if(float_equal(check_results.VOLTAGE_5V,5.0f,0.5f))
  89. {
  90. LOG_I("5V测量值在误差范围内");
  91. check_results.VOLTAGE_5V_STA = 0;
  92. }else {
  93. check_results.VOLTAGE_5V_STA = 1;
  94. LOG_E("超过允许误差:期望%.2f,实际%.2f", 5.00f, check_results.VOLTAGE_5V);
  95. }
  96. if(float_equal(check_results.VOLTAGE_12V,12.60,0.50))
  97. {
  98. check_results.VOLTAGE_12V_STA = 0;
  99. LOG_I("12V测量值在误差范围内");
  100. }else {
  101. check_results.VOLTAGE_12V_STA = 1;
  102. LOG_E("超过允许误差:期望%.2f,实际%.2f", 12.60f, check_results.VOLTAGE_12V);
  103. }
  104. if(float_equal(check_results.VOLTAGE_3_3V,3.30,0.20))
  105. {
  106. check_results.VOLTAGE_3V3_STA = 0;
  107. LOG_I("3.3V测量值在误差范围内");
  108. }else {
  109. check_results.VOLTAGE_3V3_STA = 1;
  110. LOG_E("超过允许误差:期望%.2f,实际%.2f", 3.30f, check_results.VOLTAGE_3_3V);
  111. }
  112. //太阳能板电压
  113. LOG_I("采集电压5V:%0.01f V\"",check_results.VOLTAGE_5V);
  114. LOG_I("采集电压12V:%0.01f V\"",check_results.VOLTAGE_12V);
  115. LOG_I("采集电压3.3V:%0.01f V\"",check_results.VOLTAGE_3_3V);
  116. check_results.UART_485_STA = 0;
  117. }else{
  118. check_results.VOLTAGE_12V_STA = 1;
  119. check_results.VOLTAGE_3V3_STA = 1;
  120. check_results.VOLTAGE_5V_STA = 1;
  121. check_results.UART_485_STA = 1;
  122. }
  123. #endif
  124. // if(device_status.sensor_485_sta!=200){
  125. //
  126. // error_code = eMBMasterReqReadHoldingRegister(SLAVE_ADDR, MB_SEND_REG_START,2,1000);//读多个保持寄存器
  127. // // 发送写多个保持寄存器请求
  128. // //error_code = eMBMasterReqWriteMultipleHoldingRegister(SLAVE_ADDRESS,REGISTER_ADDRESS,REGISTER_COUNT,usDataBuffer,1000);
  129. // rt_kprintf("eMBMasterReqReadHoldingRegister: %d \n",error_code);//一直出错,我便把错误返回值打印出来
  130. // if(error_code==0)
  131. // {
  132. // device_status.sensor_485_sta = 100;
  133. // error_count = 0;
  134. // //温度=25并且湿度=30不更新,否则会导致屏幕上显示空
  135. // // if((int)usMRegHoldBuf[0][0]/10!=25&&(int)usMRegHoldBuf[0][1]/10!=30)
  136. // // {
  137. // rt_sprintf(device_status.tempture,"%d.%d",(int)usMRegHoldBuf[0][0]/100, (unsigned int)usMRegHoldBuf[0][0] % 100);
  138. // rt_sprintf(device_status.humidity,"%d.%d",(int)usMRegHoldBuf[0][1]/100, (unsigned int)usMRegHoldBuf[0][1] % 100);
  139. // // sprintf(device_status.tempture,"%0.01f",(float)usMRegHoldBuf[0][0]/100);
  140. // // sprintf(device_status.humidity,"%0.01f",(float)usMRegHoldBuf[0][1]/100);
  141. // // }
  142. // //rt_thread_delay(1000);
  143. // }
  144. // }
  145. // rt_kprintf("slave data-tem:%d,hum:%d\n",usMRegHoldBuf[0][0],usMRegHoldBuf[0][1]);//0代表从机ID=1
  146. // rt_kprintf("slave data-tem:%s,hum:%s\n",device_status.tempture,device_status.humidity);//0代表从机ID=1
  147. // rt_thread_delay(5000);
  148. // /* Record the number of errors */
  149. // if (error_code != 0)
  150. // {
  151. // error_count++;
  152. // if(error_count == 10)
  153. // {
  154. // error_count = 0;
  155. // device_status.sensor_485_sta = 0;
  156. // }
  157. // }
  158. }
  159. }
  160. static void mb_master_poll(void *parameter)
  161. {
  162. eMBMasterInit(MB_RTU, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
  163. eMBMasterEnable();
  164. while (1)
  165. {
  166. eMBMasterPoll();
  167. rt_thread_mdelay(MB_POLL_CYCLE_MS);
  168. }
  169. }
  170. int mb_master_samlpe(void)
  171. {
  172. USHORT qidong[] = {0xbfd8,0xd6c6,0xc4a3,0xbfe9,0xd2d1,0xc6f4,0xb6af};
  173. static rt_uint8_t is_init = 0;
  174. rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
  175. if (is_init > 0)
  176. {
  177. rt_kprintf("sample is running\n");
  178. return -RT_ERROR;
  179. }
  180. tid1 = rt_thread_create("md_m_poll", mb_master_poll, RT_NULL, 512, MB_POLL_THREAD_PRIORITY, 10);
  181. if (tid1 != RT_NULL)
  182. {
  183. rt_thread_startup(tid1);
  184. }
  185. else
  186. {
  187. goto __exit;
  188. }
  189. tid2 = rt_thread_create("md_m_send", send_thread_entry, RT_NULL, 1024, MB_SEND_THREAD_PRIORITY, 10);
  190. if (tid2 != RT_NULL)
  191. {
  192. rt_thread_startup(tid2);
  193. }
  194. else
  195. {
  196. goto __exit;
  197. }
  198. is_init = 1;
  199. rt_thread_delay(10000);
  200. _485_play(qidong,7);
  201. return RT_EOK;
  202. __exit:
  203. if (tid1)
  204. rt_thread_delete(tid1);
  205. if (tid2)
  206. rt_thread_delete(tid2);
  207. return -RT_ERROR;
  208. }
  209. MSH_CMD_EXPORT(mb_master_samlpe, run a modbus master sample);