View Source Modbuzz (Modbuzz v0.2.0)
Modbuzz
is yet another MODBUS library, supporting both TCP and RTU.
Summary
Functions
Create a unit under the data server.
Delete request response pair from data server.
Dump data from data server.
Request data.
Start data server.
Start RTU client.
Start RTU server.
Start TCP client.
Start TCP server.
Upsert request response/callback pair to data server.
Types
@type client() :: GenServer.name()
@type data_server() :: GenServer.name()
@type error() :: Modbuzz.PDU.Protocol.t()
@type request() :: Modbuzz.PDU.Protocol.t()
@type response() :: Modbuzz.PDU.Protocol.t()
@type server() :: GenServer.name()
@type unit_id() :: 0..247
Functions
@spec create_unit(name :: data_server(), unit_id()) :: :ok | {:error, :already_created}
Create a unit under the data server.
@spec delete(name :: data_server(), unit_id(), request()) :: :ok | {:error, :unit_not_found}
Delete request response pair from data server.
@spec dump(name :: data_server(), unit_id()) :: {:ok, map()} | {:error, :unit_not_found}
Dump data from data server.
@spec request( name :: client() | data_server(), unit_id(), request(), non_neg_integer() ) :: {:ok, response()} | {:error, error()} | {:error, reason :: term()}
Request data.
@spec start_data_server(name :: data_server()) :: :ok
Start data server.
Examples
iex> :ok = Modbuzz.start_data_server(:data_server)
iex> alias Modbuzz.PDU.WriteSingleCoil
iex> req = %WriteSingleCoil.Req{output_address: 0 , output_value: true}
iex> res = %WriteSingleCoil.Res{output_address: 0 , output_value: true}
iex> :ok = Modbuzz.create_unit(:data_server, 1)
iex> :ok = Modbuzz.upsert(:data_server, 1, req, res)
iex> {:ok, ^res} = Modbuzz.request(:data_server, 1, req)
@spec start_rtu_client( name :: client(), device_name :: String.t(), transport_opts :: keyword() ) :: :ok | {:error, :already_started}
Start RTU client.
This function accepts transport_opts
as its third argument which allows to pass options to Circuits.UART
.
Options provided in transport_opts
are passed directly to Circuits.UART
without modification.
Examples
iex> :ok = Modbuzz.start_rtu_client(:client, "ttyUSB0", [speed: 9600])
iex> alias Modbuzz.PDU.WriteSingleCoil
iex> req = %WriteSingleCoil.Req{output_address: 0 , output_value: true}
iex> {:error, %WriteSingleCoil.Err{}} = Modbuzz.request(:client, req)
@spec start_rtu_server( name :: server(), device_name :: String.t(), transport_opts :: Circuits.UART.uart_option(), data_source :: data_server() | client() ) :: :ok | {:error, :already_started}
Start RTU server.
This function accepts transport_opts
as its third argument which allows to pass options to Circuits.UART
.
Options provided in transport_opts
are passed directly to Circuits.UART
without modification.
Examples
iex> :ok = Modbuzz.start_data_server(:data_server)
iex> alias Modbuzz.PDU.WriteSingleCoil
iex> req = %WriteSingleCoil.Req{output_address: 0 , output_value: true}
iex> res = %WriteSingleCoil.Res{output_address: 0 , output_value: true}
iex> :ok = Modbuzz.create_unit(:data_server, 1)
iex> :ok = Modbuzz.upsert(:data_server, 1, req, res)
iex> :ok = Modbuzz.start_rtu_server(:server, "ttyUSB0", [speed: 9600], :data_server)
@spec start_tcp_client( name :: client(), address :: :inet.socket_address() | :inet.hostname(), port :: :inet.port_number() ) :: :ok | {:error, :already_started}
Start TCP client.
Examples
iex> :ok = Modbuzz.start_tcp_client(:client, {127, 0, 0, 1}, 50200)
iex> alias Modbuzz.PDU.WriteSingleCoil
iex> req = %WriteSingleCoil.Req{output_address: 0 , output_value: true}
iex> {:error, %WriteSingleCoil.Err{}} = Modbuzz.request(:client, req)
@spec start_tcp_server( name :: server(), address :: :inet.socket_address() | :inet.hostname(), port :: :inet.port_number(), data_source :: data_server() | client() ) :: :ok | {:error, :already_started}
Start TCP server.
Examples
iex> :ok = Modbuzz.start_data_server(:data_server)
iex> alias Modbuzz.PDU.WriteSingleCoil
iex> req = %WriteSingleCoil.Req{output_address: 0 , output_value: true}
iex> res = %WriteSingleCoil.Res{output_address: 0 , output_value: true}
iex> :ok = Modbuzz.create_unit(:data_server, 1)
iex> :ok = Modbuzz.upsert(:data_server, 1, req, res)
iex> :ok = Modbuzz.start_tcp_server(:server, {127, 0, 0, 1}, 50200, :data_server)
@spec upsert(name :: data_server(), unit_id(), request(), response() | callback()) :: :ok | {:error, :unit_not_found}
Upsert request response/callback pair to data server.
When using a callback, the user is responsible for the callback. This library does not handle its error. In case of an error, the request will simply time out with noreply.