Created
December 18, 2023 23:04
-
-
Save zenom/3d339d62a0964941dbaf250b20274f81 to your computer and use it in GitHub Desktop.
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 UartListener do | |
use GenServer | |
@serial_port "/dev/cu.usbserial-2210" # e.g., "/dev/ttyUSB0" | |
@baud_rate 115200 | |
# Client | |
def start_link(args) do | |
GenServer.start_link(__MODULE__, args, name: __MODULE__) | |
end | |
def send_data(pid, data) do | |
GenServer.call(pid, {:send_data, data}) | |
end | |
# Server | |
# def init(%{port: port, speed: baud_rate} = state) do | |
def init(%{} = state) do | |
{:ok, pid} = Circuits.UART.start_link() do | |
case Circuits.UART.open(pid, @serial_port, speed: @baud_rate, active: true) do | |
:ok -> {:ok, Map.put(state, :pid, pid) } | |
{:error, reason} -> {:stop, reason} | |
end | |
end | |
end | |
def handle_info({:circuits_uart, _port, data}, state) do | |
IO.inspect(data, label: "Data") | |
IO.inspect(state, label: "State") | |
# testing = :erlang.binary_to_list(data) | |
# |> Enum.map(fn x -> Integer.to_string(x, 16) end) | |
# IO.inspect(testing, label: "Parsed") | |
{:noreply, state} | |
end | |
# def terminate(_reason, %{uart: uart}) do | |
# Circuits.UART.close(uart) | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment