2017-02-25 63 views
1

我正在使用esp 8266來控制其串行線路上的Epson投影機。我正在使用計算機和OSC在運行Epson串行命令的esp上調用函數。我遇到麻煩的命令是縮放。愛普生串行命令放大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

回答

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)是從早期的猜測。