Search code examples
javafunctionrxtx

Closing the serial port after get data, java using RXTX


I use this code for getting data from arduino and write it on text file. After I write in the file, I wanted to close the port but I can not do this, even when I pass the commPort to the serialreader class so it can close it after using the data.

I use this function to close the port but Java doesn't accept port.close();. Here is my code:

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

}

Solution

  • Note: I fix the Problem, I rewrite the serialreader class in this way and everything work fine now:

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