当前位置:   article > 正文

modbus tcp 主站-基于libmodbus开源库

modbustcp主站

一、知识点介绍

    在Modbus TCP协议中,主站被称为客户端(Client),它是发起通讯的一方。主站的功能类似于RTU主站,会向从站(服务器)发起数据请求。主站通过TCP/IP协议与从站进行通讯,发送查询操作数据的请求,并接收从站返回的响应数据。主站可以同时与多个从站进行通讯,每个从站都有一个唯一的地址标识。在Modbus TCP协议中,主站可以通过IP地址和端口号来识别从站

84021fdffa932b240861138c9130fe56.png

二、modbus tcp 主站

基于libmodbus开源库,编写modbus tcp的主站,读取bits和registers,测试代码如下:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <modbus.h>
  7. #define LOOP 1
  8. #define SERVER_ID 17
  9. #define ADDRESS_START 0
  10. #define ADDRESS_END 9
  11. int main(void)
  12. {
  13. modbus_t *ctx;
  14. int rc;
  15. int nb_fail;
  16. int nb_loop;
  17. int addr;
  18. int nb;
  19. uint8_t *tab_bits;
  20. uint8_t *tab_in_bits;
  21. uint16_t *tab_registers;
  22. uint16_t *tab_in_registers;
  23. ctx = modbus_new_tcp("192.168.1.188", 1502);
  24. modbus_set_slave(ctx, SERVER_ID);
  25. modbus_set_debug(ctx, TRUE);
  26. if (modbus_connect(ctx) == -1)
  27. {
  28. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  29. modbus_free(ctx);
  30. return -1;
  31. }
  32. /* Allocate and initialize the different memory spaces */
  33. nb = ADDRESS_END - ADDRESS_START;
  34. tab_bits = (uint8_t *) malloc(nb * sizeof(uint8_t));
  35. memset(tab_bits, 0, nb * sizeof(uint8_t));
  36. tab_in_bits = (uint8_t *) malloc(nb * sizeof(uint8_t));
  37. memset(tab_in_bits, 0, nb * sizeof(uint8_t));
  38. tab_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  39. memset(tab_registers, 0, nb * sizeof(uint16_t));
  40. tab_in_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  41. memset(tab_in_registers, 0, nb * sizeof(uint16_t));
  42. nb_loop = nb_fail = 0;
  43. while (nb_loop++ < LOOP)
  44. {
  45. for (addr = ADDRESS_START; addr < ADDRESS_END; addr++)
  46. {
  47. int i;
  48. nb = ADDRESS_END - addr;
  49. /* read BIT */
  50. rc = modbus_read_bits(ctx, addr, 1, tab_bits);
  51. if (rc != 1 )
  52. {
  53. printf("ERROR modbus_read_bits single (%d)\n", rc);
  54. printf("address = %d\n", addr);
  55. nb_fail++;
  56. }
  57. else
  58. {
  59. printf("Address = %d, value %d (0x%X) \n",
  60. addr,
  61. tab_bits[0],
  62. tab_bits[0]);
  63. }
  64. /* MULTIPLE BITS */
  65. rc = modbus_read_bits(ctx, addr, nb, tab_bits);
  66. if (rc != nb)
  67. {
  68. printf("ERROR modbus_read_bits\n");
  69. printf("Address = %d, nb = %d\n", addr, nb);
  70. nb_fail++;
  71. }
  72. else
  73. {
  74. for (i = 0; i < nb; i++)
  75. {
  76. printf("Address = %d, value %d (0x%X) \n",
  77. addr+i,
  78. tab_bits[i],
  79. tab_bits[i]);
  80. }
  81. }
  82. //read input bits
  83. //modbus_read_input_bits
  84. /* SINGLE REGISTER */
  85. rc = modbus_read_registers(ctx, addr, 1, tab_registers);
  86. if (rc != 1)
  87. {
  88. printf("ERROR modbus_read_registers single (%d)\n", rc);
  89. printf("Address = %d\n", addr);
  90. nb_fail++;
  91. }
  92. else
  93. {
  94. printf("Address = %d, value = %d (0x%X) \n",
  95. addr,
  96. tab_registers[0],
  97. tab_registers[0]);
  98. }
  99. /* MULTIPLE REGISTERS */
  100. rc = modbus_read_registers(ctx, addr, nb, tab_registers);
  101. if (rc != nb)
  102. {
  103. printf("ERROR modbus_read_registers (%d)\n", rc);
  104. printf("Address = %d, nb = %d\n", addr, nb);
  105. nb_fail++;
  106. }
  107. else
  108. {
  109. for ( i = 0; i < nb; i++)
  110.                 {
  111. printf("Address = %d, value %d (0x%X) \n",
  112. addr+i,
  113. tab_registers[i],
  114. tab_registers[i]);
  115. }
  116. }
  117. // read intput registers
  118. //modbus_read_input_registers
  119. sleep(5);
  120. }
  121. printf("Test: ");
  122. if (nb_fail)
  123. printf("%d FAILS\n", nb_fail);
  124. else
  125. printf("SUCCESS\n");
  126. }
  127. /* Free the memory */
  128. free(tab_bits);
  129. free(tab_in_bits);
  130. free(tab_registers);
  131. free(tab_in_registers);
  132. /* Close the connection */
  133. modbus_close(ctx);
  134. modbus_free(ctx);
  135. return 0;
  136. }

三、测试结果

打开modbus slave的测试软件,从站地址为0x11,配置如下:

e690a1f5268aa249056cf83b69f78046.png

modbus tcp主站,读取信息如下:

be9976048f51ecaf2094fdc6ba0946a5.png

主站正确读取到了从站的信息。

欢迎关注公众号:嵌入式学习与实践

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/971428
推荐阅读
  

闽ICP备14008679号