Skip to content

Commit f7cb80c

Browse files
committed
serial-tcp
1 parent e5bef21 commit f7cb80c

5 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎docs/modbus-connection-plan.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ ss = Sunsynk(unit=SolarmanUnit(host, port, dongle_serial_number, server_id=1))
127127
`from_url` parses the existing schemes:
128128

129129
- `tcp://host:502` → `ModbusTcpParams` (native Modbus TCP)
130-
- `serial-tcp://host:port` → `ModbusTcpParams(..., framer="rtu")`
130+
- `serial-tcp://host:port` → `ModbusSerialParams(device="socket://host:port", framer="rtu")`
131131
- `udp://host:port` → `ModbusUdpParams`
132132
- `/dev/ttyUSB0` (or any path with no scheme) → `ModbusSerialParams`
133133

‎docs/timeouts.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ two `MODBUS_ID`s share one `tcp://` PORT).
169169

170170
### `serial-tcp://` (RTU-over-TCP)
171171

172-
Same as TCP for the socket side, but the framer is **RTU**. The gateway translates RTU frames on the
173-
RS485 side. Spacing and turnaround constraints from [Serial](#serial) apply
174-
**on the bus behind the gateway**; the default **0.05s** spacing helps gateways that forward
175-
requests immediately without pacing.
172+
Built as **`ModbusSerialParams(device="socket://host:port")`** (RTU over a TCP socket). The gateway
173+
translates RTU frames on the RS485 side. Spacing, **`connect_delay`**, and **disconnect-on-timeout**
174+
from [Serial](#serial) apply — the bus behind the gateway is still half-duplex RTU.
176175

177176
If the gateway buffers poorly under rapid polling, prefer **`tcp://`** (native Modbus TCP) when the
178177
device supports it, or reduce batch size / read frequency.

‎hass-addon-sunsynk-edge/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ All items below are changes since add-on **1.3.0**.
99
- Failed Modbus writes no longer update the local register cache. #692
1010
- Fix AsyncCallback checks. #693
1111
- Bump whenever to 0.11
12+
- `serial-tcp://` uses `ModbusSerialParams(socket://…)` (modbus-connection 4.12+); timeouts
13+
disconnect like other serial links.
1214

1315
## Release 1.3.0
1416

‎src/sunsynk/connection.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def url_to_params(
3131
case "tcp":
3232
return ModbusTcpParams(host=host, port=tcp_port)
3333
case "serial-tcp":
34-
return ModbusTcpParams(host=host, port=tcp_port, framer="rtu")
34+
# RTU-over-TCP is a serial link; ModbusTcpParams(framer=) is deprecated (4.12+)
35+
sock = f"[{host}]:{tcp_port}" if ":" in host else f"{host}:{tcp_port}"
36+
return ModbusSerialParams(device=f"socket://{sock}", framer="rtu")
3537
case "udp":
3638
return ModbusUdpParams(host=host, port=tcp_port)
3739
case "serial-udp":

‎src/tests/sunsynk/test_connection.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ def test_url_to_params() -> None:
2323
assert url_to_params("tcp://1.2.3.4:502") == ModbusTcpParams(
2424
host="1.2.3.4", port=502
2525
)
26-
assert url_to_params("serial-tcp://host:8899") == ModbusTcpParams(
27-
host="host", port=8899, framer="rtu"
26+
assert url_to_params("serial-tcp://host:8899") == ModbusSerialParams(
27+
device="socket://host:8899", framer="rtu"
28+
)
29+
assert url_to_params("serial-tcp://[::1]:8899") == ModbusSerialParams(
30+
device="socket://[::1]:8899", framer="rtu"
2831
)
2932
assert url_to_params("udp://host:502") == ModbusUdpParams(host="host", port=502)
3033
assert url_to_params("/dev/ttyUSB0", baudrate=19200) == ModbusSerialParams(
@@ -62,7 +65,11 @@ def test_open_connection_serial_spacing() -> None:
6265
serial = open_connection("/dev/ttyUSB0", timeout=5)
6366
assert serial._pacer._message_spacing == DEFAULT_MESSAGE_SPACING
6467
rtu_tcp = open_connection("serial-tcp://host:8899", timeout=5)
68+
assert rtu_tcp._params == ModbusSerialParams(
69+
device="socket://host:8899", framer="rtu"
70+
)
6571
assert rtu_tcp._pacer._message_spacing == DEFAULT_MESSAGE_SPACING
72+
assert rtu_tcp._connect_delay == 0.05
6673
no_gap = open_connection("/dev/ttyUSB0", timeout=5, message_spacing=0)
6774
assert no_gap._pacer._message_spacing == 0.0
6875
custom = open_connection("tcp://127.0.0.1:502", timeout=5, message_spacing=0.1)

0 commit comments

Comments
 (0)