无法在串行和弹簧启动之间建立连接

时间:2019-06-23 20:51:47

标签: java spring spring-boot arduino

我试图从串行和春季启动中读取数据,以便可以从Arduino读取数据,所以我使用了这段代码

package com.example.databasedemo;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;

public class SerialTest implements SerialPortEventListener {
    SerialPort serialPort;
    /** The port we're normally going to use. */
    private static final String PORT_NAMES[] = {                  "/dev/tty.usbserial-A9007UX1", // Mac OS X
            "/dev/ttyUSB0", // Linux
            "COM4", // Windows
    };
    private BufferedReader input;
    private OutputStream output;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 9600;

    public void initialize() {
        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 {
            serialPort = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);
            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();

            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }


    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine=null;
                if (input.ready()) {
                    inputLine = input.readLine();
                    System.out.println(inputLine);
                }

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

    public static void main(String[] args) throws Exception {
        SerialTest main = new SerialTest();
        main.initialize();
        Thread t=new Thread() {
            public void run() {
                //the following line will keep this app alive for 1000    seconds,
                //waiting for events to occur and responding to them    (printing incoming messages to console).
                try {Thread.sleep(1000000);} catch (InterruptedException    ie) {}
            }
        };
        t.start();
        System.out.println("Started");
    }
}

但是当我运行应用程序(如果arduino正在运行字符串启动)时,出现此错误:

#
# An error report file with more information is saved as:
# C:\Users\ELA_HIDRI\IdeaProjects\databasedemo\hs_err_pid12940.log
#
# 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.
#

Process finished with exit code 1

,以及当Spring Boot运行Arduino时  给我这个错误

avrdude: ser_open(): can't open device "\\.\COM4": Access is denied.

我没有找到一种方法来解决它,当第一个正在读取另一个时,另一个被阻塞了,而另一个正在发送时,另一个人却被阻塞了。

这是我无法发布的文件,太大了

# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180003990, pid=23176, tid=6896
#
# JRE version: Java(TM) SE Runtime Environment (11.0+28) (build 11+28)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (11+28, mixed mode, tiered, compressed oops, g1 gc, windows-amd64)
# Problematic frame:
# C  [rxtxSerial.dll+0x3990]
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# 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.
#

---------------  S U M M A R Y ------------

Command Line: -Djava.library.path=C:\Users\ELA_HIDRI\Desktop -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=55606:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\bin -Dfile.encoding=UTF-8 com.example.databasedemo.SerialTest

Host: Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz, 4 cores, 7G,  Windows 10 , 64 bit Build 17763 (10.0.17763.475)
Time: Sun Jun 23 22:18:34 2019 W. Central Africa Standard Time elapsed time: 1 seconds (0d 0h 0m 1s)

---------------  T H R E A D  ---------------

Current thread (0x000001599e5af000):  JavaThread "Thread-0" [_thread_in_native, id=6896, stack(0x00000053b3500000,0x00000053b3600000)]

Stack: [0x00000053b3500000,0x00000053b3600000],  sp=0x00000053b35fe320,  free space=1016k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native 

code)
    C  [rxtxSerial.dll+0x3990]
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  gnu.io.RXTXPort.readArray([BII)I+0
j  gnu.io.RXTXPort$SerialInputStream.read([BII)I+212
j  sun.nio.cs.StreamDecoder.readBytes()I+135 java.base@11
j  sun.nio.cs.StreamDecoder.implRead([CII)I+112 java.base@11
j  sun.nio.cs.StreamDecoder.read([CII)I+180 java.base@11
j  java.io.InputStreamReader.read([CII)I+7 java.base@11
j  java.io.BufferedReader.fill()V+145 java.base@11
j  java.io.BufferedReader.readLine(Z)Ljava/lang/String;+44 java.base@11
j  java.io.BufferedReader.readLine()Ljava/lang/String;+2 java.base@11
j  com.example.databasedemo.SerialTest.serialEvent(Lgnu/io/SerialPortEvent;)V+24
j  gnu.io.RXTXPort.sendEvent(IZ)Z+382
v  ~StubRoutines::call_stub
j  gnu.io.RXTXPort.eventLoop()V+0
j  gnu.io.RXTXPort$MonitorThread.run()V+12
v  ~StubRoutines::call_stub

siginfo: EXCEPTION_ACCESS_VIOLATION (0xc0000005), reading address 0xffffffffb35ff158


Register to memory mapping:

RIP=0x0000000180003990 rxtxSerial.dll
RAX=
[error occurred during error reporting (printing register info), id 0xc0000005, EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffbb1654d76]

0 个答案:

没有答案