获取数据后关闭串行端口,java使用RXTX

获取数据后关闭串行端口,java使用RXTX,java,function,rxtx,Java,Function,Rxtx,我使用此代码从arduino获取数据并将其写入文本文件。在写入文件后,我想关闭端口,但我不能这样做,即使我将commPort传递给serialreader类,以便它在使用数据后可以关闭它 我使用此函数关闭端口,但Java不接受port.close()。这是我的密码: import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import sun.misc.IOUtils; import

我使用此代码从arduino获取数据并将其写入文本文件。在写入文件后,我想关闭端口,但我不能这样做,即使我将commPort传递给serialreader类,以便它在使用数据后可以关闭它

我使用此函数关闭端口,但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();
            }  
        }
    }

}
导入gnu.io.CommPort;
输入gnu.io.CommPortIdentifier;
导入gnu.io.SerialPort;
导入sun.misc.IOUtils;
导入java.io.FileDescriptor;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.io.PrintWriter;
导入java.io.StringWriter;
公共类双向串行通信
{
公共双向串行通信()
{
超级();
}
void connect(字符串portName)引发异常
{
CommPortIdentifier portIdentifier=CommPortIdentifier.getPortIdentifier(端口名);
if(portIdentifier.isCurrentlyOwned())
{
System.out.println(“错误:端口当前正在使用”);
}
其他的
{
CommPort CommPort=portIdentifier.open(this.getClass().getName(),2000);
if(串行端口的commPort实例)
{
SerialPort SerialPort=(SerialPort)commPort;
serialPort.setSerialPortParams(9600,serialPort.DATABITS_8,serialPort.STOPBITS_1,serialPort.PARITY_NONE);
InputStream in=serialPort.getInputStream();
(新线程(新SerialReader(in,commPort))).start();
}
其他的
{
System.out.println(“错误:本例只处理串行端口。”);
}
} 
}
公共静态类SerialReader实现可运行
{
输入流输入;
通讯端口;
公共串行读取器(InputStream-in、CommPort端口)
{
this.in=in;
}
公开作废运行()
{
字节[]缓冲区=新字节[1024];
int len=-1;
尝试
{
字符串txt=“”;
long startTime=System.currentTimeMillis();//获取开始时间

虽然((len=this.in.read(buffer))>-1&(System.currentTimeMillis()-startTime)注意:我解决了这个问题,我以这种方式重写了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();
            }

        }


    }
公共静态类SerialReader实现可运行
{
输入流输入;
串口通信;
公共串行读取器(InputStream-in、CommPort端口)
{
this.in=in;
串行端口=端口;
}
公开作废运行()
{
字节[]缓冲区=新字节[1024];
int len=-1;
尝试
{
字符串txt=“”;
long startTime=System.currentTimeMillis();//获取开始时间
而((len=this.in.read(buffer))>-1&(System.currentTimeMillis()-startTime)