sample_mb_master.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. eMBMasterReqErrCode error_code = MB_MRE_NO_ERR;
  35. rt_uint16_t error_count = 0;
  36. uint8_t log_en_inr = 60; // 打印log的速度 数值越小 打印越频繁
  37. static int8_t log_en_cnt = -1;
  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. error_code = eMBMasterReqReadHoldingRegister(SLAVE_ADDR, MB_SEND_REG_START,2,1000);//读多个保持寄存器
  51. if (chk_log_time)
  52. {
  53. rt_kprintf("error_code: %d \n",error_code);//一直出错,我便把错误返回值打印出来
  54. }
  55. if(error_code==0)
  56. {
  57. device_status.sensor_485_sta = 100;
  58. error_count = 0;
  59. //温度=25并且湿度=30不更新,否则会导致屏幕上显示空
  60. // if((int)usMRegHoldBuf[0][0]/10!=25&&(int)usMRegHoldBuf[0][1]/10!=30)
  61. // {
  62. rt_sprintf(device_status.tempture,"%d.%d",(int)usMRegHoldBuf[0][0]/100, (uint)usMRegHoldBuf[0][0] % 100);
  63. rt_sprintf(device_status.humidity,"%d.%d",(int)usMRegHoldBuf[0][1]/100, (uint)usMRegHoldBuf[0][1] % 100);
  64. // sprintf(device_status.tempture,"%0.01f",(float)usMRegHoldBuf[0][0]/100);
  65. // sprintf(device_status.humidity,"%0.01f",(float)usMRegHoldBuf[0][1]/100);
  66. // }
  67. }
  68. if (chk_log_time)
  69. {
  70. rt_kprintf("slave data-tem:%d,hum:%d\n",usMRegHoldBuf[0][0],usMRegHoldBuf[0][1]);//0代表从机ID=1
  71. rt_kprintf("slave data-tem:%s,hum:%s\n",device_status.tempture,device_status.humidity);//0代表从机ID=1
  72. }
  73. rt_thread_delay(5000);
  74. /* Record the number of errors */
  75. if (error_code != 0)
  76. {
  77. error_count++;
  78. if(error_count == 10)
  79. {
  80. error_count = 0;
  81. device_status.sensor_485_sta = 0;
  82. }
  83. }
  84. }
  85. }
  86. static void mb_master_poll(void *parameter)
  87. {
  88. eMBMasterInit(MB_RTU, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
  89. eMBMasterEnable();
  90. while (1)
  91. {
  92. eMBMasterPoll();
  93. rt_thread_mdelay(MB_POLL_CYCLE_MS);
  94. }
  95. }
  96. int mb_master_samlpe(void)
  97. {
  98. static rt_uint8_t is_init = 0;
  99. rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
  100. if (is_init > 0)
  101. {
  102. rt_kprintf("sample is running\n");
  103. return -RT_ERROR;
  104. }
  105. tid1 = rt_thread_create("md_m_poll", mb_master_poll, RT_NULL, 512, MB_POLL_THREAD_PRIORITY, 10);
  106. if (tid1 != RT_NULL)
  107. {
  108. rt_thread_startup(tid1);
  109. }
  110. else
  111. {
  112. goto __exit;
  113. }
  114. tid2 = rt_thread_create("md_m_send", send_thread_entry, RT_NULL, 512, MB_SEND_THREAD_PRIORITY, 10);
  115. if (tid2 != RT_NULL)
  116. {
  117. rt_thread_startup(tid2);
  118. }
  119. else
  120. {
  121. goto __exit;
  122. }
  123. is_init = 1;
  124. return RT_EOK;
  125. __exit:
  126. if (tid1)
  127. rt_thread_delete(tid1);
  128. if (tid2)
  129. rt_thread_delete(tid2);
  130. return -RT_ERROR;
  131. }
  132. MSH_CMD_EXPORT(mb_master_samlpe, run a modbus master sample);