2017-05-31 96 views
0

使用來自stockoverflow的代碼示例我做了一個類來接收TCP/IP上的數據。 下面的分類代碼。它工作正常,我確實收到來自其他PC的數據。 recPositions地圖構建正確。傳遞結束後我可以顯示我使用getRecPositions收到構建簡單的HMI畫面中的所有數據()在類和正在運行的線程之間傳遞數據

此線程在其他類開始。 線程運行並顯示從其他PC接收到的數據。 問題是我想訪問MainWindow類(HMI) 中的相同數據,但它總是顯示沒有收到任何內容。 類主窗口下方(不是全部,因爲有大量無用的按鈕等) 在最底部我有應顯示多少位置被記錄(在地圖元素)按鈕,但它始終顯示爲0。 代碼段從按鈕更新。

System.out.println(dataServer.getRecPositions().size()) 

所以在某些時候我做錯了什麼。 線程啓動並運行,我可以看到傳入的位置。在我收到「EOT」後的某個時間點,我收到存儲在Map中的位置。但是當我試圖在主窗口中顯示Map的大小時,它始終顯示0.

/* 
* Simple data server for receiving string data from IIWA robot 
*/ 
public class SimpleDataServer implements Runnable { 

    Map<Integer, String> recPositions = new HashMap<>(); 
    Server1Connection oneconnection; 
    ServerSocket echoServer = null; 
    Socket clientSocket = null; 
    int port; 
    boolean reset = false; 

    public SimpleDataServer(int port) { 
     this.port = port; 
    } 

    public void stopServer() { 
     System.out.println("Simple data server stopped"); 
    } 

    public void startServer() { 
     // Try to open a server socket on the given port 
     // Note that we can't choose a port less than 1024 if we are not 
     // privileged users (root) 

     try { 
      echoServer = new ServerSocket(port); 
     } 
     catch (IOException e) { 
      System.out.println(e); 
     } 

     System.out.println("Waiting for connections. Only one connection is allowed."); 

     // Create a socket object from the ServerSocket to listen and accept connections. 
     // Use Server1Connection to process the connection. 

     while (true) { 
      try { 
       clientSocket = echoServer.accept(); 
       oneconnection = new Server1Connection(clientSocket, this); 
       oneconnection.run(); 
       if(isReset()) { 
        setReset(false); 
        System.out.println("Recording finished"); 
        System.out.println("DEBUG:" + this.getRecPositions().size() + " positions recorded: " + this.getRecPositions()); 
       } 
      } 
      catch (IOException e) { 
       System.out.println(e); 
      } 
     } 
    } 

    @Override 
    public void run() { 
     int port = this.port; 
     SimpleDataServer server = new SimpleDataServer(port); 
     server.startServer(); 
    } 

    public Map<Integer, String> getRecPositions() { 
     return recPositions; 
    } 

    public void setRecPositions(Map<Integer, String> recPositions) { 
     this.recPositions = recPositions; 
    } 

    public boolean isReset() { 
     return reset; 
    } 

    public void setReset(boolean reset) { 
     this.reset = reset; 
    } 

} 

class Server1Connection { 
    BufferedReader is; 
    PrintStream os; 
    Socket clientSocket; 
    SimpleDataServer server; 

    public Server1Connection(Socket clientSocket, SimpleDataServer server) { 
     this.clientSocket = clientSocket; 
     this.server = server; 
     System.out.println("Connection established with: " + clientSocket); 
     try { 
      is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
      os = new PrintStream(clientSocket.getOutputStream()); 
     } catch (IOException e) { 
      System.out.println(e); 
     } 
    } 

    public Map<Integer, String> getPositions() { 
     return server.getRecPositions(); 
    } 

    public void run() { 
     String line; //whole line recevied 
     String segment = null; //single segment from line using comma delimiter 
     List<String> stringsList; 
     try { 
      boolean serverStop = false; 
      //server runs here 
      while (true) 
      { 
       line = is.readLine(); 
       System.out.println("DEBUG Server Received: " + line); 
       //check is string is not empty 
       if (line.equals(null)) { 
        System.err.println("Empty String received"); 
        break; 
       }   
       stringsList = new ArrayList<String>(Arrays.asList(line.split(";"))); 
       if (!stringsList.isEmpty()) { 
        stringsList.set(0, stringsList.get(0).replaceAll("\\s+","")); 
        stringsList.set((stringsList.size()-1), stringsList.get(stringsList.size()-1).replaceAll("\\s+","")); 

        String lastSegment = stringsList.get((stringsList.size()-1)); 
        if (lastSegment.equals("ETX") || lastSegment.equals("EOT")) { 
         switch (stringsList.get(0)) { 
         case "MSG": 
          stringsList.remove(0); 
          stringsList.remove(stringsList.size()-1); 
          System.out.println("Message: " + stringsList.toString()); 
          break; 
         case "POS": 
//       for (String blah : stringsList) { 
//        System.out.println("DEBUG" + blah); 
//       } 
          iiwaPosfromString iiwaPos = new iiwaPosfromString(stringsList.get(1), stringsList.get(2)); 
          System.out.println("DEBUG Position number: " + iiwaPos.posNum + " ; " + iiwaPos.toString()); 
          if (iiwaPos.getPosNum() > 0) { 
           server.getRecPositions().put(iiwaPos.getPosNum(), iiwaPos.toString()); 
          } 
          break; 

         case "EOT": 
         case "EXT": 
          //ignore handled later 
          break; 
         default: 
          System.err.println("Ausgebombt!"); 
          break; 
         } 

        } 
        //ETX End Of Text - dump data to screen - close current connection 
        if(lastSegment.equals("ETX")) { 
         System.out.println("End of Text received"); 
         break; 
        } 
        //EOT End Of Transmission - shuts down server 
        if(lastSegment.equals("EOT")) { 
         System.out.println("End of Transmission received"); 
         serverStop = true; 
         server.setReset(true); 
         break; 
        } 
        System.err.println("No correct end string!"); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        break; 

       } else { 
        System.out.println("Empty String received"); 
        break; 
       } 
      } 

      System.out.println("Connection closed."); 
      is.close(); 
      os.close(); 
      clientSocket.close(); 

      if (serverStop) server.stopServer(); 
     } catch (IOException e) { 
      System.out.println(e); 
     } 
    } 


