n0gg1n

n0gg1n

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.

Marked As Solved

fhunleth

fhunleth

Co-author of Nerves

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.

Also Liked

n0gg1n

n0gg1n

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!

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

We're in Beta

About us Mission Statement