# `Modbuv.Tcp.Client`
[🔗](https://github.com/ultra0224/modbuv/blob/v0.1.0/lib/modbuv/tcp/client.ex#L1)

Modbus TCP client. In passive mode `request/2` followed by `confirmation/1`
returns the result synchronously; in active mode responses are sent to
`d_pid` as `{:modbuv_tcp, frame, result}`.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `close`

```elixir
@spec close(pid()) :: :ok
```

Closes the underlying TCP socket and resets the client state. Always returns `:ok`.

# `configure`

```elixir
@spec configure(pid(), keyword()) :: :ok | :error
```

Updates the client's connection parameters (`:ip`, `:tcp_port`, `:timeout`, `:active`, `:d_pid`) while the socket is closed. Returns `:error` if the socket is already connected.

# `confirmation`

```elixir
@spec confirmation(pid()) :: :ok | {:ok, list()} | {:error, term()} | :error
```

Waits for and parses the server response for the last `request/2` call in passive mode. Returns `:error` if used in active mode.

# `connect`

```elixir
@spec connect(pid()) :: :ok | {:error, term()}
```

Opens a TCP connection to the configured host and port. Returns `:ok` on success or `{:error, reason}` on failure.

# `flush`

```elixir
@spec flush(pid()) :: {:ok, map()}
```

Returns and clears the map of pending active-mode messages that have not yet been matched to a response.

# `request`

```elixir
@spec request(pid(), tuple()) :: :ok | {:error, term()}
```

Sends a Modbus frame to the server. In passive mode call `confirmation/1` afterward to receive the result; in active mode returns `:ok` immediately.

# `start_link`

```elixir
@spec start_link(keyword(), keyword()) :: {:ok, pid()} | {:error, term()}
```

Starts the TCP client GenServer. `params` accepts `:ip`, `:tcp_port`, `:timeout`, `:active`, and `:d_pid`; `gen_opts` are passed to `GenServer.start_link/3`.

# `state`

```elixir
@spec state(pid()) :: %Modbuv.Tcp.Client{
  active: term(),
  buffer: term(),
  cmd: term(),
  d_pid: term(),
  ip: term(),
  pending_msg: term(),
  pending_started: term(),
  req_started_at: term(),
  sent_transid: term(),
  socket: term(),
  status: term(),
  tcp_port: term(),
  timeout: term(),
  transid: term()
}
```

Returns the current internal state struct of the client.

# `stop`

```elixir
@spec stop(pid()) :: :ok
```

Stops the client GenServer process.

---

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