modbus_new_tcp.txt 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. modbus_new_tcp(3)
  2. =================
  3. NAME
  4. ----
  5. modbus_new_tcp - create a libmodbus context for TCP/IPv4
  6. SYNOPSIS
  7. --------
  8. *modbus_t *modbus_new_tcp(const char *'ip', int 'port');*
  9. DESCRIPTION
  10. -----------
  11. The *modbus_new_tcp()* function shall allocate and initialize a modbus_t
  12. structure to communicate with a Modbus TCP IPv4 server.
  13. The _ip_ argument specifies the IP address of the server to which the client
  14. wants to establish a connection. A NULL value can be used to listen any addresses in
  15. server mode.
  16. The _port_ argument is the TCP port to use. Set the port to
  17. `MODBUS_TCP_DEFAULT_PORT` to use the default one (502). It’s convenient to use a
  18. port number greater than or equal to 1024 because it’s not necessary to have
  19. administrator privileges.
  20. RETURN VALUE
  21. ------------
  22. The function shall return a pointer to a *modbus_t* structure if
  23. successful. Otherwise it shall return NULL and set errno to one of the values
  24. defined below.
  25. ERRORS
  26. ------
  27. *EINVAL*::
  28. An invalid IP address was given.
  29. *ENOMEM*::
  30. Out of memory. Possibly, the application hits its memory limit and/or whole
  31. system is running out of memory.
  32. EXAMPLE
  33. -------
  34. [source,c]
  35. -------------------
  36. modbus_t *ctx;
  37. ctx = modbus_new_tcp("127.0.0.1", 1502);
  38. if (ctx == NULL) {
  39. fprintf(stderr, "Unable to allocate libmodbus context\n");
  40. return -1;
  41. }
  42. if (modbus_connect(ctx) == -1) {
  43. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  44. modbus_free(ctx);
  45. return -1;
  46. }
  47. -------------------
  48. SEE ALSO
  49. --------
  50. linkmb:modbus_tcp_listen[3]
  51. linkmb:modbus_free[3]
  52. AUTHORS
  53. -------
  54. The libmodbus documentation was written by Stéphane Raimbault
  55. <stephane.raimbault@gmail.com>