Port.openPort()始终返回false

时间:2019-06-14 15:35:18

标签: spring-boot arduino serial-port spring-restcontroller

嗨,我正尝试从spring boot应用程序发送数据以在Arduino串行中显示,我试图根据我在互联网上找到的一些教程编写代码。当我运行该应用程序时,我没有出现错误并选择了Port。 openPort()始终返回false,并且串行返回的内容始终断开 这是春季启动应用程序

打包com.example.databasedemo;

import com.fazecast.jSerialComm.SerialPort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.PrintWriter;

import static com.fazecast.jSerialComm.SerialPort.getCommPort;


@RestController
public class arduino {



    static com.fazecast.jSerialComm.SerialPort chosenPort;
    @RequestMapping(value="/get")
   public String getdata() throws IOException {

      SerialPort[] portNames =SerialPort.getCommPorts();
       System.out.println(portNames[0]);
       chosenPort =getCommPort("COM4");
       System.out.println(chosenPort.getSystemPortName());

       if(chosenPort.openPort()) {




                   // enter an infinite loop that sends text to the arduino
                   PrintWriter output = new PrintWriter(chosenPort.getOutputStream());

                       output.print("first try");
                       output.flush();
return "send ";

       }
    else {
        // disconnect from the serial port
        chosenPort.closePort();
        return "dis"+chosenPort.getSystemPortName();

    }

    }


}

这是Arduino代码:

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
  Serial.setTimeout(50);
}

void loop() {
  // put your main code here, to run repeatedly:
 String text = Serial.readString();
 Serial.println("name= "+text);
 delay(1000);
}

0 个答案:

没有答案