从Java发送字节时Arduino崩溃

从Java发送字节时Arduino崩溃,java,macos,serial-port,arduino,rxtx,Java,Macos,Serial Port,Arduino,Rxtx,我使用arduino网站上的,通过串口发送和接收数据到我的arduino one。然而,出于某种原因,即使我尝试只发送一个字节,Arduino也会在一段时间后崩溃。如果我通过IDE自己的串行监视器手动发送字符,就不会发生这种情况 我编写了以下方法将角色输出到Arduino: public synchronized void serialWrite(char sendIt){ try { output.write((byte)'0'); ou

我使用arduino网站上的,通过串口发送和接收数据到我的arduino one。然而,出于某种原因,即使我尝试只发送一个字节,Arduino也会在一段时间后崩溃。如果我通过IDE自己的串行监视器手动发送字符,就不会发生这种情况

我编写了以下方法将角色输出到Arduino:

public synchronized void serialWrite(char sendIt){
    try {
            output.write((byte)'0');
            output.flush();
            for (int j=0;j<1000000000;j++){
            }
        }catch (Exception e){System.out.println("Not connected...");}
    notify();
}

您的Arduino代码正在通过串行连接发送数据,但您没有从Java程序读取数据。不用多久,各种缓冲区就会被填满,然后Arduino就会等着你解除它们的阻塞


您需要从串行端口读取输出并对其进行处理。我建议运行一个后台线程,阻止读取串行端口,只将字符写入
系统。out
并在收到字符时刷新。

是否有堆栈跟踪、内存转储或某种日志?@AnubianNoob the Arduino的崩溃,不是Java。甚至不是内存转储?我们需要查看您的Arduino代码,以及更详细的“崩溃”描述。我怀疑它可能只是阻止了向您发送一些您在Java程序中没有读取的输入。阿杜伊诺号是坠毁的。Java方面的一切都很好。没有例外被抛出,我不认为是这样。当我不发送任何字符,只接收来自Arduino的数据时,它会在屏幕上正常显示。只有当我发送数据时才会发生崩溃。@francisaugusto因为当你发送数据时,你的Arduino代码会立即发送更多的数据,而当你从Java发送数据时,你还没有准备好接收Arduino输出。但这就是问题所在:我的Java代码确实需要编码才能接收数据。至少当我按下按钮时,它确实显示了它应该显示的数据。它会不会在收到发送的数据后显示它将发送回的数据?@francisaugusto你没有显示任何这样的代码。我提到过这是arduino网站上建议的标准java代码。这是一个:
#include <SoftwareSerial.h>
int buttonState=0;
int lastButtonState=0;
int buttonPushCounter=0;
long previousMillis=0;
long interval=250;
int ledState=LOW;
int ledState2=LOW;
int ledState3=LOW;
long timeElapsed=0;
SoftwareSerial portOne(10,11);

void setup(){
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(2,INPUT); 
  Serial.begin(9600);
  portOne.begin(9600);

}

boolean turnoff; 

void loop(){

  if(portOne.overflow()){
    Serial.println("There's an overflow here!");
  }
  buttonState= digitalRead(2);

  if(buttonState!=lastButtonState){
    if (buttonState==HIGH){
      buttonPushCounter++;
    }
  }
  lastButtonState=buttonState;

  if (turnoff){
    unsigned long currentMillis=millis();

    if (currentMillis-previousMillis>0 && currentMillis-previousMillis<interval){
     ledState=HIGH;
     ledState2=LOW;
     ledState3=LOW;
  }else
     if (currentMillis-previousMillis>interval && currentMillis-previousMillis<interval*2){

     ledState=LOW;
     ledState2=LOW;
     ledState3=HIGH;
  }else
     if (currentMillis-previousMillis>interval*2 && currentMillis-previousMillis<interval*3){

     ledState=LOW;
     ledState2=HIGH;
     ledState3=LOW;
  }else if (currentMillis-previousMillis>interval*3){
    previousMillis=currentMillis;  
  }

    digitalWrite(3,ledState);
   digitalWrite(4,ledState2);
   digitalWrite(5,ledState3);
  }else{
   digitalWrite(3,LOW);
   digitalWrite(4,LOW);
   digitalWrite(5,LOW);

  }


   if (buttonPushCounter==1){
     Serial.print("Button pressed!\n");
    turnoff=!turnoff;
    buttonPushCounter=0;

   }

   noInterrupts();
   char ch=Serial.read();

   delay(1);
   if(ch=='0'){

     Serial.println("Changed by serial"+turnoff);
     Serial.println(ch);
     turnoff=!turnoff;
   } 
   interrupts();


}
public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine=input.readLine();
            System.out.println(inputLine);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    // Ignore all the other eventTypes, but you should consider the other ones.
}