From 4832dc1b3c3c0aa35d2e823c88da269e207d2453 Mon Sep 17 00:00:00 2001 From: Ryota Kinukawa Date: Fri, 27 Sep 2024 15:06:36 +0900 Subject: [PATCH] Modify PDU handling --- lib/modbuzz/pdu/read_coils.ex | 9 ++++++-- lib/modbuzz/pdu/read_discrete_inputs.ex | 9 ++++++-- lib/modbuzz/pdu/read_holding_registers.ex | 23 +++++++++++++-------- lib/modbuzz/pdu/read_input_registers.ex | 9 ++++++-- lib/modbuzz/pdu/write_multiple_coils.ex | 6 +++++- lib/modbuzz/pdu/write_multiple_registers.ex | 6 +++++- 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/modbuzz/pdu/read_coils.ex b/lib/modbuzz/pdu/read_coils.ex index 19d68df..b58e748 100644 --- a/lib/modbuzz/pdu/read_coils.ex +++ b/lib/modbuzz/pdu/read_coils.ex @@ -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(), @@ -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 """ diff --git a/lib/modbuzz/pdu/read_discrete_inputs.ex b/lib/modbuzz/pdu/read_discrete_inputs.ex index 208469d..7260755 100644 --- a/lib/modbuzz/pdu/read_discrete_inputs.ex +++ b/lib/modbuzz/pdu/read_discrete_inputs.ex @@ -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(), @@ -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 """ diff --git a/lib/modbuzz/pdu/read_holding_registers.ex b/lib/modbuzz/pdu/read_holding_registers.ex index fba596c..3d8119e 100644 --- a/lib/modbuzz/pdu/read_holding_registers.ex +++ b/lib/modbuzz/pdu/read_holding_registers.ex @@ -42,14 +42,18 @@ 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 @@ -57,14 +61,15 @@ defmodule Modbuzz.PDU.ReadHoldingRegisters do @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 """ @@ -72,14 +77,14 @@ defmodule Modbuzz.PDU.ReadHoldingRegisters do 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 diff --git a/lib/modbuzz/pdu/read_input_registers.ex b/lib/modbuzz/pdu/read_input_registers.ex index 2293650..12e7eef 100644 --- a/lib/modbuzz/pdu/read_input_registers.ex +++ b/lib/modbuzz/pdu/read_input_registers.ex @@ -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(), @@ -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 """ diff --git a/lib/modbuzz/pdu/write_multiple_coils.ex b/lib/modbuzz/pdu/write_multiple_coils.ex index bf19a93..730ef50 100644 --- a/lib/modbuzz/pdu/write_multiple_coils.ex +++ b/lib/modbuzz/pdu/write_multiple_coils.ex @@ -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, diff --git a/lib/modbuzz/pdu/write_multiple_registers.ex b/lib/modbuzz/pdu/write_multiple_registers.ex index 901d4ad..882f336 100644 --- a/lib/modbuzz/pdu/write_multiple_registers.ex +++ b/lib/modbuzz/pdu/write_multiple_registers.ex @@ -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,