arduino - Read serial input until control char indicates end of message -


i'm using knewter/erlang-serial elixir , try read json-string (e.g. {"temperature":20.40,"humidity":30.10} coming in arduino via serial input after receives control-signal:

defmodule myapp.serialinput   require logger     def start_link     serial_pid = :serial.start [{:open, "/dev/cu.usbmodem1431"}, {:speed, 115200}]      control_signal = "1"     :timer.send_interval 5000, serial_pid, {:send, control_signal}      wait_for_new_values serial_pid   end     defp wait_for_new_values(serial_pid)     receive       {:data, jsonstring} when is_binary(jsonstring) ->         logger.debug "received :data #{inspect jsonstring}"      wait_for_new_values serial_pid   end end 

my problem is, serial input split (sometimes passes through @ once):

[debug] received :data "{\"t" [debug] received :data "emperature\":19.00,\"humidity\":42.00}\r\n"  [debug] received :data "{\"temperature\":19.60,\"humidity\":41" [debug] received :data ".00}\r\n"  [debug] received :data "{\"temperature\":19.50,\"humidity\":40.90}\r\n"  [debug] received :data "{\"temperature\":19.50,\"humi" [debug] received :data "dity\":40.90}\r\n"  [debug] received :data "{\"temperat" [debug] received :data "ure\":19.50,\"humidity\":41.30}\r\n" 

is there way tell receive block to wait until \r\n appears @ end of string or fixed amount of characters or that? if not, what's best way hold state outside of wait_for_new_values until \r\n appears concat these?

the receive block pretty low level function, not able such specialized thing.

state in elixir explicit, , can implemented through optional argument, accumulator (acc in case). can later pass down accumulated value recursive call , print when ends "\r\n".

defp wait_for_new_values(serial_pid, acc \\ "")   receive     {:data, chunk} when is_binary(chunk) ->       acc = acc + chunk   end    if string.ends_with? acc, "\r\n"     logger.debug "received :data #{inspect acc}"     acc = ""   end    wait_for_new_values serial_pid, acc end 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -