Last active
August 29, 2015 14:22
-
-
Save mmmries/d39bc1cf3139e440a581 to your computer and use it in GitHub Desktop.
Using knewter/erlang-serial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(1)> {:ok, pid} = Ser.start("/dev/ttyAMA0", 115_200) | |
{:ok, #PID<0.97.0>} | |
iex(2)> send(pid, {:send, "ohai"}) | |
Received serial data: ohai | |
{:send, "ohai"} | |
iex(3)> send(pid, {:send, "ohai"}) | |
{:send, "ohai"} | |
Received serial data: ohai | |
iex(4)> send(pid, {:send, "ohai u GUYZ!"}) | |
{:send, "ohai u GUYZ!"} | |
Received serial data: ohai u G | |
Received serial data: UYZ! | |
iex(5)> send(pid, {:send, "ohai u GUYZ! WAT IN THE WORLD"}) | |
{:send, "ohai u GUYZ! WAT IN THE WORLD"} | |
Received serial data: ohai u G | |
Received serial data: UYZ! WAT | |
Received serial data: IN THE | |
Received serial data: WORLD | |
iex(6)> send(pid, {:send, "Jurassic World is going to be ASUM SOS"}) | |
{:send, "Jurassic World is going to be ASUM SOS"} | |
Received serial data: Jurassic | |
Received serial data: World i | |
Received serial data: s going | |
Received serial data: to be AS | |
Received serial data: UM SOS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Ser do | |
def start(device_string, baud_rate) do | |
pid = spawn(Ser, :init, [self(), device_string, baud_rate]) | |
{:ok, pid} | |
end | |
def init(initiator, device_string, baud_rate) do | |
device = :serial.start([speed: baud_rate, open: :erlang.bitstring_to_list(device_string)]) | |
loop(device) | |
end | |
defp loop(device) do | |
receive do | |
{:data, data} -> | |
IO.puts "Received serial data: #{data}" | |
loop(device) | |
{:send, data} -> | |
send(device, {:send, data}) | |
loop(device) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment