本文介绍了打开和关闭串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试连接到 ...但是一旦我打开串口。我无法再打开它,我试图申请。这是我的代码:
I am trying to connect to a Serial Port ... but once I open the Serial Port
in the first time. I can't open it again, I've tried to apply. Here is my code:
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}
}
}
我想关闭该端口,以便将其用于其他任务。
I want to close that port so I can use it for another task.
推荐答案
根据java Communication API,您只需你的串口对象:
According to the java Communication API you just have to close() your serial port object:
serialPort.close();
close()方法来自 SerialPort
超级 CommPort
。
The close() method comes from the SerialPort
super class CommPort
.
这篇关于打开和关闭串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!