该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
问题如题了
能向板子发送数据,板子也是正常的有向串口通信
但是就是自己写的工具这边接收不到
arduino自带的串口监视器可以正常用
贴出代码麻烦大佬们给瞅瞅
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
public class FindPort implements SerialPortEventListener {
String result = "结果: ";
CommPortIdentifier portIdentifier = null;
Enumeration commPortIdentifierEnumeration = null;
SerialPort serialPort = null;
InputStream inputStream = null;
OutputStream outputStream = null;
public static void main(String[] args) {
FindPort findPort = new FindPort();
findPort.openPort();
findPort.readDataFromPort();
findPort.closePort();
System.out.println(findPort.result);
}
public void openPort() {
commPortIdentifierEnumeration = CommPortIdentifier.getPortIdentifiers();
while (commPortIdentifierEnumeration.hasMoreElements()) {
portIdentifier = commPortIdentifierEnumeration.nextElement();
if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("发现串口 " + portIdentifier.getName());
try {
serialPort = (SerialPort) portIdentifier.open(portIdentifier.getName(), 2000);
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
outputStream = serialPort.getOutputStream();
outputStream.write("R".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
outputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void serialEvent(SerialPortEvent serialPortEvent) {
String message = "";
switch (serialPortEvent.getEventType()) {
case SerialPortEvent.BI: //通讯中断
message = "Error: BI";
case SerialPortEvent.OE: //溢位错误
message = "Error: OE";
case SerialPortEvent.FE: //帧错误
message = "Error: FE";
case SerialPortEvent.PE: //奇偶校验错误
message = "Error: PE";
case SerialPortEvent.CD: //载波检测
message = "Error: CD";
case SerialPortEvent.CTS: //清除发送
message = "Error: CTS";
case SerialPortEvent.DSR: //数据设备准备好
message = "Error: DSR";
case SerialPortEvent.RI: //响铃侦测
message = "Error: RI";
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: //输出缓冲区已清空
message = "Error: OUTPUT_BUFFER_EMPTY";
System.out.println(message);
break;
case SerialPortEvent.DATA_AVAILABLE: {
readDataFromPort();
}
break;
default:
break;
}
}
public void readDataFromPort() {
byte[] bytes = new byte[1024];
try {
if (serialPort != null) {
inputStream = serialPort.getInputStream();
inputStream.read();
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
result += new String(bytes, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
outputStream.flush();
outputStream.close();
outputStream = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void closePort() {
if (serialPort != null) {
serialPort.notifyOnDataAvailable(false);
serialPort.removeEventListener();
if (inputStream != null) {
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
outputStream = null;
} catch (Exception e) {
e.printStackTrace();
}
}
serialPort.close();
serialPort = null;
}
}
}