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

View File

@ -96,11 +96,12 @@ handle_info({timeout, _, {poll_ticker, Name}}, State = #state{parent_pid = Paren
%%
poll_ticker(PollInterval, Name),
#modbus_metric{address = Address} = maps:get(Name, MetricsMap),
#modbus_metric{address = Address, type = Type} = maps:get(Name, MetricsMap),
%%
ReceiverPid = self(),
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)}};
@ -109,9 +110,11 @@ handle_info({request_reply, Ref, Val}, State = #state{parent_pid = ParentPid, de
error ->
{noreply, State};
{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]),
%% todo
RealVal = parse_value(Val, Type),
lager:debug("[modbus_device] real val is: ~p", [RealVal]),
%% 广
Key = <<"$", DeviceName/binary, ".", MetricName/binary>>,
@ -156,3 +159,37 @@ start_ticker([Name|T], Step) ->
-spec poll_ticker(PollInterval :: integer(), Name :: binary()) -> no_return().
poll_ticker(PollInterval, Name) when is_integer(PollInterval), is_binary(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),
mode = #rtu_mode{port = Port, delay_ms = DelayMs},
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),
mode = #tcp_mode{socket = Socket},
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
{rtu, Port, DelayMs} ->
{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}
end;
{error, Reason} ->
@ -292,7 +292,8 @@ connect(#modbus_transport_tcp{host = Host, port = Port, timeout = Timeout0}) ->
false ->
2000
end,
gen_tcp:connect(Host, Port, [binary], Timeout).
Socket = gen_tcp:connect(Host, Port, [binary], Timeout),
{ok, {tcp, Socket}}.
%% 3.5
-spec frame_delay(BaudRate :: integer(), DataBits :: integer(), Parity :: integer(), StopBits :: integer()) -> integer().

View File

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