LibmodbusWrapper.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "LibmodbusWrapper.h"
  2. namespace modbus
  3. {
  4. CLibmodbusWrapper::CLibmodbusWrapper()
  5. :_ctx(NULL)
  6. , _ip("")
  7. ,_port(-1)
  8. ,_slave_id(-1)
  9. {
  10. }
  11. CLibmodbusWrapper::~CLibmodbusWrapper()
  12. {
  13. deinitialize();
  14. }
  15. void CLibmodbusWrapper::deinitialize()
  16. {
  17. if (_ctx)
  18. {
  19. modbus_close(_ctx);
  20. modbus_free(_ctx);
  21. _ctx = NULL;
  22. _ip = "";
  23. _port = -1;
  24. _slave_id = -1;
  25. }
  26. }
  27. int CLibmodbusWrapper::initialize(const char *ip, int port, int slave_id)
  28. {
  29. int rc = 0;
  30. this->deinitialize();
  31. _ctx = modbus_new_tcp(ip, port);
  32. if (_ctx == NULL)
  33. {
  34. //����ʧ��
  35. fprintf(stderr, "CLibmodbusWrapper: Unable to allocate libmodbus context\n");
  36. return -3;
  37. }
  38. // ���õ���ģʽ
  39. modbus_set_debug(_ctx, FALSE);
  40. // �������ӵ�slave�ţ��˴�Ϊ1
  41. rc = modbus_set_slave(_ctx, slave_id);
  42. if (rc == -1)
  43. {
  44. fprintf(stderr, "CLibmodbusWrapper: Invalid slave ID\n");
  45. modbus_free(_ctx);
  46. _ctx = NULL;
  47. return -2;
  48. }
  49. rc = modbus_connect(_ctx);
  50. if (rc == -1)
  51. {
  52. fprintf(stderr, "CLibmodbusWrapper: Connection failed: %s\n", modbus_strerror(errno));
  53. modbus_free(_ctx);
  54. _ctx = NULL;
  55. return -1;
  56. }
  57. _ip = ip;
  58. _port = port;
  59. _slave_id = slave_id;
  60. return 0;
  61. }
  62. int CLibmodbusWrapper::read_registers(int addr, int nb, uint16_t *dest)
  63. {
  64. if (modbus_read_registers(_ctx, addr, nb, dest) == -1)
  65. {
  66. fprintf(stderr, "CLibmodbusWrapper: Read registers failed: %s\n", modbus_strerror(errno));
  67. // printf("%s---%s, %d\n", modbus_strerror(errno), "Broken pipe", strcmp(modbus_strerror(errno), "Broken pipe")==0?1:0);
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. int CLibmodbusWrapper::write_registers(int addr, int nb, uint16_t *dest)
  73. {
  74. if (modbus_write_registers(_ctx, addr, nb, dest) == -1)
  75. {
  76. fprintf(stderr, "CLibmodbusWrapper: Write registers failed: %s\n", modbus_strerror(errno));
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. int CLibmodbusWrapper::read_register(int addr, uint16_t *dest)
  82. {
  83. if (modbus_read_registers(_ctx, addr, 1, dest) == -1)
  84. {
  85. fprintf(stderr, "CLibmodbusWrapper: Read registers failed: %s\n", modbus_strerror(errno));
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. int CLibmodbusWrapper::write_register(int addr, uint16_t *dest)
  91. {
  92. if (modbus_write_registers(_ctx, addr, 1, dest) == -1)
  93. {
  94. fprintf(stderr, "CLibmodbusWrapper: Write registers failed: %s\n", modbus_strerror(errno));
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. }