Erlang 无法从它开始的地方接收消息

Erlang can not receive message from where it begins

这里有两个文件。

temp_converter() ->
receive
    {convertToCelsius, {From, TempF}} ->
        io:format("Temp-Server2 > Converter received message from ~w~n", [From]),
        ConvertedTempC = (TempF-32)*5/9,
        From ! finished,
        From ! {converted, ConvertedTempC},
        temp_converter();
    {From, {convertToCelsius, TempF}} ->
        io:format("Temp-Server > Converter received message from ~w~n", [From]),
        ConvertedTempC = (TempF-32)*5/9,
        From ! {converted, ConvertedTempC},
        temp_converter()
end.

另一个是:

sensor(Temp, Temp_Node, Dis_Node) ->
receive
    finished ->
        io:foramt("finished");
    % Receive the clock_tick from clock controller
    clock_tick ->
        io:format("Sensor_f received tick~n", []),
        {temp_converter, Temp_Node} ! {convertToCelsius, {self(), Temp}};
    % Receive the temperature which has been converted
    {converted, ConvertedTemp} ->
        io:format("Sensor_f received converted temperature~n", []),
        {display_module, Dis_Node} ! {inCelsius, ConvertedTemp};
    {'EXIT', From, Reason} ->
        io:foramt("Temperature Server down!~nGot ~p~n", [{'EXIT', From, Reason}])
end.

基本上,传感器会发送消息到temp_converter,这是在"clock_tick ->"中实现的。当 temp_converter 收到消息时,它会输出一些东西并发回消息。这就是问题所在。 它确实输出了一些东西,但传感器无法接收来自 temp_converter 的消息。我的代码有什么问题吗?我也试过回"finished"消息,还是不行!!

如何发回消息?如何更改 "From ! finished" 和 "From ! {converted, ConvertedTempC}," 以使其正确?

转换中:

{convertToCelsius, {From, TempF}} ->
    io:format("Temp-Server2 > Converter received message from ~w~n", [From]),
    ConvertedTempC = (TempF-32)*5/9,
    From ! finished, %% Here you send to sensor the message 'finished'
    From ! {converted, ConvertedTempC}, %% the conversion will be received                                    %% after the first message (guaranteed by Erlang VM)
    temp_converter();

在传感器中:

finished ->
    io:foramt("finished"); %% when you receive the first message, you print it
                           %% and leave the function. The next message will be lost
                           %% because you do not loop in the sensor function.

你只需要在receive bloc之后回忆一下sensor(Temp, Temp_Node, Dis_Node)就可以了。

请注意消息finished对您的情况毫无用处,只需调用一个转换函数即可。