首页 > 解决方案 > arduino 和 java 之间的串行通信“检测到致命错误”

问题描述

好的,伙计们——最近我的 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

标签: javacarduinoserial-communication

解决方案


我正在使用一个过时的库 - ty JockX


推荐阅读