This commit is contained in:
anlicheng 2025-07-02 22:29:29 +08:00
parent f782674451
commit a2be2aaffe
4 changed files with 58 additions and 24 deletions

View File

@ -17,15 +17,15 @@
}). }).
-record(modbus_transport_tcp, { -record(modbus_transport_tcp, {
host :: string(), host :: binary(),
port :: string(), port :: binary(),
timeout = 0 :: integer() timeout = 0 :: integer()
}). }).
-record(modbus, { -record(modbus, {
transport :: #modbus_transport_rtu{} | #modbus_transport_tcp{}, transport :: #modbus_transport_rtu{} | #modbus_transport_tcp{},
error_log = "" :: string(), error_log :: binary() | undefined,
access_log = "" :: string() access_log :: binary() | undefined
}). }).
-record(modbus_device_io, { -record(modbus_device_io, {
@ -45,12 +45,12 @@
-record(modbus_device, { -record(modbus_device, {
%% %%
name :: string(), name :: binary(),
%% %%
slave_id :: integer(), slave_id :: integer(),
model :: string() | undefined, model :: binary() | undefined,
description :: string() | undefined, description :: binary() | undefined,
device_io :: binary() | undefined, device_io :: binary() | undefined,
@ -82,19 +82,19 @@
}). }).
-record(modbus_processor, { -record(modbus_processor, {
name :: string(), name :: binary(),
input :: string(), input :: binary(),
transform :: any(), transform :: any(),
output :: [] output :: []
}). }).
-record(modbus_alarm, { -record(modbus_alarm, {
name :: string(), name :: binary(),
condition :: string(), condition :: binary(),
%% %%
hold_time :: string(), hold_time :: binary(),
%% %%
actions :: [], actions :: [],
@ -107,7 +107,7 @@
address :: integer(), address :: integer(),
type :: atom(), type :: atom(),
scale = 1.0 :: float(), scale = 1.0 :: float(),
unit :: string() | undefined, unit :: binary() | undefined,
poll = true :: boolean() poll = true :: boolean()
}). }).

View File

@ -96,11 +96,12 @@ handle_info({timeout, _, {poll_ticker, Name}}, State = #state{parent_pid = Paren
%% %%
poll_ticker(PollInterval, Name), poll_ticker(PollInterval, Name),
#modbus_metric{address = Address} = maps:get(Name, MetricsMap), #modbus_metric{address = Address, type = Type} = maps:get(Name, MetricsMap),
%% %%
ReceiverPid = self(), ReceiverPid = self(),
Ref = make_ref(), Ref = make_ref(),
ParentPid ! {request, ReceiverPid, Ref, SlaveId, Address, 1}, Cnt = get_register_count(Type),
ParentPid ! {request, ReceiverPid, Ref, SlaveId, Address, Cnt},
{noreply, State#state{inflight = maps:put(Ref, Name, Inflight)}}; {noreply, State#state{inflight = maps:put(Ref, Name, Inflight)}};
@ -109,9 +110,11 @@ handle_info({request_reply, Ref, Val}, State = #state{parent_pid = ParentPid, de
error -> error ->
{noreply, State}; {noreply, State};
{MetricName, NInflight} -> {MetricName, NInflight} ->
#modbus_metric{} = maps:get(MetricName, MetricsMap), #modbus_metric{type = Type} = maps:get(MetricName, MetricsMap),
lager:debug("[modbus_device] metric_name: ~p, get value: ~p", [MetricName, Val]), lager:debug("[modbus_device] metric_name: ~p, get value: ~p", [MetricName, Val]),
%% todo %% todo
RealVal = parse_value(Val, Type),
lager:debug("[modbus_device] real val is: ~p", [RealVal]),
%% 广 %% 广
Key = <<"$", DeviceName/binary, ".", MetricName/binary>>, Key = <<"$", DeviceName/binary, ".", MetricName/binary>>,
@ -155,4 +158,38 @@ start_ticker([Name|T], Step) ->
-spec poll_ticker(PollInterval :: integer(), Name :: binary()) -> no_return(). -spec poll_ticker(PollInterval :: integer(), Name :: binary()) -> no_return().
poll_ticker(PollInterval, Name) when is_integer(PollInterval), is_binary(Name) -> poll_ticker(PollInterval, Name) when is_integer(PollInterval), is_binary(Name) ->
erlang:start_timer(PollInterval, self(), {poll_ticker, Name}). erlang:start_timer(PollInterval, self(), {poll_ticker, Name}).
get_register_count(<<"int16">>) ->
1;
get_register_count(<<"uint16">>) ->
1;
get_register_count(<<"int32">>) ->
2;
get_register_count(<<"uint32">>) ->
2;
get_register_count(<<"int64">>) ->
4;
get_register_count(<<"uint64">>) ->
4;
get_register_count(<<"float32">>) ->
2;
get_register_count(<<"float64">>) ->
4;
get_register_count(_) ->
1.
parse_value(<<Val:8>>, _) ->
Val;
parse_value(<<Val:16>>, <<"int16">>) ->
Val;
parse_value(<<Val:16>>, <<"uint16">>) ->
Val;
parse_value(<<Val:32>>, <<"int32">>) ->
Val;
parse_value(<<Val:32/unsigned-integer>>, <<"uint32">>) ->
Val;
parse_value(<<Val:32/big-float>>, <<"float32">>) ->
Val;
parse_value(<<Val:64/big-float>>, <<"float32">>) ->
Val.

View File

@ -98,7 +98,7 @@ init([AST = #ast{modbus = Modbus = #modbus{transport = Transport, error_log = Er
{ok, ?CONNECTED, #state{ast = AST, access_log_pid = create_log_file(AccessLog), error_log_pid = create_log_file(ErrorLog), {ok, ?CONNECTED, #state{ast = AST, access_log_pid = create_log_file(AccessLog), error_log_pid = create_log_file(ErrorLog),
mode = #rtu_mode{port = Port, delay_ms = DelayMs}, mode = #rtu_mode{port = Port, delay_ms = DelayMs},
queue = queue:new(), packet_id = 1, devices_map = DevicesMap, alarm_pids = []}}; queue = queue:new(), packet_id = 1, devices_map = DevicesMap, alarm_pids = []}};
Socket -> {tcp, Socket} ->
{ok, ?CONNECTED, #state{ast = AST, access_log_pid = create_log_file(AccessLog), error_log_pid = create_log_file(ErrorLog), {ok, ?CONNECTED, #state{ast = AST, access_log_pid = create_log_file(AccessLog), error_log_pid = create_log_file(ErrorLog),
mode = #tcp_mode{socket = Socket}, mode = #tcp_mode{socket = Socket},
queue = queue:new(), packet_id = 1, devices_map = DevicesMap, alarm_pids = []}} queue = queue:new(), packet_id = 1, devices_map = DevicesMap, alarm_pids = []}}
@ -218,7 +218,7 @@ handle_event(info, {timeout, _Ref, modbus_connect}, ?DISCONNECTED, State = #stat
case ConnectResult of case ConnectResult of
{rtu, Port, DelayMs} -> {rtu, Port, DelayMs} ->
{next_state, ?CONNECTED, State#state{mode = #rtu_mode{port = Port, delay_ms = DelayMs}}, Actions}; {next_state, ?CONNECTED, State#state{mode = #rtu_mode{port = Port, delay_ms = DelayMs}}, Actions};
Socket -> {tcp, Socket} ->
{next_state, ?CONNECTED, State#state{mode = #tcp_mode{socket = Socket}}, Actions} {next_state, ?CONNECTED, State#state{mode = #tcp_mode{socket = Socket}}, Actions}
end; end;
{error, Reason} -> {error, Reason} ->
@ -292,7 +292,8 @@ connect(#modbus_transport_tcp{host = Host, port = Port, timeout = Timeout0}) ->
false -> false ->
2000 2000
end, end,
gen_tcp:connect(Host, Port, [binary], Timeout). Socket = gen_tcp:connect(Host, Port, [binary], Timeout),
{ok, {tcp, Socket}}.
%% 3.5 %% 3.5
-spec frame_delay(BaudRate :: integer(), DataBits :: integer(), Parity :: integer(), StopBits :: integer()) -> integer(). -spec frame_delay(BaudRate :: integer(), DataBits :: integer(), Parity :: integer(), StopBits :: integer()) -> integer().

View File

@ -36,7 +36,6 @@ device_io t1 {
type int16; type int16;
scale 0.1; scale 0.1;
unit "°C"; unit "°C";
poll on;
} }
# 压力传感器(输入寄存器) # 压力传感器(输入寄存器)
@ -45,7 +44,6 @@ device_io t1 {
type uint16; type uint16;
scale 0.01; scale 0.01;
unit "kPa"; unit "kPa";
poll on;
} }
} }
@ -119,7 +117,6 @@ device xyz {
type int16; type int16;
scale 0.1; scale 0.1;
unit "° C"; unit "° C";
poll on;
} }
# 压力传感器(输入寄存器) # 压力传感器(输入寄存器)
@ -128,7 +125,6 @@ device xyz {
type uint16; type uint16;
scale 0.01; scale 0.01;
unit "kPa"; unit "kPa";
poll on;
} }
# 状态位(线圈) # 状态位(线圈)