sample_mb_master.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. //USHORT data[3] = {0};
  37. while (1)
  38. {
  39. /* Test Modbus Master */
  40. // data[0] = (USHORT)(rt_tick_get() / 10);
  41. // data[1] = (USHORT)(rt_tick_get() % 10);
  42. // data[3] = data[1] + 1;
  43. // error_code = eMBMasterReqWriteMultipleHoldingRegister(SLAVE_ADDR, /* salve address */
  44. // MB_SEND_REG_START, /* register start address */
  45. // MB_SEND_REG_NUM, /* register total number */
  46. // data, /* data to be written */
  47. // RT_WAITING_FOREVER); /* timeout */
  48. error_code = eMBMasterReqReadHoldingRegister(SLAVE_ADDR, MB_SEND_REG_START,2,1000);//读多个保持寄存器
  49. rt_kprintf("error_code: %d \n",error_code);//一直出错,我便把错误返回值打印出来
  50. if(error_code==0)
  51. {
  52. device_status.sensor_485_sta = 100;
  53. error_count = 0;
  54. //温度=25并且湿度=30不更新,否则会导致屏幕上显示空
  55. // if((int)usMRegHoldBuf[0][0]/10!=25&&(int)usMRegHoldBuf[0][1]/10!=30)
  56. // {
  57. rt_sprintf(device_status.tempture,"%d.%d",(int)usMRegHoldBuf[0][0]/100, (uint)usMRegHoldBuf[0][0] % 100);
  58. rt_sprintf(device_status.humidity,"%d.%d",(int)usMRegHoldBuf[0][1]/100, (uint)usMRegHoldBuf[0][1] % 100);
  59. // sprintf(device_status.tempture,"%0.01f",(float)usMRegHoldBuf[0][0]/100);
  60. // sprintf(device_status.humidity,"%0.01f",(float)usMRegHoldBuf[0][1]/100);
  61. // }
  62. }
  63. rt_kprintf("slave data-tem:%d,hum:%d\n",usMRegHoldBuf[0][0],usMRegHoldBuf[0][1]);//0代表从机ID=1
  64. rt_kprintf("slave data-tem:%s,hum:%s\n",device_status.tempture,device_status.humidity);//0代表从机ID=1
  65. rt_thread_delay(5000);
  66. /* Record the number of errors */
  67. if (error_code != 0)
  68. {
  69. error_count++;
  70. if(error_count == 10)
  71. {
  72. error_count = 0;
  73. device_status.sensor_485_sta = 0;
  74. }
  75. }
  76. }
  77. }
  78. static void mb_master_poll(void *parameter)
  79. {
  80. eMBMasterInit(MB_RTU, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
  81. eMBMasterEnable();
  82. while (1)
  83. {
  84. eMBMasterPoll();
  85. rt_thread_mdelay(MB_POLL_CYCLE_MS);
  86. }
  87. }
  88. int mb_master_samlpe(void)
  89. {
  90. static rt_uint8_t is_init = 0;
  91. rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
  92. if (is_init > 0)
  93. {
  94. rt_kprintf("sample is running\n");
  95. return -RT_ERROR;
  96. }
  97. tid1 = rt_thread_create("md_m_poll", mb_master_poll, RT_NULL, 512, MB_POLL_THREAD_PRIORITY, 10);
  98. if (tid1 != RT_NULL)
  99. {
  100. rt_thread_startup(tid1);
  101. }
  102. else
  103. {
  104. goto __exit;
  105. }
  106. tid2 = rt_thread_create("md_m_send", send_thread_entry, RT_NULL, 512, MB_SEND_THREAD_PRIORITY, 10);
  107. if (tid2 != RT_NULL)
  108. {
  109. rt_thread_startup(tid2);
  110. }
  111. else
  112. {
  113. goto __exit;
  114. }
  115. is_init = 1;
  116. return RT_EOK;
  117. __exit:
  118. if (tid1)
  119. rt_thread_delete(tid1);
  120. if (tid2)
  121. rt_thread_delete(tid2);
  122. return -RT_ERROR;
  123. }
  124. MSH_CMD_EXPORT(mb_master_samlpe, run a modbus master sample);