| 1 | # modbus_connect |
| 2 | |
| 3 | ## Name |
| 4 | |
| 5 | modbus_connect - establish a Modbus connection |
| 6 | |
| 7 | ## Synopsis |
| 8 | |
| 9 | ```c |
| 10 | int modbus_connect(modbus_t *ctx); |
| 11 | ``` |
| 12 | |
| 13 | ## Description |
| 14 | |
| 15 | The *modbus_connect()* function shall establish a connection to a Modbus server, |
| 16 | a network or a bus using the context information of libmodbus context given in |
| 17 | argument. |
| 18 | |
| 19 | ## Return value |
| 20 | |
| 21 | The function shall return 0 if successful. Otherwise it shall return -1 and set |
| 22 | errno to one of the values defined by the system calls of the underlying |
| 23 | platform. |
| 24 | |
| 25 | ## Example |
| 26 | |
| 27 | ```c |
| 28 | modbus_t *ctx; |
| 29 | |
| 30 | ctx = modbus_new_tcp("127.0.0.1", 502); |
| 31 | if (modbus_connect(ctx) == -1) { |
| 32 | fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); |
| 33 | modbus_free(ctx); |
| 34 | return -1; |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | ## See also |
| 39 | |
| 40 | - [modbus_close](modbus_close.md) |