-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomd.erl
44 lines (40 loc) · 1.3 KB
/
comd.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-module(comd).
-export([p/0]).
string_to_num(S) ->
case string:to_float(S) of
{error,no_float} ->
list_to_integer(S);
{F,_Rest} ->
io:format("~p~n", [F]),
F
end.
loop(Port) ->
receive
{Port, {data, Data}} ->
io:format("Received data: ~w~n", [Data]),
L = string:tokens(Data, " "),
io:format("L: ~p~n", [L]),
Fun = fun string_to_num/1,
lists:foreach(Fun, L),
io:format("Data: ~p~n", [Data]);
{'EXIT', Port, Status} ->
io:format("EXIT: ~p ~p~n", [Port, Status]),
exit(port_terminated);
{Port, {exit_status, Status}} ->
io:format("Normal: ~p ~p~n", [Port, Status]),
exit(normal_process_exit);
Other ->
io:format("Unexpected data: ~p~n", [Other]),
exit(got_some_unexpected_message)
end,
loop(Port).
p() ->
io:format("Starting~n", []),
Cmd = "./comd -x 123.0 -y 234.0 -z 345.0", % don't need newline
process_flag(trap_exit, true),
Port = open_port({spawn,Cmd}, [use_stdio, exit_status]),
Payload = list_to_binary("1.0 2.0 3.0\n") ,
io:format("Opened the port: ~w~n", [Port]),
erlang:port_command(Port, Payload),
io:format("Sent command to port: ~p~n", [Payload]),
loop(Port).