sample_mb_master.c 5.6 KB

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