gpt4 book ai didi

java - 在 Linux 中使用 Java RXTX 与 Arduino 进行串行通信

转载 作者:太空狗 更新时间:2023-10-29 11:13:20 35 4
gpt4 key购买 nike

我正在尝试使用 java 程序连接 Arduino,当从串行连接将数据从 pc 发送到 arduino 时它给出了错误并且程序崩溃了

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f0dbc202462, pid=11386, tid=139696967497472
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b14) (build 1.8.0_45-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [librxtxSerial.so+0x6462] read_byte_array+0x52
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

这些是我的初始化方法和串口发送方法

串口连接初始化

public void initialize() {
// the next line is for Raspberry Pi and
// gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

//First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}

try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);

// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();

// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);


//added

} catch (Exception e) {
System.err.println(e.toString());
}
}

通过串行连接发送数据

public synchronized void send_command(String command) {
try {
output = serialPort.getOutputStream();

output.write(command.getBytes());
System.out.println(command);

} catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "Serial Connection Error!", JOptionPane.WARNING_MESSAGE);
}

}

串口关闭方法

 public synchronized void close() {
try{
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}

}catch (Exception e){
e.printStackTrace();
}

}

最佳答案

好在瞎折腾找到了解决办法,好像是

这里为 Raspberry pi 编写的权利导致它......评论它解决了问题......

System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

//System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

还是不知道到底是什么问题...

关于java - 在 Linux 中使用 Java RXTX 与 Arduino 进行串行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31369133/

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