| 1 | # modbus_proxy |
| 2 | |
| 3 | ## Name |
| 4 | |
| 5 | modbus_proxy - forward a request to a backend device and relay the response |
| 6 | |
| 7 | ## Synopsis |
| 8 | |
| 9 | ```c |
| 10 | int modbus_proxy(modbus_t *frontend_ctx, modbus_t *backend_ctx, const uint8_t *req, int req_length); |
| 11 | ``` |
| 12 | |
| 13 | ## Description |
| 14 | |
| 15 | The *modbus_proxy()* function shall forward a Modbus request received on |
| 16 | `frontend_ctx` to a device connected through `backend_ctx`, and relay the |
| 17 | response back to the original requester on `frontend_ctx`. |
| 18 | |
| 19 | This function is intended for building Modbus gateways or proxies that bridge |
| 20 | two different backends. A typical use case is a TCP-to-RTU gateway: a client |
| 21 | sends a TCP request which is forwarded to an RTU device on a serial bus, and the |
| 22 | RTU response is relayed back as a TCP response. |
| 23 | |
| 24 | The request `req` of length `req_length` should be a raw request as returned by |
| 25 | [modbus_receive](modbus_receive.md). The slave address is extracted from `req` |
| 26 | and set on `backend_ctx` before forwarding. |
| 27 | |
| 28 | The function handles protocol translation between backends automatically: |
| 29 | |
| 30 | - the PDU (Protocol Data Unit) is extracted from the frontend framing |
| 31 | - it is re-framed for the backend protocol |
| 32 | - the backend response PDU is extracted and re-framed for the frontend protocol |
| 33 | - the transaction identifier from the original request is preserved |
| 34 | |
| 35 | On communication errors, an appropriate Modbus gateway exception response is |
| 36 | sent back to the frontend client: |
| 37 | |
| 38 | - `MODBUS_EXCEPTION_GATEWAY_PATH` (0x0A) if the request cannot be forwarded or |
| 39 | if a non-timeout communication error occurs |
| 40 | - `MODBUS_EXCEPTION_GATEWAY_TARGET` (0x0B) if the backend device does not |
| 41 | respond (timeout) or sends an invalid response |
| 42 | |
| 43 | ## Return value |
| 44 | |
| 45 | The function shall return the length of the response sent on `frontend_ctx` if |
| 46 | successful. Otherwise it shall return -1 and set errno. |
| 47 | |
| 48 | ## Errors |
| 49 | |
| 50 | - *EINVAL*, `frontend_ctx` or `backend_ctx` is NULL, or `req` is NULL, or |
| 51 | `req_length` is less than 1. |
| 52 | - *EMBBADDATA*, the PDU extracted from the request is invalid (too short or too |
| 53 | long). |
| 54 | |
| 55 | ## Example |
| 56 | |
| 57 | ```c |
| 58 | modbus_t *tcp_ctx; |
| 59 | modbus_t *rtu_ctx; |
| 60 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 61 | int rc; |
| 62 | |
| 63 | tcp_ctx = modbus_new_tcp("0.0.0.0", 1502); |
| 64 | rtu_ctx = modbus_new_rtu("/dev/ttyUSB0", 38400, 'E', 8, 1); |
| 65 | |
| 66 | int s = modbus_tcp_listen(tcp_ctx, 1); |
| 67 | modbus_connect(rtu_ctx); |
| 68 | |
| 69 | for (;;) { |
| 70 | modbus_tcp_accept(tcp_ctx, &s); |
| 71 | |
| 72 | for (;;) { |
| 73 | rc = modbus_receive(tcp_ctx, query); |
| 74 | if (rc < 0) |
| 75 | break; |
| 76 | if (rc == 0) |
| 77 | continue; |
| 78 | |
| 79 | /* Forward the TCP request to the RTU device and relay the response */ |
| 80 | modbus_proxy(tcp_ctx, rtu_ctx, query, rc); |
| 81 | } |
| 82 | |
| 83 | modbus_close(tcp_ctx); |
| 84 | } |
| 85 | |
| 86 | modbus_close(rtu_ctx); |
| 87 | modbus_free(tcp_ctx); |
| 88 | modbus_free(rtu_ctx); |
| 89 | ``` |
| 90 | |
| 91 | ## See also |
| 92 | |
| 93 | - [modbus_receive](modbus_receive.md) |
| 94 | - [modbus_reply](modbus_reply.md) |
| 95 | - [modbus_reply_exception](modbus_reply_exception.md) |