View Source Modbuzz.TCP.Client (Modbuzz v0.1.2)

This is MODBUS TCP client GenServer module.

Summary

Functions

Makes a synchronous call to the server and waits for a response. Only available when active: false.

Casts a request to the server without waiting for a response. Only available when active: true.

Returns a specification to start this module under a supervisor.

Starts a Elixir.Modbuzz.TCP.Client GenServer process linked to the current process.

Functions

Link to this function

call(name \\ __MODULE__, unit_id \\ 0, request, timeout \\ 5000)

View Source
@spec call(
  GenServer.server(),
  unit_id :: 0..255,
  request :: Modbuzz.PDU.Protocol.t(),
  timeout()
) :: {:ok, response :: term()} | {:error, reason :: term()}

Makes a synchronous call to the server and waits for a response. Only available when active: false.

The response type is {:ok, %Res{}} or {:error, %Err{} | reason :: term()}.

Examples

iex> alias Modbuzz.PDU.{WriteSingleCoil, ReadCoils}
[Modbuzz.PDU.WriteSingleCoil, Modbuzz.PDU.ReadCoils]
iex> Modbuzz.TCP.Client.call(%WriteSingleCoil.Req{output_address: 0 , output_value: true})
{:ok, %WriteSingleCoil.Res{output_address: 0 , output_value: true}}
iex> Modbuzz.TCP.Client.call(%ReadCoils.Req{starting_address: 0 , quantity_of_coils: 1})
{:ok, %ReadCoils.Res{byte_count: 1, [true, false, false, false, false, false, false, false]}}
Link to this function

cast(name \\ __MODULE__, unit_id \\ 0, request, from_pid \\ self())

View Source
@spec cast(
  GenServer.server(),
  unit_id :: 0..255,
  request :: Modbuzz.PDU.Protocol.t(),
  pid()
) :: :ok

Casts a request to the server without waiting for a response. Only available when active: true.

This function always returns :ok regardless of whether the destination server (or node) exists. Therefore it is unknown whether the destination server successfully handled the request.

Its response is sent as a meessage, looks like

{:modbuzz, unit_id, request, response}

The response type is {:ok, %Res{}} or {:error, %Err{} | reason :: term()}.

Examples

iex> alias Modbuzz.PDU.{WriteSingleCoil.Req, ReadCoils.Req}
[Modbuzz.PDU.WriteSingleCoil.Req, Modbuzz.PDU.ReadCoils.Req]
iex> Modbuzz.TCP.Client.cast(%WriteSingleCoil.Req{output_address: 0 , output_value: true})
:ok
iex> Modbuzz.TCP.Client.cast(%ReadCoils.Req{starting_address: 0 , quantity_of_coils: 1})
:ok

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec start_link(keyword()) :: GenServer.on_start()

Starts a Elixir.Modbuzz.TCP.Client GenServer process linked to the current process.

Options

Examples

iex> Modbuzz.TCP.Client.start_link([address: {192, 168, 0, 123}, port: 502, active: false])