MODBUS_NEW_TCP_PI(3)
๐ MODBUS_NEW_TCP_PI ๐
okay bestie, welcome to the libmodbus manual, section 3, and today's main character is modbus_new_tcp_pi โ she's giving TCP Protocol Independent energy and honestly? obsessed. โจ
๐ธ NAME ๐ธ
modbus_new_tcp_pi โ create a libmodbus context for TCP Protocol Independent
๐ฆ SYNOPSIS ๐ฆ
modbus_t *modbus_new_tcp_pi(const char *node, const char *service);
๐ DESCRIPTION ๐
so the modbus_new_tcp_pi() function is gonna allocate and initialize a modbus_t structure so you can talk to a Modbus TCP IPv4 or IPv6 server. like, she sets you up. she's the wingwoman. ๐ซถ
the node argument is the host name or IP address of the host you're connecting to โ for example "192.168.0.5", "::1" or "server.com". and plot twist: you can pass a NULL value to listen on any addresses in server mode. versatile queen. ๐
the service argument is the service name / port number you're connecting to. want the default Modbus port? use the string "502". and ngl, on many Unix systems it's lowkey convenient to use a port number greater than or equal to 1024 because then you don't need administrator privileges โ no sudo moment required. ๐
๐ RETURN VALUE ๐
if it slays, the function returns a pointer to a modbus_t structure. if it does not slay, it returns NULL and sets errno to one of the values defined below. bestie. ๐ญ
โญ ERRORS โญ
EINVALโ thenodestring is empty or has been truncated. or theservicestring is empty or has been truncated. basically somebody sent an empty text message and we're all confused. ๐ENOMEMโ out of memory. possibly the application is hitting its memory limit and/or the whole system is running out of memory. she's running on fumes, fr fr. ๐ฎโ๐จ
๐ EXAMPLE ๐
modbus_t *ctx;
ctx = modbus_new_tcp_pi("::1", "1502");
if (ctx == NULL) {
fprintf(stderr, "Unable to allocate libmodbus context\n");
return -1;
}
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
look at that. clean. iconic. she ate. ๐ฅ
๐ SEE ALSO ๐
go read them, they're all part of the friend group. ๐ง
๐ AUTHORS ๐
the libmodbus documentation was written by Stรฉphane Raimbault <stephane.raimbault@gmail.com> โ thank you king, we love a documentation legend. ๐ซก
and that's the whole page, bestie! libmodbus v3.1.6, dated 11/17/2025. now go forth and create contexts responsibly. mwah! ๐โจ
๐ฆ where this page comes from
- debian bookworm: libmodbus-dev 3.1.6-2.1+deb12u1 ยท LGPL-2.1-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms