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:
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.