EtherCAT.Bus.Transport.UdpSocket (ethercat v0.5.0)

Copy Markdown View Source

UDP/IP transport for simulator and integration endpoints.

Sends the EtherCAT payload (Bus.Frame.encode/1 output) inside a UDP datagram, using the conventional EtherCAT UDP port 0x88A4 (34980) by default. This is not the raw Ethernet transport used for ordinary physical EtherCAT rings; the destination must explicitly support this encapsulation.

Implements the EtherCAT.Bus.Transport behaviour. Application startup should select it through EtherCAT.Backend, not by opening a transport directly.

Options

  • :host — destination IP tuple (default: {255, 255, 255, 255} broadcast)
  • :port — local bind and destination UDP port (default: 34980 = 0x88A4)
  • :bind_ip — optional local IP tuple; otherwise binds all local addresses

A same-host simulator must bind a different local address when sharing the port. See EtherCAT.Simulator for a Linux loopback example.

Summary

Functions

Close the UDP socket. Returns the struct with raw set to nil.

Drain buffered datagrams and disable active delivery.

Returns nil — UDP sockets don't use OS interface link monitoring.

Match a {:udp, raw, _ip, _port, data} message from this socket.

Returns a string identifier for telemetry: "host:port".

Open a UDP socket bound to the configured port and optional local IP.

Returns true when the socket is open.

Re-arm — delegates to set_active_once/1 (UDP has no drain issue).

Send an EtherCAT payload as a UDP datagram.

Arm for one async delivery via {:active, :once}.

Returns nil — UDP has no Ethernet-level source MAC.

Types

t()

@type t() :: %EtherCAT.Bus.Transport.UdpSocket{
  host: :inet.ip_address(),
  port: :inet.port_number(),
  raw: :gen_udp.socket() | nil
}

Functions

close(sock)

@spec close(t()) :: t()

Close the UDP socket. Returns the struct with raw set to nil.

drain(udp_socket)

@spec drain(t()) :: :ok

Drain buffered datagrams and disable active delivery.

interface(udp_socket)

@spec interface(t()) :: nil

Returns nil — UDP sockets don't use OS interface link monitoring.

match(udp_socket, arg2)

@spec match(t(), term()) :: {:ok, binary(), integer(), nil} | :ignore

Match a {:udp, raw, _ip, _port, data} message from this socket.

Returns {:ok, ecat_payload, rx_at, nil} when the message belongs to this socket. src_mac is always nil for UDP (no Ethernet headers). This function matches socket ownership only; EtherCAT payload validation is performed by the link/frame layer.

name(udp_socket)

@spec name(t()) :: String.t()

Returns a string identifier for telemetry: "host:port".

open(opts)

@spec open(keyword()) :: {:ok, t()} | {:error, term()}

Open a UDP socket bound to the configured port and optional local IP.

open?(udp_socket)

@spec open?(t()) :: boolean()

Returns true when the socket is open.

rearm(sock)

@spec rearm(t()) :: :ok

Re-arm — delegates to set_active_once/1 (UDP has no drain issue).

send(udp_socket, ecat_payload)

@spec send(t(), binary()) :: {:ok, integer()} | {:error, term()}

Send an EtherCAT payload as a UDP datagram.

set_active_once(udp_socket)

@spec set_active_once(t()) :: :ok

Arm for one async delivery via {:active, :once}.

src_mac(udp_socket)

@spec src_mac(t()) :: nil

Returns nil — UDP has no Ethernet-level source MAC.