Wanted: Tutorial on Pattern Matching to parse a binary protocol

Hi Folks,

Does anyone have a good pointer to a guide or tutorial on pattern matching to parse a binary protocol? I’m trying to monitor a serial protocol using Circuits.UART.

Here is an example of the protocol I’m trying to parse: Infinity Framing Protocol · nebulous/infinitude Wiki · GitHub

Thanks!

1 Like

Shows a nice way by using Mixin module. Highly recommend this talk

3 Likes

Thanks! That talk is very useful.

1 Like

In this case parsing these messages is ultra easy:

def parse(<<destination_address,
            destination_bus,
            source_address,
            source_bus,
            length,
            pid,
            ext,
            function,
            data::binary-size(length),
            checksum::32>>
) do
  if valid_checksum?(<<destination_address,
            destination_bus,
            source_address,
            source_bus,
            length,
            pid,
            ext,
            function,
            data::binary>>) do
    {:ok, %{
      destination_address: destination_address,
      destination_bus: destination_bus,
      source_address: source_address,
      source_bus: source_bus,
      pid: pid,
      ext: ext,
      function: function,
      data: data
    }}
  else
    {:error, :invalid_checksum}
  end
end

Unfortunately, the spec didn’t specified which CRC16 they meant so I cannot provide that one.

4 Likes

Nice. Thank you!

1 Like

Have you seen this blog post?

It’s my goto binary pattern matching blog post.

5 Likes

That’s great. Thanks for sharing that post.

1 Like