EtherCAT.Capture (ethercat v0.5.0)

Copy Markdown View Source

Helpers for interactive slave-capture sessions.

Use this module from iex -S mix ethercat.capture --interface eth0 to:

  • inspect the discovered ring while it is held in PREOP
  • capture static slave identity and SII layout from a live device
  • optionally snapshot specific SDO entries
  • write a portable data-only capture file
  • generate a self-contained best-effort driver scaffold for integration tests
  • generate a simulator companion scaffold from that capture

The generated scaffold preserves static structure only. It does not infer dynamic behavior, timing, or a complete object dictionary.

Examples

{:ok, slaves} = EtherCAT.Capture.list_slaves(session)

{:ok, capture} =
  EtherCAT.Capture.capture(session, :slave_1,
    sdos: [{0x1008, 0x00}, {0x1009, 0x00}]
  )

{:ok, capture_path} =
  EtherCAT.Capture.write_capture(session, :slave_1, sdos: [{0x1008, 0x00}])

{:ok, %{driver_path: driver_path}} =
  EtherCAT.Capture.gen_driver(session, :slave_1, module: MyApp.EL1809)

{:ok, %{module_path: module_path}} =
  EtherCAT.Capture.gen_simulator(
    session,
    :slave_1,
    module: MyApp.EL1809.Simulator,
    sdos: [{0x1008, 0x00}]
  )

Summary

Functions

Capture a live slave into a structural snapshot.

Convert a capture map into simulator definition options.

Generate a self-contained best-effort driver scaffold for slave_name or a previously loaded capture map.

Generate a simulator companion scaffold for slave_name.

Print a short interactive command summary.

Return a compact snapshot of the currently discovered slaves.

Load a data-only capture file written by write_capture/3.

Load a data-only capture file and raise on failure.

Render the best-effort driver scaffold for slave_name or a previously loaded capture map.

Render a capture-backed simulator scaffold for slave_name or a previously loaded capture map.

Write a data-only capture file for slave_name.

Types

capture()

@type capture() :: map()

sdo_ref()

@type sdo_ref() :: {non_neg_integer(), non_neg_integer()}

Functions

capture(session, slave_name, opts \\ [])

@spec capture(EtherCAT.Session.t(), atom(), keyword()) ::
  {:ok, capture()} | {:error, term()}

Capture a live slave into a structural snapshot.

Supported options:

  • :sdos — list of {index, subindex} tuples to upload and include

definition_options(capture)

@spec definition_options(capture()) :: keyword()

Convert a capture map into simulator definition options.

The generated options normalize PDO data onto the simulator's simpler process-data model: one output window and one input window with canonical physical offsets.

gen_driver(capture, opts)

gen_driver(session, slave_name, opts)

@spec gen_driver(EtherCAT.Session.t(), atom(), keyword()) ::
  {:ok, %{driver_path: String.t()}} | {:error, term()}

Generate a self-contained best-effort driver scaffold for slave_name or a previously loaded capture map.

Supported options:

  • :module (required) — driver module name
  • :driver_path — output path for the generated driver file
  • :force — overwrite existing files when true
  • :sdos — list of {index, subindex} tuples to upload and include

Use write_capture/3 separately if you also want to persist the captured snapshot file alongside the generated driver.

gen_simulator(session, slave_name, opts)

@spec gen_simulator(EtherCAT.Session.t(), atom(), keyword()) ::
  {:ok, %{capture_path: String.t(), module_path: String.t()}} | {:error, term()}

Generate a simulator companion scaffold for slave_name.

Supported options:

  • :module (required) — simulator companion module name
  • :module_path — output path for the generated module
  • :capture_path — output path for the generated capture file
  • :force — overwrite existing files when true
  • :sdos — list of {index, subindex} tuples to upload and include

help()

@spec help() :: :ok

Print a short interactive command summary.

list_slaves(session)

@spec list_slaves(EtherCAT.Session.t()) :: {:ok, [map()]} | {:error, term()}

Return a compact snapshot of the currently discovered slaves.

load_capture(path)

@spec load_capture(Path.t()) :: {:ok, capture()} | {:error, term()}

Load a data-only capture file written by write_capture/3.

load_capture!(path)

@spec load_capture!(Path.t()) :: capture()

Load a data-only capture file and raise on failure.

render_driver(capture, opts)

render_driver(session, slave_name, opts)

@spec render_driver(EtherCAT.Session.t(), atom(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Render the best-effort driver scaffold for slave_name or a previously loaded capture map.

This returns the final module source directly instead of writing files.

Supported options:

  • :module (required) — driver module name
  • :simulator_module — simulator companion module name; defaults to Module.concat(module, Simulator)
  • :signal_names — map or keyword list of {{direction, pdo_index}, name} overrides used for the rendered signal names
  • :sdos — list of {index, subindex} tuples to upload and include when slave_name is a live slave

render_simulator(capture, opts)

render_simulator(session, slave_name, opts)

@spec render_simulator(EtherCAT.Session.t(), atom(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Render a capture-backed simulator scaffold for slave_name or a previously loaded capture map.

This returns the final module source directly instead of writing files.

Supported options:

  • :module (required) — simulator companion module name
  • :capture_path — output path for the generated capture file; used to compute the relative load path embedded in the module
  • :module_path — output path of the generated module; used to compute the relative load path embedded in the module
  • :sdos — list of {index, subindex} tuples to upload and include when slave_name is a live slave

write_capture(session, slave_name, opts \\ [])

@spec write_capture(EtherCAT.Session.t(), atom(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Write a data-only capture file for slave_name.

Supported options:

  • :path — output path; defaults to priv/ethercat/captures/...
  • :force — overwrite an existing file when true
  • :sdos — list of {index, subindex} tuples to upload and include