gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:21:23 24 4
gpt4 key购买 nike

我使用此代码从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();
}
}
}

}

最佳答案

注意:我解决了问题,我用这种方式重写了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();
}

}


}

关于java - 获取数据后关闭串口,java使用RXTX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31243525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com