Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify PDU handling #14

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/modbuzz/pdu/read_coils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ defmodule Modbuzz.PDU.ReadCoils do
end

defmodule Res do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)
@moduledoc """
#{Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)}

`byte_count` is automatically calculated during encoding.
"""

@type t :: %__MODULE__{
byte_count: byte(),
Expand All @@ -64,7 +68,8 @@ defmodule Modbuzz.PDU.ReadCoils do
"""
def encode(struct) do
binary = struct.coil_status |> Modbuzz.PDU.Helper.to_binary()
<<@function_code, struct.byte_count, binary::binary-size(struct.byte_count)>>
byte_count = byte_size(binary)
<<@function_code, byte_count, binary::binary-size(byte_count)>>
end

@doc """
Expand Down
9 changes: 7 additions & 2 deletions lib/modbuzz/pdu/read_discrete_inputs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ defmodule Modbuzz.PDU.ReadDiscreteInputs do
end

defmodule Res do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)
@moduledoc """
#{Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)}

`byte_count` is automatically calculated during encoding.
"""

@type t :: %__MODULE__{
byte_count: byte(),
Expand All @@ -64,7 +68,8 @@ defmodule Modbuzz.PDU.ReadDiscreteInputs do
"""
def encode(struct) do
binary = struct.input_status |> Modbuzz.PDU.Helper.to_binary()
<<@function_code, struct.byte_count, binary::binary-size(struct.byte_count)>>
byte_count = byte_size(binary)
<<@function_code, byte_count, binary::binary-size(byte_count)>>
end

@doc """
Expand Down
23 changes: 14 additions & 9 deletions lib/modbuzz/pdu/read_holding_registers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,49 @@ defmodule Modbuzz.PDU.ReadHoldingRegisters do
end

defmodule Res do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)
@moduledoc """
#{Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)}

`byte_count` is automatically calculated during encoding.
"""

@type t :: %__MODULE__{
byte_count: byte(),
register_value: 0x0000..0xFFFF
register_values: [0x0000..0xFFFF]
}

defstruct [:byte_count, :register_value]
defstruct [:byte_count, :register_values]

defimpl Modbuzz.PDU.Protocol do
@function_code 0x03

@doc """
iex> res = %Modbuzz.PDU.ReadHoldingRegisters.Res{
...> byte_count: 0x06,
...> register_value: [555, 0, 100]
...> register_values: [555, 0, 100]
...> }
iex> Modbuzz.PDU.Protocol.encode(res)
<<#{@function_code}, 0x06, 0x02, 0x2B, 0x00, 0x00, 0x00, 0x64>>
"""
def encode(struct) do
binary = struct.register_value |> Modbuzz.PDU.Helper.to_binary()
<<@function_code, struct.byte_count, binary::binary-size(struct.byte_count)>>
binary = struct.register_values |> Modbuzz.PDU.Helper.to_binary()
byte_count = byte_size(binary)
<<@function_code, byte_count, binary::binary-size(byte_count)>>
end

@doc """
iex> res = %Modbuzz.PDU.ReadHoldingRegisters.Res{}
iex> Modbuzz.PDU.Protocol.decode(res, <<#{@function_code}, 0x06, 0x02, 0x2B, 0x00, 0x00, 0x00, 0x64>>)
%Modbuzz.PDU.ReadHoldingRegisters.Res{
byte_count: 0x06,
register_value: [555, 0, 100]
register_values: [555, 0, 100]
}
"""
def decode(struct, <<@function_code, byte_count, register_value::binary-size(byte_count)>>) do
def decode(struct, <<@function_code, byte_count, register_values::binary-size(byte_count)>>) do
%{
struct
| byte_count: byte_count,
register_value: Modbuzz.PDU.Helper.to_registers(register_value)
register_values: Modbuzz.PDU.Helper.to_registers(register_values)
}
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/modbuzz/pdu/read_input_registers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ defmodule Modbuzz.PDU.ReadInputRegisters do
end

defmodule Res do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)
@moduledoc """
#{Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)}

`byte_count` is automatically calculated during encoding.
"""

@type t :: %__MODULE__{
byte_count: byte(),
Expand All @@ -67,7 +71,8 @@ defmodule Modbuzz.PDU.ReadInputRegisters do
"""
def encode(struct) do
binary = struct.input_registers |> Modbuzz.PDU.Helper.to_binary()
<<@function_code, struct.byte_count, binary::binary-size(struct.byte_count)>>
byte_count = byte_size(binary)
<<@function_code, byte_count, binary::binary-size(byte_count)>>
end

@doc """
Expand Down
6 changes: 5 additions & 1 deletion lib/modbuzz/pdu/write_multiple_coils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ defmodule Modbuzz.PDU.WriteMultipleCoils do
@moduledoc false

defmodule Req do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)
@moduledoc """
Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)

`byte_count` and `quantity_of_outputs` are automatically calculated during encoding.
"""

@type t :: %__MODULE__{
starting_address: 0x0000..0xFFFF,
Expand Down
6 changes: 5 additions & 1 deletion lib/modbuzz/pdu/write_multiple_registers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ defmodule Modbuzz.PDU.WriteMultipleRegisters do
@moduledoc false

defmodule Req do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)
@moduledoc """
Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)

`byte_count` and `quantity_of_registers` are automatically calculated during encoding.
"""

@type t :: %__MODULE__{
starting_address: 0x0000..0xFFFF,
Expand Down