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

时间:2015-07-06 10:29:17

标签: 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 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();
            }  
        }
    }

}

1 个答案:

答案 0 :(得分: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();
            }

        }


    }