# `AVModbus.TCP`
[🔗](https://github.com/piyopiyoex/avmodbus/blob/v0.1.0/lib/avmodbus/tcp.ex#L1)

Modbus TCP の MBAP framing と stream extraction を提供します。

MBAP header は transaction id、protocol id、後続 byte 数、unit id を保持します。
Modbus の protocol id は常に `0` です。TCP stream に複数 frame が連結されていても
`decode/1` は先頭の 1 frame だけを返し、残りを保持します。

# `decode`

```elixir
@spec decode(binary()) ::
  {:ok, 0..65535, 0..255, binary(), binary()}
  | {:discard, binary()}
  | :more
  | {:error, :invalid_length | :invalid_frame}
```

TCP stream の先頭から 1 frame を decode します。

- `{:ok, transaction_id, unit_id, pdu, rest}`: 完全な Modbus frame
- `:more`: header または frame が未完了
- `{:discard, rest}`: protocol id が 0 ではない完全な frame
- `{:error, :invalid_length}`: PDU が 1..253 bytes にならない MBAP length

invalid length の後は stream boundary を確定できないため、caller は connection を
close してください。

# `encode`

```elixir
@spec encode(0..65535, 0..255, binary()) :: {:ok, binary()} | {:error, term()}
```

transaction id、unit id、PDU を Modbus TCP ADU に encode します。

---

*Consult [api-reference.md](api-reference.md) for complete listing*
