java 关闭串口_java – 打开和关闭串行端口
我正在嘗試連接到Serial Port …但是一旦我第一次打開(kāi)串行端口.我不能再打開(kāi)它,我試著申請(qǐng).這是我的代碼:
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) {}
}
}
}
}
我想關(guān)閉該端口,以便我可以將它用于其他任務(wù).
總結(jié)
以上是生活随笔為你收集整理的java 关闭串口_java – 打开和关闭串行端口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电脑改硬盘模式惠普(惠普主机改硬盘模式)
- 下一篇: java collection详解_ja