2017-05-31 69 views
1

我有一個openmodelica接口。如何從輸出塊連接到openmodelica中的輸入塊?

block InputInterfaceBlock 

    CPSModel.ConnectionObjects.SocketConnection con =  CPSModel.ConnectionObjects.SocketConnection("/pathToSocket/rpcSocket"); 

    Modelica.Blocks.Interfaces.RealOutput y annotation(
    Placement(visible = true, transformation(origin = {194, 2}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {106, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); 

algorithm 
    while true loop 
    y := CPSModel.Functions.readFromSocket(con); 
    print("Message from server : " + String(y) + "\n"); 
    end while; 

    annotation(
    __OpenModelica_simulationFlags(jacobian = "coloredNumerical", s = "dassl", lv = "LOG_STATS"), 
uses(Modelica(version = "3.2.2")), 
Icon(graphics = {Text(origin = {4, -1}, extent = {{-62, 73}, {62, -73}}, textString = "Input\nInterface", fontName =   "DejaVu Sans Mono Bold")})); 

    annotation(
    Placement(visible = true, transformation(origin = {-70, 70}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); 
end InputInterfaceBlock; 

我有一個接口塊(InputInterfaceBlock),其從在該路徑所限定的插座讀取。我想讓這個接口塊連接到另一個塊(OutputInterfaceBlock)。

block OutputInterfaceBlock 

    CPSModel.ConnectionObjects.SocketConnection con = CPSModel.ConnectionObjects.SocketConnection("pathToModel/rpcSocket"); 

    Modelica.Blocks.Interfaces.RealInput y annotation(
    Placement(visible = true, transformation(origin = {194, 2}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {106, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); 

algorithm 
    print("Trying to send : " + String(y) + "\n"); 
     CPSModel.Functions.writeToSocket(con, y); 
     print("Message send to server." + "\n"); 

annotation(
    __OpenModelica_simulationFlags(jacobian = "coloredNumerical", s = "dassl", lv = "LOG_STATS"), 
uses(Modelica(version = "3.2.2")), 
Icon(graphics = {Text(origin = {4, -1}, extent = {{-62, 73}, {62, -73}}, textString = "Output\nInterface", fontName = "DejaVu Sans Mono")})); 
    annotation(
    Placement(visible = true, transformation(origin = {-70, 70}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); 
end OutputInterfaceBlock; 

我的模型如下。

model MechatronicSystem 

    CPSModel.Models.InputInterfaceBlock Input annotation(
     Placement(visible = true, transformation(origin = {-90, 8}, extent = {{-28, -28}, {28, 28}}, rotation = 0))); 

    CPSModel.Models.OutputInterfaceBlock Output annotation(
     Placement(visible = true, transformation(origin = {72, 12}, extent = {{28, 28}, {-28, -28}}, rotation = 0))); 

equation 
    connect(Input.y, Output.y) annotation(
    Line(points = {{-60, 8}, {44, 8}, {44, 12}, {42, 12}}, color = {0, 0, 127})); 
annotation(
     uses(Modelica(version = "3.2.2"))); 
end MechatronicSystem; 

我可以從插座到模型得到的InputInterfaceBlock的數據,但是當我嘗試將數據發送到OutputInterfaceBlock。它沒有收到OutputInterfaceBlock

我該如何解決?

回答

3

您在InputInterfaceBlock中使用了while true循環,但Modelica中的不同算法不是協同例程,而是常規算法。

您可以用when sample(0.1,0.1) then ... end when;或類似的方法替換它,它會每隔0.1秒運行一次代碼。

while循環導致模型應該卡在InputInterfaceBlock和OutputInterfaceBlock中不被調用。