    public class iiwaPosfromString { 
     private double posX; 
     private double posY; 
     private double posZ; 
     private double posA; 
     private double posB; 
     private double posC; 
     private int posNum; 

     public iiwaPosfromString(String posNum, String posString) { 
      List <String>stringsList = new ArrayList<String>(Arrays.asList(posString.split(" "))); 
      for (int i = 0; i < stringsList.size(); i++) { 
       String newElement = stringsList.get(i); 
       newElement = newElement.replaceAll("[^\\d.]", ""); 
       stringsList.set(i, newElement); 
      } 
      this.setPosNum(Integer.parseInt(posNum)); 
      this.setPosX(Double.parseDouble(stringsList.get(1))); 
      this.setPosY(Double.parseDouble(stringsList.get(2))); 
      this.setPosZ(Double.parseDouble(stringsList.get(3))); 
      //this is stupid and don't do that 
      //from right to left, string to double, change radians to degrees, format to two decimals(string), string to double again 
      this.setPosA(Double.parseDouble(String.format("%.2f",(Math.toDegrees(Double.parseDouble(stringsList.get(4))))))); 
      this.setPosB(Double.parseDouble(String.format("%.2f",(Math.toDegrees(Double.parseDouble(stringsList.get(4))))))); 
      this.setPosC(Double.parseDouble(String.format("%.2f",(Math.toDegrees(Double.parseDouble(stringsList.get(4))))))); 
     } 

     public double getPosX() { 
      return posX; 
     } 

     public void setPosX(double posX) { 
      this.posX = posX; 
     } 

     public double getPosY() { 
      return posY; 
     } 

     public void setPosY(double posY) { 
      this.posY = posY; 
     } 

     public double getPosZ() { 
      return posZ; 
     } 

     public void setPosZ(double posZ) { 
      this.posZ = posZ; 
     } 

     public double getPosA() { 
      return posA; 
     } 

     public void setPosA(double posA) { 
      this.posA = posA; 
     } 

     public double getPosB() { 
      return posB; 
     } 

     public void setPosB(double posB) { 
      this.posB = posB; 
     } 

     public double getPosC() { 
      return posC; 
     } 

     public void setPosC(double posC) { 
      this.posC = posC; 
     } 
     @Override 
     public String toString() { 
      return "<" + 
        "X: " + getPosX() + ", " + 
        "Y: " + getPosY() + ", " + 
        "Z: " + getPosZ() + ", " + 
        "A: " + getPosA() + ", " + 
        "B: " + getPosB() + ", " + 
        "C: " + getPosC() + 
        ">"; 
     } 

     public int getPosNum() { 
      return posNum; 
     } 

     public void setPosNum(int posNum) { 
      this.posNum = posNum; 
     } 
    } 
} 


public class MainWindow { 

    private Thread dataServerThread; 
    private SimpleDataServer dataServer; 
    private XmlParserGlobalVarsRD globalVarPLC, globalVarKRC; 
    private JFrame frame; 
    private JTextField simpleWorkingDirStiffness; 
    private JTextField simpleWorkingDirAdditionalForce; 
    private JTextField simpleTravelDistance; 
    private JTextField simpleTravelVelocity; 
    private JTextField simpleTotalTime; 
    private JTextField simpleTheoreticalDepth; 
    private JTextField simpleZProgress; 
    private JComboBox oscillationMode; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        MainWindow window = new MainWindow(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public MainWindow() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 

     final JCheckBox emptyScanCycle = new JCheckBox("VRSI Scan Cycle Plain Fasteners"); 

     //set data server thread and start it 
     dataServer = new SimpleDataServer(30008); 
     dataServerThread = new Thread(dataServer); 
     dataServerThread.setDaemon(true); 
     dataServerThread.start(); 

... 

     JButton pbUpdate = new JButton("UPDATE"); 
     pbUpdate.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       System.out.println(dataServer.getRecPositions().size()) 
      } 
     }); 
     pbUpdate.setBounds(210, 176, 90, 28); 
     frame.getContentPane().add(pbUpdate); 

    } 

回答

0

我相信問題出在您的SimpleDataServer.run方法中。你是從內部的SimpleDataServer實例創建SimpleDataServer的一個單獨的實例。因此,所有的通信是發生在你的主窗口沒有直接引用的對象。我相信你的SimpleDataServer.run方法應該如下所示:

@Override 
public void run() { 
    this.startServer(); 
} 
+0

Thx for tip。我今晚會試一試。 – kr16

+0

謝謝先生。只是測試它,它按預期工作。走過去的代碼,並意識到什麼是錯的 - 你是正確的。 – kr16