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__, %{})

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

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

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

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 that might also be helpful.


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