Simulate UART device on host pc

Hey everyone!

I just want to simulate an uart device on the host pc to test my code locally.
I’m using tty0tty to simulate the uart interface.
My code so far:

defmodule UartSimulator do
    use GenServer

    @path_tty0tty "/bin/tty0tty"   

    def start_link() do
    {:ok, pid} = GenServer.start_link(__MODULE__, %{})
  end

  def init(state) do
    tty0tty = Port.open({:spawn, path_tty0tty}, [:binary])
    port = File.open!("/dev/pts/2", [:write])

    {:ok, %{state | tty0tty: tty0tty, port: port}}
  end

  def handle_cast({:data_reporting_mode, mode, activity}, %{port: port} = state) do
    IO.binwrite(port, "hello world")
    {:noreply, state}
  end
end

Is this the way to go?
And how can I implement this module in my testing environment? I’m getting the error that the module is not available.

Hi @n0gg1n,

I use tty0tty to test circuits_uart on CI. I start it outside of Elixir, though, since it requires super user privileges to load its special Linux kernel module. After it’s loaded, you should notice new serial ports on your machine called tnt0, tnt1, etc. They’re connected in pairs. Whatever you send out tnt0 comes in tnt1 and vice versa.

I have some docs at https://github.com/elixir-circuits/circuits_uart#building-and-running-the-unit-tests that might also be helpful.

3 Likes

Hey @fhunleth,

thanks for your answer! I just wanted to temporary open an uart emulation port with the tty0tty pts program. Unfortunately it didn’t work out the way I wanted. I’m now using the kernel module and it works.
Thanks for your great work!

1 Like