【发布时间】:2016-01-23 20:12:25
【问题描述】:
我创建了工作工作者,它在 :elixir_serial 处理程序中从 Arduino 获取消息,但现在我想在 Channel 中使用它来广播接收到的数据,我可以将套接字注入到 :elixir_serial handle_info() 吗?
defmodule MyApp.Serialport do
require Logger
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, [])
end
def init([]) do
work()
{:ok, []}
end
defp work do
{:ok, serial} = Serial.start_link
Serial.open(serial, "/dev/tty.arduino")
Serial.set_speed(serial, 9600)
Serial.connect(serial)
Logger.debug "pid #{inspect serial}"
end
def handle_info({:elixir_serial, serial, data}, state) do
Logger.debug "received :data #{inspect data}"
{:noreply, state}
end
end
您对如何改进工作代码有任何建议,例如。 Gen_Server 是必要的吗?
【问题讨论】: