我正在使用esp 8266通过其串行线控制Epson投影机。我正在使用计算机和OSC来调用运行Epson串行命令的esp上的函数。我执行失败的命令是缩放。用于缩放镜头的Epson Serial命令1"单击"是
ZOOM INC\r
投影机执行代码后,会返回:
或:ERR
。在我的函数中,假设根据用户输入的内容多次执行命令。在我的功能中,我想等到投影机再次执行命令之前响应,这就是我一直在尝试的。
void zoom_inc(OSCMessage &msg){
OSCMessage qLab_msg("/cue/p0/name");
Serial.print("Zoom ");
lock = 1;
char cmd[10] = "ZOOM INC\r";
if(msg.getInt(0) < 0){
cmd[5] = 'D';
cmd[6] = 'E';
}
int high = (abs(msg.getInt(0)) > 50)? 50 : abs(msg.getInt(0));
Serial.print(cmd);
Serial.print(" ");
Serial.print(high);
Serial.println(" times");
unsigned long startTime;
unsigned long currentTime;
unsigned long diff;
boolean response = false;
String readString; //create response string
for(int i = 0; i < high; i++){
projSerial.write(cmd);
startTime = millis();
while(!response){
while (projSerial.available() > 0) { //look for projector response
Serial.write(projSerial.read());
delay(3);
char c = projSerial.read();
readString += c;
}
readString.trim(); //clean projector response
if(readString.length() == 1){
Serial.println("Read Data");
Serial.println(readString.length());
Serial.println(readString);
Serial.println("------------------------------------");
response = true;
}
currentTime = millis();
diff = currentTime - startTime;
if(diff >= 5000 || diff < 0){
Serial.println("Timeout");
response = true;
}
}
delay(200);
}
qLab_msg.add("Zoom Incremental");
Udp.beginPacket(qLabIP, qLabPort);
qLab_msg.send(Udp);
Udp.endPacket();
qLab_msg.empty();
}
这不起作用,因为它只做了它假设的一半。例如,如果用户发送30,则仅执行14
答案 0 :(得分:2)
这看起来很可疑:
Serial.write(projSerial.read());
delay(3);
char c = projSerial.read();
readString += c;
您刚刚从projSerial中读取了两个字符。一个回到你的主串口,另一个将被添加到字符串。从投影机的响应中丢失大致所有其他角色似乎与获得预期步数的一半左右一致。
尝试:
char c = projSerial.read(); // read it just once
Serial.write(c);
readString += c;
我认为delay(3)
来自早先的猜测。