From a2be2aaffeb8063cb25bdbd8ca4fb0eff0e3a6b2 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 2 Jul 2025 22:29:29 +0800 Subject: [PATCH] fix --- apps/modbus/include/modbus_ast.hrl | 26 ++++++++--------- apps/modbus/src/modbus_device.erl | 45 +++++++++++++++++++++++++++--- apps/modbus/src/modbus_service.erl | 7 +++-- modbus.conf | 4 --- 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/apps/modbus/include/modbus_ast.hrl b/apps/modbus/include/modbus_ast.hrl index 2634501..8f84e32 100644 --- a/apps/modbus/include/modbus_ast.hrl +++ b/apps/modbus/include/modbus_ast.hrl @@ -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() }). diff --git a/apps/modbus/src/modbus_device.erl b/apps/modbus/src/modbus_device.erl index 4b1d42d..ea2b01e 100644 --- a/apps/modbus/src/modbus_device.erl +++ b/apps/modbus/src/modbus_device.erl @@ -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>>, @@ -155,4 +158,38 @@ 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}). \ No newline at end of file + 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; +parse_value(<>, <<"int16">>) -> + Val; +parse_value(<>, <<"uint16">>) -> + Val; +parse_value(<>, <<"int32">>) -> + Val; +parse_value(<>, <<"uint32">>) -> + Val; +parse_value(<>, <<"float32">>) -> + Val; +parse_value(<>, <<"float32">>) -> + Val. \ No newline at end of file diff --git a/apps/modbus/src/modbus_service.erl b/apps/modbus/src/modbus_service.erl index adc5f65..99214d1 100644 --- a/apps/modbus/src/modbus_service.erl +++ b/apps/modbus/src/modbus_service.erl @@ -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(). diff --git a/modbus.conf b/modbus.conf index b96d0c4..5fd093d 100644 --- a/modbus.conf +++ b/modbus.conf @@ -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; } # 状态位(线圈)