sample_mb_master.c 5.4 KB

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