Skip to content

jjcarstens/tty0tty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TTY0TTY

REUSE status

Elixir port for tty0tty null modem emulator

Use

This library is almost exclusively for testing purposes. tty0tty creates 2 pseudo-ttys that are connected together allowing you to run unit tests which use serial ports (like Circuits.UART) without the need for external hardware of adapters.

defmodule SerialTest do
  use ExUnit.Case, async: true

  test "can open a serial port" do
    {:ok, uart} = Circuits.UART.start_link()

    port_name = "/tmp/dummy1"

    # Open serial port before use
    {:ok, _port_sup} = TTY0TTY.open(port_name)

    assert :ok = Cicuits.UART.open(uart, port_name)
  end
end

Under the hood, TTY0TTY.open/2 opens 2 devices (<port_name>, and <port_name>-twin) and connects their TX <-> RX to emulate the serial connection. This allows you to also verifying reading serial data by sending data to the connected twin port:

defmodule SerialTest do
  use ExUnit.Case, async: true

  test "can read a serial port" do
    {:ok, uart} = Circuits.UART.start_link()

    port_name = "/tmp/dummy1"

    # Open serial port before use
    {:ok, _port_sup} = TTY0TTY.open(port_name)

    assert :ok = Cicuits.UART.open(uart, port_name)

    File.write!([port_name, "-twin"], "howdy!")

    assert_receive {:circuits_uart, ^port_name, "howdy!"}
  end
end