modbus_new_tcp.txt 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.
  15. The _port_ argument is the TCP port to use. Set the port to
  16. _MODBUS_TCP_DEFAULT_PORT_ to use the default one (502). It’s convenient to use a
  17. port number greater than or equal to 1024 because it’s not necessary to have
  18. administrator privileges.
  19. RETURN VALUE
  20. ------------
  21. The _modbus_new_tcp()_ function shall return a pointer to a *modbus_t* structure
  22. if successful. Otherwise it shall return NULL and set errno to one of the values
  23. defined below.
  24. ERRORS
  25. ------
  26. *EINVAL*::
  27. An invalid IP address was given.
  28. EXAMPLE
  29. -------
  30. [source,c]
  31. -------------------
  32. modbus_t *ctx;
  33. ctx = modbus_new_tcp("127.0.0.1", 1502);
  34. if (ctx == NULL) {
  35. fprintf(stderr, "Unable to allocate libmodbus context\n");
  36. return -1;
  37. }
  38. if (modbus_connect(ctx) == -1) {
  39. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  40. modbus_free(ctx);
  41. return -1;
  42. }
  43. -------------------
  44. SEE ALSO
  45. --------
  46. linkmb:modbus_new_rtu[3]
  47. linkmb:modbus_free[3]
  48. AUTHORS
  49. -------
  50. The libmodbus documentation was written by Stéphane Raimbault
  51. <stephane.raimbault@gmail.com>