Modbuv.Tcp.Client (modbuv v0.1.0)

Copy Markdown View Source

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}.

Summary

Functions

Returns a specification to start this module under a supervisor.

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

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.

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

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

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

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

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

Returns the current internal state struct of the client.

Stops the client GenServer process.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

close(pid)

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

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

configure(pid, params)

@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(pid)

@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(pid)

@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(pid)

@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(pid, frame)

@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(params, gen_opts \\ [])

@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(pid)

@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(pid)

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

Stops the client GenServer process.