sample_mb_master.c 5.9 KB

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