diff --git a/lib/modbuzz/rtu/client/receiver.ex b/lib/modbuzz/rtu/client/receiver.ex index 72fd66e..55e8299 100644 --- a/lib/modbuzz/rtu/client/receiver.ex +++ b/lib/modbuzz/rtu/client/receiver.ex @@ -5,6 +5,7 @@ defmodule Modbuzz.RTU.Client.Receiver do alias Modbuzz.PDU alias Modbuzz.RTU.ADU + alias Modbuzz.RTU.Log @server_device_failure 0x04 @server_device_busy 0x06 @@ -78,7 +79,8 @@ defmodule Modbuzz.RTU.Client.Receiver do # already responded {:noreply, state} else - # something wrong, treat as server failure + Log.error("RTU server didn't respond.") + # treat as server device failure {:ok, req} = PDU.decode_request(adu.pdu) err = PDU.to_error(req, @server_device_failure) GenServer.reply(caller, {:error, err}) @@ -107,7 +109,8 @@ defmodule Modbuzz.RTU.Client.Receiver do {:error, :binary_is_short} -> {:noreply, %{state | binary: new_binary}} - {:error, %ADU{unit_id: unit_id, pdu: _pdu, crc_valid?: false}} -> + {:error, %ADU{unit_id: unit_id, pdu: _pdu, crc_valid?: false} = adu} -> + Log.warning("CRC error detected, #{inspect(adu)}.") caller = Enum.fetch!(callers, unit_id) if not is_nil(caller), do: GenServer.reply(caller, {:error, :crc_error}) diff --git a/lib/modbuzz/rtu/log.ex b/lib/modbuzz/rtu/log.ex index 4e8a9b7..59d15ec 100644 --- a/lib/modbuzz/rtu/log.ex +++ b/lib/modbuzz/rtu/log.ex @@ -11,6 +11,10 @@ defmodule Modbuzz.RTU.Log do Logger.error(what_happend <> why(reason) <> where(state)) end + def warning(what_happend) do + Logger.warning(what_happend) + end + def warning(what_happend, reason, state) do Logger.warning(what_happend <> why(reason) <> where(state)) end diff --git a/lib/modbuzz/rtu/server.ex b/lib/modbuzz/rtu/server.ex index b8c53ec..8e17508 100644 --- a/lib/modbuzz/rtu/server.ex +++ b/lib/modbuzz/rtu/server.ex @@ -5,6 +5,7 @@ defmodule Modbuzz.RTU.Server do alias Modbuzz.PDU alias Modbuzz.RTU.ADU + alias Modbuzz.RTU.Log @spec start_link(keyword()) :: GenServer.on_start() def start_link(args) when is_list(args) do @@ -58,7 +59,8 @@ defmodule Modbuzz.RTU.Server do {:error, :binary_is_short} -> {:noreply, %{state | binary: new_binary}} - {:error, %ADU{unit_id: _unit_id, pdu: _pdu, crc_valid?: false}} -> + {:error, %ADU{unit_id: _unit_id, pdu: _pdu, crc_valid?: false} = adu} -> + Log.warning("CRC error detected, #{inspect(adu)}.") # ignore request {:noreply, %{state | binary: <<>>}} end