2015-07-06 56 views
1

我使用這段代碼從arduino獲取數據並將其寫入文本文件。在我寫入文件後,我想關閉端口,但是我不能這樣做,即使我將commPort傳遞給serialreader類,以便它可以在使用數據後關閉它。獲取數據後關閉串口,使用RXTX的java

我使用此函數關閉端口,但Java不接受port.close();。這裏是我的代碼:

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 
import sun.misc.IOUtils; 
import java.io.FileDescriptor; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.io.StringWriter; 

public class TwoWaySerialComm 
{ 
    public TwoWaySerialComm() 
    { 
     super(); 
    } 

    void connect (String portName) throws Exception 
    { 
     CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
     if (portIdentifier.isCurrentlyOwned()) 
     { 
      System.out.println("Error: Port is currently in use"); 
     } 
     else 
     { 
      CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); 

      if (commPort instanceof SerialPort) 
      { 
       SerialPort serialPort = (SerialPort) commPort; 
       serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 

       InputStream in = serialPort.getInputStream(); 
       (new Thread(new SerialReader(in, commPort))).start(); 
      } 
      else 
      { 
       System.out.println("Error: Only serial ports are handled by this example."); 
      } 
     } 
    } 

    public static class SerialReader implements Runnable 
    { 
     InputStream in; 
     CommPort port; 

     public SerialReader (InputStream in, CommPort port) 
     { 
      this.in = in; 
     } 

     public void run() 
     { 
      byte[] buffer = new byte[1024]; 
      int len = -1; 
      try 
      { 
       String txt = ""; 
       long startTime = System.currentTimeMillis(); //fetch starting time 
       while((len = this.in.read(buffer)) > -1 & (System.currentTimeMillis()-startTime)<6000) 
       { 
        System.out.print(new String(buffer,0,len)); 
        txt = txt + (new String(buffer,0,len)); 
       } 
       PrintWriter writer = new PrintWriter("Patient" + startTime + ".txt", "UTF-8"); 
       writer.println(txt); 
       writer.close(); 
       this.in.close(); 
       return; 
       port.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

回答

2

注:我解決問題,我改寫serialreader類用這種方式和現在一切都正常工作:

public static class SerialReader implements Runnable 
    { 
     InputStream in; 
     CommPort serialport; 

     public SerialReader (InputStream in, CommPort port) 
     { 
      this.in = in; 
      serialport = port; 

     } 

     public void run() 
     { 
      byte[] buffer = new byte[1024]; 
      int len = -1; 
      try 
      { 
       String txt = ""; 
       long startTime = System.currentTimeMillis(); //fetch starting time 
       while((len = this.in.read(buffer)) > -1 & (System.currentTimeMillis()-startTime)<6000) 
       { 
        System.out.print(new String(buffer,0,len)); 
        txt = txt + (new String(buffer,0,len)); 
       } 
       PrintWriter writer = new PrintWriter("Patient" + startTime + ".txt", "UTF-8"); 
       writer.println(txt); 
       writer.close(); 
       this.in.close(); 
       serialport.close(); 

      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 

     } 


    }