Modbux.Rtu.Master (Modbux v0.3.14)

View Source

API for a Modbus RTU Master device.

Summary

Functions

Returns a specification to start this module under a supervisor.

Close the Master serial port.

Configure the Master serial port.

Callback implementation for GenServer.init/1.

Open the Master serial port.

Read and parse the last request (if the last request timeouts).

Send a request to Modbus RTU Slave.

Starts a Modbus RTU Master process.

Gets the Master state.

Functions

async_uart_read(state, cmd)

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

close(pid)

Close the Master serial port.

configure(pid, params)

Configure the Master serial port.

The following options are available:

  • tty - defines the serial port to spawn the Master.
  • timeout - defines slave timeout.
  • active - (true or false) specifies whether data is received as messages (mailbox) or by calling request/2.
  • gen_opts - defines extra options for the Genserver OTP configuration.
  • uart_opts - defines extra options for the UART configuration.

init(arg)

Callback implementation for GenServer.init/1.

open(pid)

Open the Master serial port.

read(pid)

@spec read(atom() | pid() | {atom(), any()} | {:via, atom(), any()}) :: any()

Read and parse the last request (if the last request timeouts).

request(pid, cmd)

@spec request(atom() | pid() | {atom(), any()} | {:via, atom(), any()}, tuple()) ::
  :ok | {:ok, list()} | {:error, String.t()}

Send a request to Modbus RTU Slave.

cmd is one of:

  • {:rc, slave, address, count} read count coils.
  • {:ri, slave, address, count} read count inputs.
  • {:rhr, slave, address, count} read count holding registers.
  • {:rir, slave, address, count} read count input registers.
  • {:fc, slave, address, value} force single coil.
  • {:phr, slave, address, value} preset single holding register.
  • {:fc, slave, address, values} force multiple coils.
  • {:phr, slave, address, values} preset multiple holding registers.

start_link(params)

@spec start_link(keyword()) :: :ignore | {:error, any()} | {:ok, pid()}

Starts a Modbus RTU Master process.

The following options are available:

  • tty - defines the serial port to spawn the Master.
  • timeout - defines slave timeout.
  • active - (true or false) specifies whether data is received as messages (mailbox) or by calling request/2.
  • gen_opts - defines extra options for the Genserver OTP configuration.
  • uart_opts - defines extra options for the UART configuration (defaults:
    [speed: 115200, rx_framing_timeout: 1000]).

The messages (when active mode is true) have the following form:

{:modbus_rtu, {:slave_response, cmd, values}} or

{:modbus_rtu, {:slave_error, payload, reason}}

The following are some reasons:

  • :ecrc - corrupted message (invalid crc).
  • :einval - invalid function.
  • :eaddr - invalid memory address requested.

Example

Modbux.Rtu.Master.start_link(tty: "tnt0", active: true, uart_opts: [speed: 9600])

state(pid)

Gets the Master state.

stop(pid)

@spec stop(atom() | pid() | {atom(), any()} | {:via, atom(), any()}) :: :ok