1

UNO のハードウェア シリアルに問題があります。多くの場合、1 文字 (通常は受信された最初の文字) が欠落しているように見え、場合によっては送信全体が失われます。これは、シリアル モニターで入力したコンピューターから Arduino がデータを受信して​​いる場合にのみ発生します。文字列を送信すると RX ライトが点滅しているのが見えますが、arduino はそれを完全に無視します。

私が送信しているデータは、カンマで区切られた 3 つの 8 ビット符号なし整数です。

#include <Adafruit_NeoPixel.h>
//#include <OneSheeld.h>

#define PIN 6
#define LEDS 5

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);
int leds = LEDS-1;
byte red;
byte green;
byte blue;
int i;
// pins for the LEDs:
//const int red = 3;
//const int green = 5;
//const int blue = 6;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  strip.begin();
  strip.setPixelColor(0,12,12,12);
     strip.show(); // Initialize all pixels to 'off'
     Serial.print("number of LEDS in strip:");
     Serial.println(LEDS);
     i=0;
  // make the pins outputs:
//  pinMode(redPin, OUTPUT); 
//  pinMode(greenPin, OUTPUT); 
//  pinMode(bluePin, OUTPUT); 

}

void loop() {
  red = 0;
  green=0;
  blue= 0;

 // i=0;
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
      red = Serial.parseInt(); 
    // do it again:
      green = Serial.parseInt(); 
    // do it again:
       blue = Serial.parseInt();
       delay(1);

    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {
      // constrain the values to 0 - 255 and invert
      // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
//      red = constrain(red, 0, 255);
//      green = constrain(green, 0, 255);
//      blue = constrain(blue, 0, 255);     
         Serial.print("LED being served = ");
         Serial.println(i);


      // fade the red, green, and blue legs of the LED: 

//      analogWrite(redPin, red);
//      analogWrite(greenPin, green);
//      analogWrite(bluePin, blue);
      strip.setPixelColor(i,red,green,blue);
      strip.show();

      // print the three numbers in one string as hexadecimal:
      Serial.print("R=");
      Serial.println(red);
      Serial.print("G=");
      Serial.println(green);
      Serial.print("B=");
      Serial.println(blue);

     if(i==leds)
     i=0;
     else
     i=i+1;
    }
  }
}

次の文字列 string を入力したときのシリアル モニタからの出力例を次に示します: <25,25,25>

出力:

number of LEDS in strip:5
LED being served = 0
R=25
G=25
B=25
LED being served = 1   <<< this transmission got lost the first time it was sent
R=25
G=25
B=25
LED being served = 2
R=25
G=25
B=25
LED being served = 3
R=5
G=25
B=25
LED being served = 4 <<< This transmission got lost the first 4 times it was sent
R=5
G=25
B=25
LED being served = 0
R=25
G=25
B=25
LED being served = 1
R=25
G=25
B=25
LED being served = 2
R=25
G=25
B=25
LED being served = 3
R=25
G=25
B=25
LED being served = 4
R=5
G=25
B=25
LED being served = 0
R=25
G=25
B=25
LED being served = 1
R=5
G=25
B=25
LED being served = 2
R=25
G=25
B=25
LED being served = 3
R=25
G=25
B=25
LED being served = 4
R=25
G=25
B=25

ありがとう

4

1 に答える 1

2

あなたの問題はおそらくタイミングの問題です:

while (Serial.available() > 0) {
  red = Serial.parseInt(); 
  green = Serial.parseInt(); 
  blue = Serial.parseInt();

この while ループに初めて入ったときは、シリアル ラインから最初のバイトを受け取っただけであることに注意してください。2 番目のバイトはまだ送信されていない可能性があります。「25,25,25」を Arduino に送信し、最初の文字しかない場合は、red = Serial.parseInt()2 を返し、それを赤に割り当てます。green = Serial.parseInt()5 をblue = Serial.parseInt()取得し、25 を取得します。その後、シリアル バッファーに ",25" が残ったままになります。これにより、次の loop() のパスでさらに問題が発生する可能性があります。

parseInt() のドキュメントでは、有効な整数バイトを最大 1 秒待機すると述べていますが、1/Hz 秒待機して存在するかどうかを確認するかどうかについては、1 秒後に何をするかについては述べていません。もっと来るかどうか。そうであれば、潜在的なハードウェアの原因を調べる必要があります....

シリアルは信頼できるプロトコルではないことに注意してください。各バイトは、アプリケーションに配信される前にパリティがチェックされます。パリティが失敗した場合、まったく配信されない可能性があります。

以前、Arduinoでこのような問題が発生しました。漂遊RF、ブレッドボードのノイズ、またはその他の電気的問題がいくつかあるだけで、シリアルラインを読み取ると0が1に、またはその逆になります. シリーズの最初のバイトで損失が発生していることを考えると、この症状が見られるのではないかと強く思います。シリアルピンの電圧蓄積が遅い場合、読み取りに行くと、最初に 1 が表示されます。現在のプロジェクトでは、最上位ビットを 0 と 1 に交互に設定することで上位バイトと下位バイトを識別します。 . 時折、接頭辞が 0 のバイトのトリプルまたは接頭辞が 1 のトリプルを取得し、それらのサンプルを単にチャックします。

あなたのUNOは電気的に絶縁された表面に置かれていますか? ブレッドボードを使用している場合、どれくらい信頼できますか? 接続ラインに沿ってほぼゼロの抵抗が得られ、それらの間のほぼ無限の抵抗が得られますか? シリアル チャネルを処理する安っぽい USB ケーブルを持っていますか? もしそうなら、近くにある強力な RF 信号が問題の原因になっている可能性があります。

于 2014-07-23T01:25:02.047 に答える