NervesTip: `:tty0tty` null terminal emulator

Testing code destined for hardware can be tricky, but it just got one tiny bit easier!

:tty0tty was just released which is an Elixir port of the same C lib name. Why is that good? Well, it makes it easy to create a local serial port PTY that can be targeted for reads and writes. This effectively allows you to easily test code that may rely on serial ports (like Circuits.UART) without having to adjust your whole implementation!

Check out the documentation for more info!

6 Likes

This looks super useful - thank you so much :champagne: I had to try it out immediately. Here are my first impressions:

  • TTY0TTY.open("/tmp/some-device-name") creates two files (or TTYs or whatever they are :smile: ) on disk, “/tmp/some-device-name” and “/tmp/some-device-name-twin”. Bytes written to the first one will be received on the second one, and vice versa. This means they can be opened from another OS process on the system, so it’s not only useful for unit tests. Very useful when doing “exploratory testing” on a system that doesn’t have the hardware to open the serial ports. I love it.
  • On Linux I can use TTY0TTY.open to create the two TTYs, and open each of them with their own Circuits.UART process, and then I can use Circuits.UART.write to send data back and forth between the two processes. But this does not seem to work on MacOS. On MacOS I can use File.write to send data to a Circuits.UART process, but seemingly only in one direction. Do you know if this is something that could be made to work on MacOS eventually? I noticed the C code mentions Linux explicitly. I will be happy to provide more details if what I wrote is not clear.

Thank you for this :pray:

1 Like

Sorry, I just tested it again on MacOS, and now it seems to work the same as on Linux. I probably made a mistake due to my excitement :sweat_smile: The following script works the same on MacOS and Linux:

Mix.install([{:circuits_uart, "~> 1.5"}, {:tty0tty, "~> 1.0"}])
TTY0TTY.open("/tmp/barbarfoopipe")
{:ok, s1} = Circuits.UART.start_link
{:ok, s2} = Circuits.UART.start_link
Circuits.UART.open(s1, "/tmp/barbarfoopipe")
Circuits.UART.open(s2, "/tmp/barbarfoopipe-twin")
Circuits.UART.write(s1, "babi")
Circuits.UART.write(s2, "bubi")
3 Likes