0

这里有两个文件。

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 的消息。我的代码有什么问题吗?我也尝试发回“完成”消息,但它仍然不起作用!

我怎样才能发回消息?如何将“From !finished”和“From ! {converted, ConvertedTempC}”更改为正确的?

4

1 回答 1

0

在转换中:

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

你只需要sensor(Temp, Temp_Node, Dis_Node)在接收块之后回忆,它应该没问题。

请注意,该消息finished在您的情况下是无用的,对转换函数的简单调用应该可以完成这项工作。

于 2015-03-12T12:56:51.573 回答