I don’t know your exact requirements, but when working with Nerves.UART
i usually do something along the following:
defmodule Plotter.UARTHandler do
use GenServer
alias Plotter.UARTHandler
defstruct [:uart]
def start_link(args, opts), do: GenServer.start_link(__MODULE__, args, opts)
def interact_with_device(plotter, timeout), do: GenServer.call(plotter, {:interact, timeout}, timeout + 100)
def init(_args) do
{:ok, uart} = Nerves.UART.start_link()
:ok = Nerves.UART.open(uart, "ttyAMA0", 9600, active: false)
{:ok, struct(UARTHandler, uart: uart)}
end
def handle_call({:interact, timeout}, _from, state) do
:ok = Nerves.UART.write(state.uart, "SOME FORMATTED BINARY")
case Nerves.UART.read(state.uart, timeout) do
{:ok, data} -> {:reply, {:ok, data}, state}
{:error, reason} -> {:reply, {:error, reason}, state}
end
end
end
since it seems like you are using Phoenix, you will then likely want to do something like this in your application.ex
file.
defmodule Plotter.Application do
use Application
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec
# Define workers and child supervisors to be supervised
children = [
# Start the endpoint when the application starts
supervisor(PlotterWeb.Endpoint, []),
# Start your own worker by calling: Plotter.Worker.start_link(arg1, arg2, arg3)
# worker(Plotter.Worker, [arg1, arg2, arg3]),
{Plotter.UARTHandler, [[], name: GlobalHandler]}, # <- this is the important change.
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Plotter.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
PlotterWeb.Endpoint.config_change(changed, removed)
:ok
end
end
then you can make a controller like:
defmodule PlotterWeb.PlotterController do
use PlotterWeb, :controller
def plot(conn, _, ) do
case Plotter.UARTHandler.interact_with_device(GlobalHandler, 2000) do
{:ok, data} -> send_resp(conn, 200, data)
{:error, reason} -> send_resp(conn, 501, "an error occured: #{inspect reason}")
end
end
end