sample_mb_master.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #ifdef PKG_MODBUS_MASTER_SAMPLE
  15. #define SLAVE_ADDR MB_SAMPLE_TEST_SLAVE_ADDR
  16. #define PORT_NUM MB_MASTER_USING_PORT_NUM
  17. #define PORT_BAUDRATE MB_MASTER_USING_PORT_BAUDRATE
  18. #else
  19. #define SLAVE_ADDR 0x01
  20. #define PORT_NUM 5
  21. #define PORT_BAUDRATE 9600
  22. #endif
  23. #define PORT_PARITY MB_PAR_NONE
  24. #define MB_POLL_THREAD_PRIORITY 30
  25. #define MB_SEND_THREAD_PRIORITY 31
  26. //#define MB_SEND_REG_START 1034
  27. #define MB_SEND_REG_START 0
  28. #define MB_SEND_REG_NUM 10
  29. #define MB_POLL_CYCLE_MS 500
  30. Device_Status_typedef device_status;
  31. extern USHORT usMRegInBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_INPUT_NREGS];
  32. extern SHORT usMRegHoldBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_HOLDING_NREGS];
  33. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS)
  34. {
  35. UCHAR SLAVE_ADDRESS = 0x10;
  36. USHORT REGISTER_ADDRESS = 0x9c41;
  37. eMBMasterReqErrCode _code_ret = eMBMasterReqWriteMultipleHoldingRegister(SLAVE_ADDRESS, /* salve address */
  38. REGISTER_ADDRESS, /* register start address */
  39. NREGS, /* register total number */
  40. Buffer, /* data to be written */
  41. RT_WAITING_FOREVER); /* timeout */
  42. return _code_ret;
  43. }
  44. static void send_thread_entry(void *parameter)
  45. {
  46. BOOL IN_3A = 0; //数据解析方式设置
  47. eMBMasterReqErrCode error_code = MB_MRE_NO_ERR;
  48. rt_uint16_t error_count = 0;
  49. rt_uint16_t ok_count = 0;
  50. while (1)
  51. {
  52. #if 0
  53. error_code = eMBMasterReqReadInputRegister(SLAVE_ADDR, MB_SEND_REG_START,3,1000);
  54. rt_kprintf("eMBMasterReqReadInputRegister: %d \n",error_code);//一直出错,把错误返回值打印出来
  55. if(error_code==0)
  56. {
  57. if(IN_3A==0){
  58. error_code = eMBMasterReqWriteHoldingRegister(0,0x3A,1,1000);
  59. IN_3A = 1;
  60. rt_kprintf("数据解析方式设置:%d\n",error_code);
  61. }
  62. device_status.sensor_485_sta = 200;
  63. error_count = 0;
  64. for(int i=0;i<3;i++){
  65. rt_kprintf("usMRegInBuf:%d-%u\n",i,usMRegInBuf[0][i]);
  66. }
  67. rt_sprintf(device_status.voltage_sun,"%d.%d",usMRegInBuf[0][0]/100,usMRegInBuf[0][0]%100/10);
  68. //太阳能板电压
  69. HMI_WRITE("menu.g0.txt=\"太阳能板电压:%sV\"",device_status.voltage_sun);
  70. }else{
  71. rt_sprintf(device_status.voltage_sun,"%s","0.0");
  72. }
  73. #endif
  74. if(device_status.sensor_485_sta!=200){
  75. error_code = eMBMasterReqReadHoldingRegister(SLAVE_ADDR, MB_SEND_REG_START,2,1000);//读多个保持寄存器
  76. // 发送写多个保持寄存器请求
  77. //error_code = eMBMasterReqWriteMultipleHoldingRegister(SLAVE_ADDRESS,REGISTER_ADDRESS,REGISTER_COUNT,usDataBuffer,1000);
  78. rt_kprintf("eMBMasterReqReadHoldingRegister: %d \n",error_code);//一直出错,我便把错误返回值打印出来
  79. if(error_code==0)
  80. {
  81. device_status.sensor_485_sta = 100;
  82. error_count = 0;
  83. ok_count++;
  84. //温度=25并且湿度=30不更新,否则会导致屏幕上显示空
  85. // if((int)usMRegHoldBuf[0][0]/10!=25&&(int)usMRegHoldBuf[0][1]/10!=30)
  86. // {
  87. rt_sprintf(device_status.tempture,"%d.%d",(int)usMRegHoldBuf[0][0]/100, (unsigned int)usMRegHoldBuf[0][0] % 100);
  88. rt_sprintf(device_status.humidity,"%d.%d",(int)usMRegHoldBuf[0][1]/100, (unsigned int)usMRegHoldBuf[0][1] % 100);
  89. // sprintf(device_status.tempture,"%0.01f",(float)usMRegHoldBuf[0][0]/100);
  90. // sprintf(device_status.humidity,"%0.01f",(float)usMRegHoldBuf[0][1]/100);
  91. // }
  92. //rt_thread_delay(1000);
  93. }
  94. }
  95. rt_kprintf("slave data-tem:%d,hum:%d\n",usMRegHoldBuf[0][0],usMRegHoldBuf[0][1]);//0代表从机ID=1
  96. rt_kprintf("slave data-tem:%s,hum:%s\n",device_status.tempture,device_status.humidity);//0代表从机ID=1
  97. rt_thread_delay(5000);
  98. /* Record the number of errors */
  99. if (error_code != 0)
  100. {
  101. error_count++;
  102. if(error_count == 10)
  103. {
  104. error_count = 0;
  105. device_status.sensor_485_sta = 0;
  106. }
  107. }
  108. }
  109. }
  110. static void mb_master_poll(void *parameter)
  111. {
  112. eMBMasterInit(MB_RTU, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
  113. eMBMasterEnable();
  114. while (1)
  115. {
  116. eMBMasterPoll();
  117. rt_thread_mdelay(MB_POLL_CYCLE_MS);
  118. }
  119. }
  120. int mb_master_samlpe(void)
  121. {
  122. USHORT qidong[] = {0xbfd8,0xd6c6,0xc4a3,0xbfe9,0xd2d1,0xc6f4,0xb6af};
  123. static rt_uint8_t is_init = 0;
  124. rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
  125. if (is_init > 0)
  126. {
  127. rt_kprintf("sample is running\n");
  128. return -RT_ERROR;
  129. }
  130. tid1 = rt_thread_create("md_m_poll", mb_master_poll, RT_NULL, 512, MB_POLL_THREAD_PRIORITY, 10);
  131. if (tid1 != RT_NULL)
  132. {
  133. rt_thread_startup(tid1);
  134. }
  135. else
  136. {
  137. goto __exit;
  138. }
  139. tid2 = rt_thread_create("md_m_send", send_thread_entry, RT_NULL, 1024, MB_SEND_THREAD_PRIORITY, 10);
  140. if (tid2 != RT_NULL)
  141. {
  142. rt_thread_startup(tid2);
  143. }
  144. else
  145. {
  146. goto __exit;
  147. }
  148. is_init = 1;
  149. rt_thread_delay(10000);
  150. _485_play(qidong,7);
  151. return RT_EOK;
  152. __exit:
  153. if (tid1)
  154. rt_thread_delete(tid1);
  155. if (tid2)
  156. rt_thread_delete(tid2);
  157. return -RT_ERROR;
  158. }
  159. MSH_CMD_EXPORT(mb_master_samlpe, run a modbus master sample);