bmitc

bmitc

Using Circuits.UART, how do you read a specific amount of bytes?

For Circuits.UART, I am looking for a way to read a specific amount of bytes where the number of bytes to read may vary from call to call. This is a common use case needed for when certain messages return a specific amount of bytes in the response but the number of bytes in the response may differ from message to message.

For example, I expected to be able to do something like this:

iex> {:ok, pid} = Circuits.UART.start_link
iex> Circuits.UART.open(pid, "COM2", speed: 115200, active: false)
iex> Circuits.UART.write(pid, "some message expecting 3 bytes in response\r\n")
iex> {:ok, three_byte_response} = Circuits.UART.read(pid, 3, 3000)
iex> Circuits.UART.write(pid, "some message expecting 5 bytes in response\r\n")
iex> {:ok, five_byte_response} = Circuits.UART.read(pid, 5, 3000)

This isn’t possible right now because the read/2 of course doesn’t have a number of bytes to read parameter. I originally expected there to be something like read/3 which would accept a specific number of bytes to read.

It seems a possible alternative is to create a framer behaviour, but that has two downsides: (1) the data isn’t actually framed, (2) this would require constantly calling configure like Circuits.UART.configure(pid, number_of_bytes: rx_framing_timeout: 500) before every write and read pair. I’m not entirely sure of the consequences of doing (2), but it’s the only possible way I could see doing this with the framing feature (if it would even work).

I think the Circuits.UART module should have a read/3 function like read(pid, bytes_to_read, timeout). This is quite common in the serial communication world:

  1. LabVIEW VISA Read
  2. .NET SerialPort.Read
  3. pySerial

So I’m curious, how have people handled this with Circuits.UART?

I asked this question a while back on the Circuits GitHub repo, but got a non-answer.

First Post!

Sebb

Sebb

I do not use circuits, but from the docs it seems, that this is not supported. So you either have to change the other side to a framed protocol or change circuits.uart or write your own - which is not too hard if you do not aim to be as comfortable as circuits.uart.

Open a Port — Elixir v1.12.3 and just take the bytes from the uart and write them to stdout (and vice versa).

I did this for a project of mine and it seems to work but its not battle tested. If you feel up to it I could make the code public for you to get it.

Note: I use a framed protocol, so you’d have to add the functionality you need. But my uart-adapter has just 200loc and should be easy to handle.

To give an idea how this works:

...

static void
forward(int const from, int const to, int const err_code)
{
  static char   buf[BUFSIZ];
  ssize_t const size = read(from, buf, BUFSIZ);
  require(size > 0, err_code + ERR_READ_ERROR_);
  ssize_t const write_size = write(to, buf, size);
  require(write_size == size, err_code + ERR_WRITE_ERROR_);
}

int
main(int argc, char* argv[])
{
  f_uart = config_uart(argc, argv);

  int const maxfd = f_uart + 1;
  fd_set    fds;

  for (;;) {
    FD_ZERO(&fds);
    FD_SET(f_uart, &fds);
    FD_SET(STDIN_FILENO, &fds);

    select(maxfd, &fds, NULL, NULL, NULL);

    if (FD_ISSET(f_uart, &fds)) {
      forward(f_uart, STDOUT_FILENO, ERR_FWD_STDOUT_FAILED_);
    }
    if (FD_ISSET(STDIN_FILENO, &fds)) {
      forward(STDIN_FILENO, f_uart, ERR_FWD_STDIN_FAILED_);
    }
  }

and on the Elixir side:

 def handle_info({_port, {:data, data}}, state = %{subscriber: subscriber}) do

    if subscriber do
      send(subscriber, {:data, data})
    end

    {:noreply, state}
  end

...

  @impl true
  def handle_cast({:write, data}, %{port: port} = state) do
    Port.command(port, data)
    {:noreply, state}
  end

...

Last Post!

bmitc

bmitc

Thank you for the response! I apologize for never responding, although I did read it at the time.

I haven’t been working on this project but may start it up again sometime in the new year. I will probably navigate away from Elixir Circuits, at least Ciruits.Uart since it doesn’t seem to be broadly supported for broad use cases.

In this case, as in most use cases of RS-232 and such communication, I have no control over the device’s serial protocol. One often wants to communicate with an external device that is delivered by someone else. I am considering writing my own Port library, likely using .NET’s cross-platform serial library as a starter.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement