【发布时间】:2021-09-09 04:59:06
【问题描述】:
好的,伙计们——最近我的 java 项目和我的 arduino 之间的串行通信出现了一些问题。我正在尝试从我的 arduino 发送字符串并使用 jssc 在 java 中接收它们;但我不断得到
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000007110b5db, pid=12580, tid=3400
#
# JRE version: Java(TM) SE Runtime Environment (16.0.1+9) (build 16.0.1+9-24)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (16.0.1+9-24, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64)
# Problematic frame:
# C [jSSC-2.8_x86_64.dll+0xb5db]
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\charl\IdeaProjects\serialcommunicationtest1\hs_err_pid12580.log
#
# If you would like to submit a bug report, please visit:
# https://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.
#
这是我的代码,如果有人可以做任何事情的话:
Arduino 代码:
int timedelay;
int count;
void setup() {
Serial.begin(9600);
timedelay = 1000;
count = 0;
}
void loop() {
String s = "counter : " + String (count);
Serial.println(s);
count++;
delay(timedelay);
}
和 Java 代码:
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortException;
import jssc.SerialPortList;
public class Main {
public void connect(String portname) {
// now we need to serialport
SerialPort port = new SerialPort(portname);
try {
port.openPort();
port.setParams(
SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE
);
port.addEventListener((SerialPortEvent event)->{
if(event.isRXCHAR()) {
try {
String s = port.readString();
System.out.print(s);
} catch (SerialPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (SerialPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// we need to array of string that hold all the ports
String portlist[] = SerialPortList.getPortNames();
for(int i =0; i<portlist.length;i++) {
System.out.println(portlist[i]);
}
Main obj = new Main();
obj.connect(portlist[0]);
}
}
感谢您的阅读,如果您有任何建议,我们将不胜感激:D
【问题讨论】:
-
Main obj = new Main();似乎是错误的。只需将connect设为静态函数(暂时),然后调用Main.connect(portlist[0]);而不是创建新的Main对象。 -
如果你正在使用废弃的库进行串行通信:github.com/scream3r/java-simple-serial-connector你可能想要切换到更新的 fork github.com/java-native/jssc
标签: java c arduino serial-communication