11defmodule Serial do
2+ @ moduledoc """
3+ Serial communication through Elixir ports.
4+ """
5+
26 use GenServer
37
48 @ send 0
@@ -11,51 +15,82 @@ defmodule Serial do
1115 @ parity_even 7
1216 @ break 8
1317
14- def start_link ( pid ) do
15- GenServer . start_link ( __MODULE__ , pid )
18+ @ doc """
19+ Starts a serial port. The process invoking this function will receive
20+ messages in the form of `{:elixir_serial, pid, data}`.
21+ """
22+ def start_link ( opts \\ [ ] ) do
23+ GenServer . start_link ( __MODULE__ , self ( ) , opts )
1624 end
1725
26+ @ doc """
27+ Opens a connection to the given tty.
28+ """
1829 def open ( pid , tty ) do
1930 GenServer . call ( pid , { :open , tty } )
2031 end
2132
33+ @ doc """
34+ Close the currently open connection.
35+ """
2236 def close ( pid ) do
2337 GenServer . call ( pid , { :cmd , @ close } )
2438 end
2539
40+ @ doc """
41+ Reopens the connection to the current tty.
42+ """
2643 def connect ( pid ) do
2744 GenServer . call ( pid , { :cmd , @ connect } )
2845 end
2946
47+ @ doc """
48+ Disconnects from the current tty.
49+ """
3050 def disconnect ( pid ) do
3151 GenServer . call ( pid , { :cmd , @ disconnect } )
3252 end
3353
54+ @ doc """
55+ Sets the connection speed to the given value.
56+ """
3457 def set_speed ( pid , speed ) do
3558 set_speed ( pid , speed , speed )
3659 end
3760
61+ @ doc """
62+ Sets the input and output connection speed to the given values.
63+ """
3864 def set_speed ( pid , in_speed , out_speed ) do
3965 GenServer . call ( pid , { :speed , in_speed , out_speed } )
4066 end
4167
68+ @ doc """
69+ Sets the parity to either `:odd` and `:even`.
70+ """
4271 def set_parity ( pid , :odd ) do
4372 GenServer . call ( pid , { :cmd , @ parity_odd } )
4473 end
4574 def set_parity ( pid , :even ) do
4675 GenServer . call ( pid , { :cmd , @ parity_even } )
4776 end
4877
78+ @ doc """
79+ Sends a break to the open connection.
80+ """
4981 def break ( pid ) do
5082 GenServer . call ( pid , { :cmd , @ break } )
5183 end
5284
85+ @ doc """
86+ Sends data over the open connection.
87+ """
5388 def send_data ( pid , data ) do
5489 GenServer . call ( pid , { :send , data } )
5590 end
5691
5792 def init ( pid ) do
58- exec = :code . priv_dir ( :elixir_serial ) ++ '/serial'
93+ exec = :code . priv_dir ( :serial ) ++ '/serial'
5994 port = Port . open ( { :spawn_executable , exec } , [ { :args , [ '-erlang' ] } , :binary , { :packet , 2 } ] )
6095 { :ok , { pid , port } }
6196 end
0 commit comments