2013-03-11 74 views
0

我寫了「GPS藍牙接收器」的一些代碼它向我發送了繼續的數據,我收到它,但我無法在我的text-box中顯示。返回語句在while循環中丟失

建議我如何改進我的代碼,以便在'while循環'未結束時繼續傳輸數據。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Enumeration; 
import java.util.TooManyListenersException; 

public class communication { 
    public static CommPortIdentifier portId = null; 

    public String com() throws UnsupportedCommOperationException, IOException, 
     TooManyListenersException 
    { 
    int c = 1; 

    String wantedPortName = "COM16"; 

    Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); 

    while (portIdentifiers.hasMoreElements()) { 
     CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); 
     if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL 
      && pid.getName().equals(wantedPortName)) { 
     portId = pid; 
     break; 
     } 
    } 
    if (portId == null) { 
     System.err.println("Could not find serial port " + wantedPortName); 
     System.exit(1); 
    } else { 
     System.out.println("system find gps reciever"); 
    } 
    SerialPort port = null; 
    try { 
     port = (SerialPort) portId.open("RMC", 1); 
     System.out.println("all are ok"); 
    } catch (PortInUseException e) { 
     System.err.println("Port already in use: " + e); 
     System.exit(1); 
    } 
    // int i=Integer.parseInt(new vps().baud_rate.getItemAt(new vps().baud_rate.getItemCount())); 
    port.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, 
     SerialPort.PARITY_NONE); 

    BufferedReader is = null; 

    try { 
     is = new BufferedReader(new InputStreamReader(port.getInputStream())); 
     System.out.println("data is ok"); 
    } catch (IOException e) { 
     System.err.println("Can't open input stream: write-only"); 
     is = null; 
    } 

    while (new test().bp)// this is variable is ouside of class and define by public it work 
    { 

     String st = is.readLine(); 

     // System.out.println(st); 
     // st = st.replace(st, ""); 

     return st; 

    } 
    if (is != null) 
     is.close(); 

    if (port != null) 
     port.close(); 

    } 
} 

測試類代碼...

import java.io.IOException; 
import java.util.TooManyListenersException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.comm.UnsupportedCommOperationException; 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/* 
* test.java 
* 
* Created on Mar 11, 2013, 9:08:52 AM 
*/ 
/** 
* 
* @author DJ ROCKS 
*/ 
public class test extends javax.swing.JFrame { 
    public boolean bp=false; 

    /** Creates new form test */ 
    public test() { 
     initComponents(); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     ok = new javax.swing.JButton(); 
     a = new javax.swing.JTextField(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     ok.setText("ok"); 
     ok.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       okActionPerformed(evt); 
      } 
     }); 

     a.setText(" "); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(62, 62, 62) 
         .addComponent(a, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(238, 238, 238) 
         .addComponent(ok))) 
       .addContainerGap(124, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(a, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(67, 67, 67) 
       .addComponent(ok) 
       .addContainerGap(160, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void okActionPerformed(java.awt.event.ActionEvent evt) { 
     // TODO add your handling code here: 
     bp=true; 
     if(evt.getSource()==ok) 
     { 
      try { 
       a.setText(new communication().com()); 
      } catch (UnsupportedCommOperationException ex) { 
       Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (IOException ex) { 
       Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (TooManyListenersException ex) { 
       Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new test().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JTextField a; 
    private javax.swing.JButton ok; 
    // End of variables declaration 
} 
當我運行這段代碼編譯器顯示錯誤等....

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - missing return statement 
at communication.com(communication.java:14) 
at test.okActionPerformed(test.java:86) 
at test.access$000(test.java:22) 
at test$1.actionPerformed(test.java:47) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
at java.awt.Component.processMouseEvent(Component.java:6505) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
at java.awt.Component.processEvent(Component.java:6270) 
at java.awt.Container.processEvent(Container.java:2229) 
at java.awt.Component.dispatchEventImpl(Component.java:4861) 
at java.awt.Container.dispatchEventImpl(Container.java:2287) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
at java.awt.Container.dispatchEventImpl(Container.java:2273) 
at java.awt.Window.dispatchEventImpl(Window.java:2719) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:682) 
at java.awt.EventQueue$3.run(EventQueue.java:680) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
at java.awt.EventQueue$4.run(EventQueue.java:696) 
at java.awt.EventQueue$4.run(EventQueue.java:694) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 

回答

2

這是你的函數的末尾:

while(new test().bp)//this is variable is ouside of class and define by public it work 
{ 

    st = is.readLine(); 
    return st; 
} 
if (is != null) is.close(); 
if (port != null) port.close(); 

如果bp爲false,則退出沒有ret的while循環urning。在這種情況下,您關閉了is和port,然後...沒有返回語句。所以如果發生這種情況,編譯器不知道它應該返回什麼。順便說一句,那個循環邏輯沒有意義,因爲它是一個榮耀的「if」 - 如果你返回,你不可能再次循環。

+0

所以我怎麼能繼續循環? – dharma11 2013-03-12 16:47:53