# `EtherCAT.Frame`
[🔗](https://github.com/tallakt/yaethercat/blob/v0.1.0/lib/ethercat/frame.ex#L1)

An EtherCAT frame: a header of two bytes, with the length of what follows and its type, then one
or more datagrams (ETG.1000.4, 5.3). It goes in an Ethernet frame of EtherType 0x88A4, or in a UDP
datagram to port 0x88A4 (34980).

    iex> frame = EtherCAT.Frame.encode([%{EtherCAT.Datagram.new({:brd, 0x0000, 1}) | index: 7}])
    <<13, 0x10, 0x07, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0>>
    iex> {:ok, [datagram]} = EtherCAT.Frame.decode(frame)
    iex> datagram.command
    :brd

Only frames of datagrams are this library's: the spec's network variables (type 4) and mailbox
frames (type 5) are decoded no further than their type.

# `type`

```elixir
@type type() :: 0..15
```

The type in a frame's header: 1 for datagrams.

# `decode`

```elixir
@spec decode(binary()) ::
  {:ok, [EtherCAT.Datagram.t()]}
  | {:error, {:type, type()} | :invalid_length | :invalid_command}
```

The datagrams of a frame. What follows its length, such as padding, is passed over.

  * `{:ok, datagrams}`
  * `{:error, {:type, type}}` for a frame of another type than datagrams
  * `{:error, :invalid_length}`, `{:error, :invalid_command}` for one that isn't a frame

# `encode`

```elixir
@spec encode([EtherCAT.Datagram.t()]) :: binary()
```

The frame of some datagrams, `next` set in all but the last. Raises `ArgumentError` when they don't
fit in one.

# `ethertype`

```elixir
@spec ethertype() :: 34980
```

The EtherType of EtherCAT frames, and the UDP port they go to.

# `from_ethernet`

```elixir
@spec from_ethernet(binary()) :: {:ok, &lt;&lt;_::48&gt;&gt;, binary()} | :error
```

The EtherCAT frame in an Ethernet frame, with the source address, or `:error` for an Ethernet frame
of another EtherType. A VLAN tag is passed over.

# `max_size`

```elixir
@spec max_size() :: pos_integer()
```

The bytes of datagrams a frame can take.

# `to_ethernet`

```elixir
@spec to_ethernet(binary(), &lt;&lt;_::48&gt;&gt;) :: binary()
```

An EtherCAT frame in an Ethernet frame from `source`, a MAC address of six bytes, to the broadcast
address, padded to the least Ethernet allows.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
