-
Notifications
You must be signed in to change notification settings - Fork 10
/
serial.erl
85 lines (74 loc) · 2.1 KB
/
serial.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
% -*- Erlang -*-
% File: serial.erl (~jb/serialport/serial.erl)
% Author: Johan Bevemyr
% Created: Tue Oct 22 14:07:24 1996
% Purpose:
-module(serial).
-author('[email protected]').
-export([start/0,start/1,init/1,loop/2]).
-include("serial.hrl").
start() ->
start([]).
start(Options) ->
Pid = spawn_link(serial, init, [self()]),
process_options(Pid,Options),
Pid.
process_options(Pid,[]) -> done;
process_options(Pid,[Opt|Opts]) ->
Pid ! Opt,
process_options(Pid,Opts).
init(Pid) ->
process_flag(trap_exit,true),
Port = open_port({spawn,"./serial -erlang"},[binary,{packet,2}]),
loop(Pid,Port).
loop(Pid,Port) ->
receive
{Port, {data, Bytes}} ->
Pid ! {data, Bytes},
serial:loop(Pid,Port);
{send, Bytes} ->
send_serial(Port,[?SEND,Bytes]),
serial:loop(Pid,Port);
{connect} ->
send_serial(Port,[?CONNECT]),
serial:loop(Pid,Port);
{disconnect} ->
send_serial(Port,[?DISCONNECT]),
serial:loop(Pid,Port);
{open, TTY} ->
send_serial(Port,[?OPEN,TTY]),
serial:loop(Pid,Port);
{close} ->
send_serial(Port,[?CLOSE]),
serial:loop(Pid,Port);
{speed, NewInSpeed, NewOutSpeed} ->
send_serial(Port,[?SPEED,integer_to_list(NewInSpeed)," ",
integer_to_list(NewOutSpeed),0]),
serial:loop(Pid,Port);
{speed, NewSpeed} ->
send_serial(Port,[?SPEED,integer_to_list(NewSpeed)," ",
integer_to_list(NewSpeed),0]),
serial:loop(Pid,Port);
{parity_odd} ->
send_serial(Port,[?PARITY_ODD]),
serial:loop(Pid,Port);
{parity_even} ->
send_serial(Port,[?PARITY_EVEN]),
serial:loop(Pid,Port);
{break} ->
send_serial(Port,[?BREAK]),
serial:loop(Pid,Port);
stop ->
stopped;
{'EXIT', Port, Why} ->
io:format("Port exited with reason ~w~n", [Why]),
exit(Why);
{'EXIT', Linked, Why} ->
io:format("Linked ~w exited with reason ~w~n", [Linked,Why]),
exit(Why);
OtherError ->
io:format("Received unknown message ~w~n",[OtherError]),
serial:loop(Pid,Port)
end.
send_serial(Port,Message) ->
Port ! {self(),{command,Message}}.