modbus_new_tcp.txt 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 etablish 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 *modbus_new_tcp()* function shall return a pointer to a *modbus_t* structure
  23. if 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. EXAMPLE
  30. -------
  31. [source,c]
  32. -------------------
  33. modbus_t *ctx;
  34. ctx = modbus_new_tcp("127.0.0.1", 1502);
  35. if (ctx == NULL) {
  36. fprintf(stderr, "Unable to allocate libmodbus context\n");
  37. return -1;
  38. }
  39. if (modbus_connect(ctx) == -1) {
  40. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  41. modbus_free(ctx);
  42. return -1;
  43. }
  44. -------------------
  45. SEE ALSO
  46. --------
  47. linkmb:modbus_tcp_listen[3]
  48. linkmb:modbus_free[3]
  49. AUTHORS
  50. -------
  51. The libmodbus documentation was written by Stéphane Raimbault
  52. <stephane.raimbault@gmail.com>