シリアル経由で Arduino に色を送信しようとしています。これは、Arduino に色を送信する私の Mac で実行されている Objective-C コードです。
unsigned char rgb[4];
rgb[1] = ...some color
rgb[2] = ...some color
rgb[3] = ...some color
rgb[0]=0xff; //I am setting the first value to 0xff so I know where to start reading the bytes
if(_serialFileDescriptor!=-1) {
write(_serialFileDescriptor, rgb, 4);
}
私が送信した後、Arduinoはそれを受け取ります。最初に読み取った最初のバイトが 0xff であるかどうかを確認して、Arduino をコンピューターと同期させます。もしそうなら、私は続けて色をつけます。ここでの問題は、明らかに最初のバイトが 0xff ではなく、if ステートメントが入力されないことです。
void loop(){
//protocol expects data in format of 4 bytes
//(xff) as a marker to ensure proper synchronization always
//followed by red, green, blue bytes
char buffer[4];
if (Serial.available()>3) {
Serial.readBytes(buffer, 4);
if(buffer[0] == 0xff){ //when I comment out the if statement code works fine but //the colors which are read are wrong
red = buffer[1];
green= buffer[2];
blue = buffer[3];
}
}
//finally control led brightness through pulse-width modulation
analogWrite (redPin, red);
analogWrite (greenPin, green);
analogWrite (bluePin, blue);
}
Objective-Cコードでこれに設定されているにもかかわらず、最初の読み取りバイトがbever 0xffである理由がわかりません。