gpt4 book ai didi

java - 打开和关闭串口

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:52 27 4
gpt4 key购买 nike

我正在尝试连接到 Serial Port ...但是一旦我第一次打开串口。又打不开了,我试过申请了。这是我的代码:

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) {}
}
}
}
}

我想关闭那个端口,以便我可以将它用于其他任务。

最佳答案

根据java Communication API,你只需要close()你的串口对象:

serialPort.close();

close() 方法来自 SerialPort 父类(super class) CommPort

关于java - 打开和关闭串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6924516/

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