fix service
This commit is contained in:
parent
8712cd0707
commit
9e3842e645
@ -4,26 +4,28 @@
|
|||||||
%%% @doc
|
%%% @doc
|
||||||
%%%
|
%%%
|
||||||
%%% @end
|
%%% @end
|
||||||
%%% Created : 13. 6月 2025 09:46
|
%%% Created : 20. 6月 2025 16:38
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(modbus_service).
|
-module(modbus_service).
|
||||||
-author("anlicheng").
|
-author("anlicheng").
|
||||||
-include("modbus_ast.hrl").
|
-include("modbus_ast.hrl").
|
||||||
|
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_statem).
|
||||||
|
|
||||||
%% rtu指令
|
|
||||||
-define(MODBUS_CONNECT, 16#01).
|
|
||||||
-define(MODBUS_READ, 16#02).
|
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/1]).
|
-export([start_link/1]).
|
||||||
-export([test/0]).
|
-export([test/0]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_statem callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
-export([init/1, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
||||||
|
|
||||||
-define(SERVER, ?MODULE).
|
%% rtu指令
|
||||||
|
-define(MODBUS_CONNECT, 16#01).
|
||||||
|
-define(MODBUS_READ, 16#02).
|
||||||
|
|
||||||
|
%% 当前的状态
|
||||||
|
-define(DISCONNECTED, disconnected).
|
||||||
|
-define(CONNECTED, connected).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
port,
|
port,
|
||||||
@ -31,33 +33,31 @@
|
|||||||
error_log_pid :: pid() | undefined
|
error_log_pid :: pid() | undefined
|
||||||
}).
|
}).
|
||||||
|
|
||||||
|
%%%===================================================================
|
||||||
|
%%% API
|
||||||
|
%%%===================================================================
|
||||||
|
|
||||||
test() ->
|
test() ->
|
||||||
{ok, Config} = file:read_file("/usr/local/code/cloudkit/modbus/modbus.conf"),
|
{ok, Config} = file:read_file("/usr/local/code/cloudkit/modbus/modbus.conf"),
|
||||||
%lager:debug("config is: ~ts", [Config]),
|
%lager:debug("config is: ~ts", [Config]),
|
||||||
{ok, AST} = modbus_parser:parse(Config),
|
{ok, AST} = modbus_parser:parse(Config),
|
||||||
start_link(AST).
|
start_link(AST).
|
||||||
|
|
||||||
%%%===================================================================
|
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
||||||
%%% API
|
%% initialize. To ensure a synchronized start-up procedure, this
|
||||||
%%%===================================================================
|
%% function does not return until Module:init/1 has returned.
|
||||||
|
start_link(AST = #ast{}) ->
|
||||||
%% @doc Spawns the server and registers the local name (unique)
|
gen_statem:start_link(?MODULE, [AST], []).
|
||||||
-spec(start_link(AST :: #ast{}) ->
|
|
||||||
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
|
|
||||||
start_link(AST) ->
|
|
||||||
gen_server:start_link(?MODULE, [AST], []).
|
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% gen_server callbacks
|
%%% gen_statem callbacks
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Initializes the server
|
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or
|
||||||
-spec(init(Args :: term()) ->
|
%% gen_statem:start_link/[3,4], this function is called by the new
|
||||||
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
%% process to initialize.
|
||||||
{stop, Reason :: term()} | ignore).
|
|
||||||
init([AST = #ast{modbus = Modbus, devices = Devices}]) ->
|
init([AST = #ast{modbus = Modbus, devices = Devices}]) ->
|
||||||
|
|
||||||
lager:debug("modbus is: ~p", [Modbus]),
|
lager:debug("modbus is: ~p", [Modbus]),
|
||||||
Device = hd(Devices),
|
Device = hd(Devices),
|
||||||
lager:debug("devices is: ~p", [Device#modbus_device.metrics]),
|
lager:debug("devices is: ~p", [Device#modbus_device.metrics]),
|
||||||
@ -70,66 +70,45 @@ init([AST = #ast{modbus = Modbus, devices = Devices}]) ->
|
|||||||
%{ok, AccessLogPid} = modbus_logger:start_link(AccessLog),
|
%{ok, AccessLogPid} = modbus_logger:start_link(AccessLog),
|
||||||
%{ok, ErrorLogPid} = modbus_logger:start_link(ErrorLog),
|
%{ok, ErrorLogPid} = modbus_logger:start_link(ErrorLog),
|
||||||
|
|
||||||
{ok, #state{access_log_pid = undefined, error_log_pid = undefined}}.
|
{ok, ?DISCONNECTED, #state{}}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Handling call messages
|
%% @doc This function is called by a gen_statem when it needs to find out
|
||||||
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
|
%% the callback mode of the callback module.
|
||||||
State :: #state{}) ->
|
callback_mode() ->
|
||||||
{reply, Reply :: term(), NewState :: #state{}} |
|
handle_event_function.
|
||||||
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_call(_Request, _From, State = #state{}) ->
|
|
||||||
{reply, ok, State}.
|
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Handling cast messages
|
%% @doc If callback_mode is handle_event_function, then whenever a
|
||||||
-spec(handle_cast(Request :: term(), State :: #state{}) ->
|
%% gen_statem receives an event from call/2, cast/2, or as a normal
|
||||||
{noreply, NewState :: #state{}} |
|
%% process message, this function is called.
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
handle_cast(_Request, State = #state{}) ->
|
|
||||||
{noreply, State}.
|
|
||||||
|
|
||||||
%% @private
|
handle_event(info, {read, SlaveId, Address}, ?CONNECTED, State = #state{port = Port}) ->
|
||||||
%% @doc Handling all non call/cast messages
|
|
||||||
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
|
|
||||||
{noreply, NewState :: #state{}} |
|
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
|
||||||
%% 发送读取指令
|
|
||||||
handle_info({read, SlaveId, Address}, State = #state{port = Port}) ->
|
|
||||||
ReadCmd = <<?MODBUS_READ:8, SlaveId:8, Address:16>>,
|
ReadCmd = <<?MODBUS_READ:8, SlaveId:8, Address:16>>,
|
||||||
Port ! {self(), {command, ReadCmd}},
|
Port ! {self(), {command, ReadCmd}},
|
||||||
ok;
|
{keep_state, State};
|
||||||
|
|
||||||
handle_info({Port, {data, Data}}, State = #state{port = Port}) ->
|
%% 从port读取数据
|
||||||
|
handle_event(info, {Port, {data, Data}}, ?CONNECTED, State = #state{port = Port}) ->
|
||||||
lager:debug("port data is: ~p", [Data]),
|
lager:debug("port data is: ~p", [Data]),
|
||||||
{noreply, State};
|
{keep_state, State};
|
||||||
|
|
||||||
handle_info(_Info, State = #state{}) ->
|
handle_event(_EventType, _EventContent, _StateName, State = #state{}) ->
|
||||||
{noreply, State}.
|
NextStateName = the_next_state_name,
|
||||||
|
{next_state, NextStateName, State}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc This function is called by a gen_server when it is about to
|
%% @doc This function is called by a gen_statem when it is about to
|
||||||
%% terminate. It should be the opposite of Module:init/1 and do any
|
%% terminate. It should be the opposite of Module:init/1 and do any
|
||||||
%% necessary cleaning up. When it returns, the gen_server terminates
|
%% necessary cleaning up. When it returns, the gen_statem terminates with
|
||||||
%% with Reason. The return value is ignored.
|
%% Reason. The return value is ignored.
|
||||||
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
|
terminate(_Reason, _StateName, _State = #state{}) ->
|
||||||
State :: #state{}) -> term()).
|
|
||||||
terminate(_Reason, _State = #state{}) ->
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
%% @doc Convert process state when code is changed
|
%% @doc Convert process state when code is changed
|
||||||
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
|
code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
||||||
Extra :: term()) ->
|
{ok, StateName, State}.
|
||||||
{ok, NewState :: #state{}} | {error, Reason :: term()}).
|
|
||||||
code_change(_OldVsn, State = #state{}, _Extra) ->
|
|
||||||
{ok, State}.
|
|
||||||
|
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
@ -152,4 +131,4 @@ connect(#modbus{transport = #modbus_transport_tcp{host = Host, port = Port, time
|
|||||||
false ->
|
false ->
|
||||||
2000
|
2000
|
||||||
end,
|
end,
|
||||||
gen_tcp:connect(Host, Port, [binary], Timeout).
|
gen_tcp:connect(Host, Port, [binary], Timeout).
|
||||||
@ -1,134 +0,0 @@
|
|||||||
%%%-------------------------------------------------------------------
|
|
||||||
%%% @author anlicheng
|
|
||||||
%%% @copyright (C) 2025, <COMPANY>
|
|
||||||
%%% @doc
|
|
||||||
%%%
|
|
||||||
%%% @end
|
|
||||||
%%% Created : 20. 6月 2025 16:38
|
|
||||||
%%%-------------------------------------------------------------------
|
|
||||||
-module(modbus_service2).
|
|
||||||
-author("anlicheng").
|
|
||||||
-include("modbus_ast.hrl").
|
|
||||||
|
|
||||||
-behaviour(gen_statem).
|
|
||||||
|
|
||||||
%% API
|
|
||||||
-export([start_link/1]).
|
|
||||||
-export([test/0]).
|
|
||||||
|
|
||||||
%% gen_statem callbacks
|
|
||||||
-export([init/1, format_status/2, state_name/3, handle_event/4, terminate/3, code_change/4, callback_mode/0]).
|
|
||||||
|
|
||||||
%% rtu指令
|
|
||||||
-define(MODBUS_CONNECT, 16#01).
|
|
||||||
-define(MODBUS_READ, 16#02).
|
|
||||||
|
|
||||||
%% 当前的状态
|
|
||||||
-define(DISCONNECTED, disconnected).
|
|
||||||
-define(CONNECTED, connected).
|
|
||||||
|
|
||||||
-record(state, {
|
|
||||||
port,
|
|
||||||
access_log_pid :: pid() | undefined,
|
|
||||||
error_log_pid :: pid() | undefined
|
|
||||||
}).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% API
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
test() ->
|
|
||||||
{ok, Config} = file:read_file("/usr/local/code/cloudkit/modbus/modbus.conf"),
|
|
||||||
%lager:debug("config is: ~ts", [Config]),
|
|
||||||
{ok, AST} = modbus_parser:parse(Config),
|
|
||||||
start_link(AST).
|
|
||||||
|
|
||||||
%% @doc Creates a gen_statem process which calls Module:init/1 to
|
|
||||||
%% initialize. To ensure a synchronized start-up procedure, this
|
|
||||||
%% function does not return until Module:init/1 has returned.
|
|
||||||
start_link(AST = #ast{}) ->
|
|
||||||
gen_statem:start_link(?MODULE, [AST], []).
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% gen_statem callbacks
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Whenever a gen_statem is started using gen_statem:start/[3,4] or
|
|
||||||
%% gen_statem:start_link/[3,4], this function is called by the new
|
|
||||||
%% process to initialize.
|
|
||||||
init([AST = #ast{modbus = Modbus, devices = Devices}]) ->
|
|
||||||
lager:debug("modbus is: ~p", [Modbus]),
|
|
||||||
Device = hd(Devices),
|
|
||||||
lager:debug("devices is: ~p", [Device#modbus_device.metrics]),
|
|
||||||
% Res = connect(Transport),
|
|
||||||
% lager:debug("connect res: ~p", [Res]),
|
|
||||||
|
|
||||||
{ok, DevicePid} = modbus_device:start_link(hd(Devices)),
|
|
||||||
lager:debug("device pid: ~p", [DevicePid]),
|
|
||||||
|
|
||||||
%{ok, AccessLogPid} = modbus_logger:start_link(AccessLog),
|
|
||||||
%{ok, ErrorLogPid} = modbus_logger:start_link(ErrorLog),
|
|
||||||
|
|
||||||
{ok, ?DISCONNECTED, #state{}}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc This function is called by a gen_statem when it needs to find out
|
|
||||||
%% the callback mode of the callback module.
|
|
||||||
callback_mode() ->
|
|
||||||
handle_event_function.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc If callback_mode is handle_event_function, then whenever a
|
|
||||||
%% gen_statem receives an event from call/2, cast/2, or as a normal
|
|
||||||
%% process message, this function is called.
|
|
||||||
|
|
||||||
handle_event(info, {read, SlaveId, Address}, ?CONNECTED, State = #state{port = Port}) ->
|
|
||||||
ReadCmd = <<?MODBUS_READ:8, SlaveId:8, Address:16>>,
|
|
||||||
Port ! {self(), {command, ReadCmd}},
|
|
||||||
{keep_state, State};
|
|
||||||
|
|
||||||
%% 从port读取数据
|
|
||||||
handle_event(info, {Port, {data, Data}}, ?CONNECTED, State = #state{port = Port}) ->
|
|
||||||
lager:debug("port data is: ~p", [Data]),
|
|
||||||
{keep_state, State};
|
|
||||||
|
|
||||||
handle_event(_EventType, _EventContent, _StateName, State = #state{}) ->
|
|
||||||
NextStateName = the_next_state_name,
|
|
||||||
{next_state, NextStateName, State}.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc This function is called by a gen_statem when it is about to
|
|
||||||
%% terminate. It should be the opposite of Module:init/1 and do any
|
|
||||||
%% necessary cleaning up. When it returns, the gen_statem terminates with
|
|
||||||
%% Reason. The return value is ignored.
|
|
||||||
terminate(_Reason, _StateName, _State = #state{}) ->
|
|
||||||
ok.
|
|
||||||
|
|
||||||
%% @private
|
|
||||||
%% @doc Convert process state when code is changed
|
|
||||||
code_change(_OldVsn, StateName, State = #state{}, _Extra) ->
|
|
||||||
{ok, StateName, State}.
|
|
||||||
|
|
||||||
%%%===================================================================
|
|
||||||
%%% Internal functions
|
|
||||||
%%%===================================================================
|
|
||||||
|
|
||||||
connect(#modbus{transport = #modbus_transport_rtu{port = Port, baudrate = Baudrate, stopbits = Stopbits, parity = Parity, timeout = Timeout}}) ->
|
|
||||||
RealExecCmd = "",
|
|
||||||
Port = erlang:open_port({spawn_executable, RealExecCmd}, [binary, {packet, 2}, exit_status]),
|
|
||||||
|
|
||||||
Len0 = byte_size(Port),
|
|
||||||
ConnectCmd = <<?MODBUS_CONNECT:8, Len0:8, Port/binary, Baudrate:32, Stopbits:8, Parity:8, Timeout:32>>,
|
|
||||||
%% 建立连接
|
|
||||||
Port ! {self(), {command, ConnectCmd}},
|
|
||||||
ok;
|
|
||||||
|
|
||||||
connect(#modbus{transport = #modbus_transport_tcp{host = Host, port = Port, timeout = Timeout0}}) ->
|
|
||||||
Timeout = case is_integer(Timeout0) andalso Timeout0 > 0 of
|
|
||||||
true ->
|
|
||||||
Timeout0;
|
|
||||||
false ->
|
|
||||||
2000
|
|
||||||
end,
|
|
||||||
gen_tcp:connect(Host, Port, [binary], Timeout).
|
|
||||||
Loading…
x
Reference in New Issue
Block a user