2017-07-07 14 views
0

私は、自動システムを実行しているArduinoと通信するためにC++プログラムに取り組んでいます。私はこのことから、シリアルライブラリを使用しています:Arduino to C++ Serial Gibberish [解決済み]

https://playground.arduino.cc/Interfacing/CPPWindows

私はアルドゥイーノに、それは、システムが動くべきか、それを教えてくれるコマンドに解析する文字列を送信しようとしています。シリアルからコマンドを読み込むためのコードはこれです:

`無効serialEvent(){

while (Serial.available()) { 
    char in = (char)Serial.read(); 
    input.concat(in); 
    } 
    int endChar = input.indexOf('\r'); 
    if (endChar != -1) { 
    //inputLog.concat(input); 
    //inputLog.concat('\n'); 
    String command = input.substring(0,endChar); 
    command.toLowerCase(); 
    input.remove(0, endChar+1); 
    String keyword = command.substring(0,4); 
    if(keyword == "move"){ 
      int i = command.indexOf('x'); 
      if (i != -1) { 
      endChar = command.indexOf(i, ','); 
      xMove = command.substring(i + 1, endChar).toFloat(); 
      } 
      i = command.indexOf('y'); 
      if (i != -1) { 
      endChar = command.indexOf(i, ','); 
      yMove = command.substring(i + 1, endChar).toFloat(); 
      } 
    } else if (keyword == "stop") { 
     xMove = 0; 
     yMove = 0; 
    } else if (keyword == "xpos") { 
     Serial.print("X: "); 
     Serial.println(xPos); 
    } else if (keyword == "ypos") { 
     Serial.print("Y: "); 
     Serial.println(yPos); 
    } else if (keyword == "getp") { 
     Serial.print("X: "); 
     Serial.print(xPos); 
     Serial.print(", Y: "); 
     Serial.println(yPos); 
    } else if (keyword == "setx") { 
     xPos = command.substring(4).toFloat(); 
    } else if (keyword == "sety") { 
     yPos = command.substring(4).toFloat(); 
    } else if (keyword == "setp") { 
     int i = command.indexOf('x'); 
      if (i != -1) { 
      endChar = command.indexOf(i, ','); 
      xPos = command.substring(i + 1, endChar).toFloat(); 
      } 
      i = command.indexOf('y'); 
      if (i != -1) { 
      endChar = command.indexOf(i, ','); 
      yPos = command.substring(i + 1, endChar).toFloat(); 
      } 
    } else if (keyword == "getl") { 
     Serial.println(inputLog); 
     inputLog = ""; 
    } else { 
     Serial.println("error: 1"); 
     return; 
    } 
    Serial.println("ok\r"); 
    //Serial.print(nc); 
    } 

}`

この部分は、私はそれを実行することができますので、正常に動作しているように見えますArduinoシリアルモニターとそれは動作します。

while (SP->IsConnected()) 
{ 
    string outputData = ""; 
    cin >> outputData; 
    outputData.append(pcr); 
    //outputData.append(pnc); 
    writeResult = SP->WriteData((char*)outputData.c_str(), outputData.length()); 
    //writeResult = writeResult && SP->WriteData(pnc, 1); 
    //writeResult = writeResult & SP->WriteData(pcr, 1); 

    while (true) { 
     Sleep(500); 
     char incomingData[256] = ""; 
     readResult = SP->ReadData(incomingData, dataLength); 
     //printf("Bytes read: (0 means no data available) %i\n", readResult); 
     //incomingData[readResult] = 0; 
     input.append(incomingData, 0, readResult); 

     check_ok = input.find(pcr); 
     check_error = input.find(error); 

     if (check_ok != string::npos || check_error != string::npos || readResult == 0) { 
      cout << "Receiving: " << input << '\n'; 
      input.clear(); 
      break; 
     } 
    } 
} 

変数は、PCRをArduinoのコマンドの終了を識別するために使用する改行文字へのポインタとして以前のコードで定義される:ここでIはC++で書いたコードの短い試験であります。このコードを実行した結果、Arduinoは最初のコマンドを読み込んで実行しているようです。しかし、それはまたいくつかの不器用な文字を読み上げ、その大部分は数値として-52に相当します。次のコマンドは何もしていないようだが、arduinoはぎこちないキャラクターを続けている。さらに、C++のReadData関数は正常に動作しているようです。なぜこれが起こるのか誰にも分かりますか? Arduinoのシリアルバッファは、何とかWriteData関数によって破損していますか?

編集:

私はこの問題を発見したようです。ポインタがnullで終了していないため、outputData(pcr)の文がoutputData(pcr、0、1)である必要がありました。したがって、文字列はヌル文字に達するまで余分な文字を追加していました。コメントと答えをありがとう。

+0

シリアルポートを正しく設定しましたか?正しいボーレート、終了文字、そのようなもの?シリアルモニタへの通常の出力は、悪いボーレートを意味します。 –

+0

いいえ、それは悪いボーレートではありません。それは一貫してすべきすべてを読んでいます。その唯一の理由は、私がarduinoを送信するデータの最後に不気味さを加えます。 –

+0

よかったです。ストップビット、パリティ、および改行文字が正しく設定されていることを確認して確認しましたか? –

答えて

0

データラインの長さはどのくらいですか? TTLレベルを使用してデータを送信しているかどうかを確認しますか? 回線が数メートルより長く、TTLレベルの通信(デバイス間で直接配線を行う)を使用している場合、間違いなく他のデバイスからゴミが届きます。長距離通信回線は、代わりにRS232またはRS485を使用する必要があります。

関連する問題