2017-11-03 7 views
0

저는 아주 간단한 어플리케이션을 가지고 있습니다. 블루투스를 통해 내 Arduino에서 내 이오닉 어플리케이션 (안드로이드)으로 데이터를 보내야합니다.블루투스로 여러 데이터 읽기 이모닉을 사용하여 시리얼을 읽으십시오.

그러나 Ionic에서이 작업을 수행하는 올바른 방법에 대한 질문이 있습니다.

아두 이노 코드

char BTString; 

/* serial1 is a BluetoothSerial instance */ 
if (serial1.available()) { 
    BTString = serial1.read(); 

    /* S is the command that my Ionic application sends to start the measurement */ 
    if (BTString == 'S') { 
      BTString = ""; 

     /* Here is where I will do the calculations of the measurements and send the results via Bluetooth, but in this example I will only send hypothetical results */ 

      serial1.write("D"); // This indicates Start 
      serial1.write("45.55"); // measurement 1 
      serial1.write("79.80"); // measurement 2 
      serial1.write("67.20"); // measurement 3 
      serial1.write("F"); // This indicates the end 
    } 
} 

이온 코드

this._bluetoothSerial.write('S') // Start the measurement 
    .then((data: any) => { 
    this.measuring(); // Wait for results 
    }) 
    .catch((e) => { 
    this.errorCommBluetooth(); // Error alert 
    }); 


    private measuring(): void { 
    this.readOk = false; 

    do { 
     this._bluetoothSerial.available() 
     .then((data: any) => { 
     this._bluetoothSerial.readUntil('F') 
     .then((data: any) => { 

      if (data[0] == 'D') { 
      this.measurement1 = data[1] + data[2] + data[3] + data[4] + data[5]; 
      this.measurement2 = data[6] + data[7] + data[8] + data[9] + data[10]; 
      this.measurement3 = data[11] + data[12] + data[13] + data[14] + data[15]; 

      this.readOk = true; 
      } 

     }); 
     }); 
    }while (this.readOk == false); 
    } 

이 코드는 작동하지 않습니다.

이 작업을 수행하고 readUntil 함수를 사용하여 3 개의 변수에 측정 값을 저장하는 가장 좋은 방법은 무엇입니까?

답변

0

해결! 이 코드는 나를 위해 일했습니다 :

this._bluetoothSerial.available() 
.then((number: any) => { 
    this._bluetoothSerial.read() 
    .then((data: any) => { 
     if (data[0] == "D" && data[16] == "F") { 
     this.measure1 = parseFloat(data[1]+data[2]+data[3]+data[4]+data[5]); 
     this.measure2 = parseFloat(data[6]+data[7]+data[8]+data[9]+data[10]); 
     this.measure3 = parseFloat(data[11]+data[12]+data[13]+data[14]+data[15]); 

     this._bluetoothSerial.clear(); 
     } 
    }); 
});