samuelventura / baud

Elixir Serial Port with Modbus RTU support
MIT License
15 stars 5 forks source link

Make `readall` function blocking #5

Open pallix opened 6 years ago

pallix commented 6 years ago

Is there anyway to make the readall blocking? It returns "" when no data are available.

samuelventura commented 6 years ago

Short answer is no. Long answer is baud was written with a master/slave request/response use case in mind were not having a response after certain time is a failure.

Can you share what you are trying to accomplish to see if I can recommend a workaround?

pallix commented 6 years ago

I am reading packets coming from a serial line. Maybe I can call readn multiple times. But it will timeout if length(packet) < count.

samuelventura commented 6 years ago

A blocking readall wouldn't know when to return. readall should be understood as read all that is available.

samuelventura commented 6 years ago

What are you using as a finish mark? Can you anticipate the length of the expected packet? Do your packets end with an specific byte?

pallix commented 6 years ago
A blocking readall wouldn't know when to return. readall should be understood as read all that is available.

Could it not return only when data are available?

I am using a special byte for the ending (with escaping if necessary).

samuelventura commented 6 years ago

Added a sample to show how to wait indefinitely for some data. Hope that helps.

pallix commented 6 years ago

Thanks! That may put some load on the CPU or reduce performance. Somehow nerves-uart manages to do a blocking call. If I understand correctly they designed their library differently / for other use cases?

samuelventura commented 6 years ago

nerves-uart is a more complete multithreading Erlang Port and Baud is a NIF wrapper around the basic serial port operations. Blocking and packeting in Baud need to be emulated in Elixir code. With proper sleeping, polling hardly adds any CPU load at low speeds.