嗨,我尝试使用jserialcomm通过java在Arduino上点亮LED,但是我不能。我使用Jserialcomm库进行串行通信。我的解决方案是在串行连接上从Java向arduino发送“ 1”。 。因此,arduino需要捕捉“ 1”字符并点亮一个led。但是led不能点亮。我需要你的帮助。这是代码:
Java代码:
import com.fazecast.jSerialComm.SerialPort;
import java.io.IOException;
public class JavaArduinoCommunicationWithJserialcomm {
public static void main(String[] args) throws IOException, InterruptedException {
SerialPort sp = SerialPort.getCommPort("COM5");
sp.setComPortParameters(9600, 8, 1, 0);
sp.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 0, 0);
if (sp.openPort()) {
System.out.println("Port is open :)");
} else {
System.out.println("Failed to open port :(");
return;
}
sp.getOutputStream().write(1);
sp.getOutputStream().flush();
if (sp.closePort()) {
System.out.println("Port is closed :)");
} else {
System.out.println("Failed to close port :(");
return;
}
}
}
Arduino代码:
int led = 13;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
int value = Serial.read();
if (value == '1') {
digitalWrite(led, HIGH);
}
if (value == '0') {
digitalWrite(led, LOW);
}
}
}
我在等你的答案。谢谢。