gpt4 book ai didi

java - 我如何关闭 RXTX 中的 CommPort

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:42 26 4
gpt4 key购买 nike

我正在尝试使用此关闭 Comm 端口:

public synchronized void closeComportButtonActionPerformed(
java.awt.event.ActionEvent evt) {

try {
twoWaySerCom.disconnect();
} catch (Exception e) {

e.printStackTrace();
}
}

我的脱节是这样的:

 void disconnect() throws Exception {
if (serialPort != null) {
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
try {

// close the i/o streams.
in.close();
out.close();
} catch (IOException ex) {
// don't care
}
// Close the port.
serialPort.close();
}

当我按下关闭按钮时,我的应用程序只是卡住,最终我在控制台中收到一条错误消息:

A fatal error has been detected by the Java Runtime Environment:

# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00949e69, pid=7568, tid=4724
#
# JRE version: Java(TM) SE Runtime Environment (8.0_25-b18) (build 1.8.0_25-b18)
# Java VM: Java HotSpot(TM) Client VM (25.25-b02 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [rxtxSerial.dll+0x9e69]

谁能解释一下我在这里做错了什么?谢谢

最佳答案

 if(portInUse!=null)
{
try{
portInUse.port = (SerialPort)portInUse.portId.open(this.getClass().getName(),TIME_OUT);
portInUse.port.setSerialPortParams(baudRate,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
comConn=this.COM_CONNECTED;
}

portInUse.input = new BufferedReader(
newInputStreamReader(portInUse.port.getInputStream())
);

portInUse.output = portInUse.port.getOutputStream();
//adding listeners to input and output streams
portInUse.port.addEventListener(this);
portInUse.port.notifyOnDataAvailable(true);
portInUse.port.notifyOnOutputEmpty(true);
currentPort=portInUse;
}

portInUse 的数据类型:

private class CustomPort
{
String portName;
CommPortIdentifier portId;
SerialPort port;
BufferedReader input;
OutputStream output;
Scanner inputName;
}
CustomPort currentPort=new CustomPort();

以及我使用的冲洗功能:

public void flush()
{
try {
this.currentPort.output.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我使用的关闭函数:

public void close(){
if(currentPort.port!=null){
currentPort.port.close(); //close serial port

currentPort.input = null; //close input and output streams
currentPort.output = null;
}
}

如何关闭端口:

 this.flush();
this.close();

反复测试了很多次,效果很好。

关于java - 我如何关闭 RXTX 中的 CommPort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28766941/

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