【发布时间】:2015-09-15 16:39:09
【问题描述】:
我正在尝试连接到我的串行端口“COM6”,但现在我无法连接到它。我尝试使用portId.getName() 打印出可用的串行端口列表。它显示了可用的串行端口列表,但 COM6 没有显示在那里。
这是我的 Java 代码:
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM6")) {
//if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("Arduino", 2000);
} catch (PortInUseException e) {
System.out.println("Error1 is "+ e);
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
System.out.println("Error2 is "+ e);
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
System.out.println("Error3 is "+ e);
}
try{
byte data1 = 1;
outputStream.write(data1);
System.out.println("helo");
System.out.println(data1);
}catch(IOException e){
System.out.println("Error 3: " + e);
}
}
}
}
serialPort.close();
我已在设备管理器中检查了 COM6。它显示可用。如果我在 vb 中运行相同的 com 端口,它会完美运行。谁能帮我解决这个问题。谢谢。
【问题讨论】:
标签: java serialization serial-port serial-communication