Java Arduino端口卡住了
我正在尝试创建一种模式,可以打开和关闭Arduinos的一个端口。然而,在大约2次开/关之后,它会被卡住,并且不会关闭,除非我拔下Arduino。我尝试过很多解决方案,比如在int向端口发送电源后,尝试将int设置为0。以下是我在服务器端和arduino端的代码: Arduino代码:Java Arduino端口卡住了,java,serial-port,arduino,Java,Serial Port,Arduino,我正在尝试创建一种模式,可以打开和关闭Arduinos的一个端口。然而,在大约2次开/关之后,它会被卡住,并且不会关闭,除非我拔下Arduino。我尝试过很多解决方案,比如在int向端口发送电源后,尝试将int设置为0。以下是我在服务器端和arduino端的代码: Arduino代码: int Relay = 13; //The pin that the relay is attached to //int time; //Creates temp variable void setup()
int Relay = 13;
//The pin that the relay is attached to
//int time;
//Creates temp variable
void setup() {
Serial.begin(9600);
pinMode(Relay, OUTPUT);
}
void loop() {
while(true) {
//Check if data has been sent from the computer:
if (Serial.available()) {
int time;
//Assign serial value to temp
time = Serial.parseInt();
//Output value to relay
delay(1000);
digitalWrite(Relay, HIGH);
delay(time);
digitalWrite(Relay, LOW);
}
}
}
服务器代码:
for(int i = 0; i < 11; i++) {
out.write("10000".getBytes("UTF-8"));
out.flush();
Thread.sleep(1500);
}
for(int i=0;i<11;i++){
out.write(“10000.getBytes”(“UTF-8”);
out.flush();
睡眠(1500);
}
如果有人发现任何错误或看到解决方案,请告诉我
谢谢:)我刚刚意识到我做错了什么。在这段代码中:
out.write("10000".getBytes("UTF-8"));
out.flush();
Thread.sleep(1500);
我希望它等待10秒,直到它发送另一个信号,但它发出了out.write(“10000.getBytes”(“UTF-8”)代码>
每1.5秒一次,导致出现故障。为了解决这个问题,我改变了
Thread.sleep(1500);
到
谢谢:)我的元素已经用完了,但是有没有可能因为客户端延迟,串行缓冲区正在生成一个大整数(“100001000010000”),并且当客户端调用parseInt
时,延迟是巨大的?我不确定如何实现parseInt
。。。
Thread.sleep(11500);