View Source Modbuzz.TCP.Client (Modbuzz v0.1.1)
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
@spec call( GenServer.server(), unit_id :: 0..255, request :: Modbuzz.PDU.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 the same as Modbuzz.PDU.decode/2
of request or {:error, reason :: term()}
.
Examples
iex> alias Modbuzz.PDU.{WriteSingleCoil, ReadCoils}
[Modbuzz.PDU.WriteSingleCoil, Modbuzz.PDU.ReadCoils]
iex> Modbuzz.TCP.Client.call(%WriteSingleCoil{output_address: 0 , output_value: true})
{:ok, nil}
iex> Modbuzz.TCP.Client.call(%ReadCoils{starting_address: 0 , quantity_of_coils: 1})
{:ok, [true]}
cast(name \\ __MODULE__, unit_id \\ 0, request, from_pid \\ self())
View Source@spec cast(GenServer.server(), unit_id :: 0..255, request :: Modbuzz.PDU.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 the same as Modbuzz.PDU.decode/2
of request or {:error, reason :: term()}
.
Examples
iex> alias Modbuzz.PDU.{WriteSingleCoil, ReadCoils}
[Modbuzz.PDU.WriteSingleCoil, Modbuzz.PDU.ReadCoils]
iex> Modbuzz.TCP.Client.cast(%WriteSingleCoil{output_address: 0 , output_value: true})
:ok
iex> Modbuzz.TCP.Client.cast(%ReadCoils{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
:name
- used for name registration as described in the "Name registration" section in the documentation forGenServer
:address
- passed through to:gen_tcp.connect/4
:port
- passed through to:gen_tcp.connect/4
:active
- passed through to:gen_tcp.connect/4
Examples
iex> Modbuzz.TCP.Client.start_link([address: {192, 168, 0, 123}, port: 502, active: false])