Skip to content

Commit

Permalink
added float helper doc
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelventura committed Mar 25, 2017
1 parent aeb7f91 commit 28ad695
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
51 changes: 51 additions & 0 deletions lib/float.ex
Original file line number Diff line number Diff line change
@@ -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::float-32>> = <<w0::16, w1::16>>
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::float-32>> = <<w0::16, w1::16>>
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::16, w1::16>> = <<f::float-32>>
[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)
Expand Down
9 changes: 8 additions & 1 deletion lib/master.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Modbus.Tcp.Master do
## Example
```elixir
#run with: mix opto22
alias Modbus.Tcp.Master
# opto22 rack configured as follows
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/shared.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Modbus.Model.Shared do
@moduledoc false
alias Modbus.Model

def start_link(params, opts \\ []) do
Expand Down

0 comments on commit 28ad695

Please sign in to comment.