From 28ad695a109200275c3689a8ac906cbc0b051635 Mon Sep 17 00:00:00 2001 From: Samuel Ventura Date: Sat, 25 Mar 2017 14:31:25 -0600 Subject: [PATCH] added float helper doc --- lib/float.ex | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/master.ex | 9 ++++++++- lib/shared.ex | 1 + 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/float.ex b/lib/float.ex index a0455de..06266a4 100644 --- a/lib/float.ex +++ b/lib/float.ex @@ -1,25 +1,76 @@ defmodule Modbus.IEEE754 do + @moduledoc """ + IEEE754 float helper + Based on https://www.h-schmidt.net/FloatConverter/IEEE754.html. + """ + + @doc """ + Converts a couple of 16-bit registers to IEEE754 float. + + ## Example + + ```elixir + +5.0 = IEEE754.from_2_regs(0x40a0, 0x0000) + ``` + """ def from_2_regs(w0, w1) do <> = <> value end + @doc """ + Converts a couple of 16-bit registers to IEEE754 float. + + ## Example + + ```elixir + +5.0 = IEEE754.from_2_regs(0x40a0, 0x0000) + ``` + """ def from_2_regs([w0, w1]) do <> = <> f end + + @doc """ + Converts a list of 2n 16-bit registers to a IEEE754 floats list. + + ## Example + + ```elixir + [-5.0, +5.0] = IEEE754.from_2n_regs([0xc0a0, 0x0000, 0x40a0, 0x0000]) + ``` + """ def from_2n_regs([]), do: [] def from_2n_regs([w0, w1 | tail]) do [from_2_regs(w0, w1) | from_2n_regs(tail)] end + @doc """ + Converts a IEEE754 float to a couple of 16-bit registers. + + ## Example + + ```elixir + [0xc0a0, 0x0000] = IEEE754.to_2_regs(-5.0) + ``` + """ def to_2_regs(f) do <> = <> [w0, w1] end + @doc """ + Converts a list of IEEE754 floats to a list of 2n 16-bit registers. + + ## Example + + ```elixir + [0xc0a0, 0x0000, 0x40a0, 0x0000] = IEEE754.to_2n_regs([-5.0, +5.0]) + ``` + """ def to_2n_regs([]), do: [] def to_2n_regs([f | tail]) do [w0, w1] = to_2_regs(f) diff --git a/lib/master.ex b/lib/master.ex index 67fde1e..67a3298 100644 --- a/lib/master.ex +++ b/lib/master.ex @@ -5,6 +5,7 @@ defmodule Modbus.Tcp.Master do ## Example ```elixir + #run with: mix opto22 alias Modbus.Tcp.Master # opto22 rack configured as follows @@ -34,10 +35,15 @@ defmodule Modbus.Tcp.Master do #alternate m1.p2 and m1.p3 :ok = Master.exec(pid, {:fc, 1, 6, [1, 0]}) + #https://www.h-schmidt.net/FloatConverter/IEEE754.html #write -5V (IEEE 754 float) to m3.p0 + #<<-5::float-32>> -> <<192, 160, 0, 0>> :ok = Master.exec(pid, {:phr, 1, 24, [0xc0a0, 0x0000]}) + :ok = Master.exec(pid, {:phr, 1, 24, Modbus.IEEE754.to_2_regs(-5.0)}) #write +5V (IEEE 754 float) to m3.p1 + #<<+5::float-32>> -> <<64, 160, 0, 0>> :ok = Master.exec(pid, {:phr, 1, 26, [0x40a0, 0x0000]}) + :ok = Master.exec(pid, {:phr, 1, 26, Modbus.IEEE754.to_2_regs(+5.0)}) :timer.sleep(20) #outputs settle delay @@ -46,6 +52,8 @@ defmodule Modbus.Tcp.Master do #read previous analog channels as input registers {:ok, [0xc0a0, 0x0000, 0x40a0, 0x0000]} = Master.exec(pid, {:rir, 1, 24, 4}) + {:ok, data} = Master.exec(pid, {:rir, 1, 24, 4}) + [-5.0, +5.0] = Modbus.IEEE754.from_2n_regs(data) ``` """ alias Modbus.Tcp @@ -71,7 +79,6 @@ defmodule Modbus.Tcp.Master do ```elixir Modbus.Tcp.Master.start_link([ip: {10,77,0,2}, port: 502, timeout: 2000]) ``` - """ def start_link(params, opts \\ []) do Agent.start_link(fn -> init(params) end, opts) diff --git a/lib/shared.ex b/lib/shared.ex index 229c1df..d5c9654 100644 --- a/lib/shared.ex +++ b/lib/shared.ex @@ -1,4 +1,5 @@ defmodule Modbus.Model.Shared do + @moduledoc false alias Modbus.Model def start_link(params, opts \\ []) do