アフィリエイト広告
アフィリエイト広告

Arduino / ubuntuとシリアル通信してみた -3-

シリアル通信を通してパソコンから Arduino へ数値を送りたいという話。

送られてくるのは文字列なので、そいつを数値に変換するためにいろいろ細工が必要になる。でもきっと何か、もっと簡単な方法があるはずだと思っていたわけですが、なんと、あるじゃないですか、数値をそのまま取り出してくれる関数が。

parseInt()
Description
parseInt() returns the first valid (long) integer number from the serial buffer. Characters that are not integers (or the minus sign) are skipped.

しかも附属のスケッチ例にこんなのがあります。

Read ASCII String
This sketch uses the Serial.parseInt() function to locate values separated by a non-alphanumeric character. Often people use a comma to indicate different pieces of information (this format is commonly referred to as comma-separated-values or CSV), but other characters like a space or a period will work too. The values are parsed into integers and used to determine the color of a RGB LED. You’ll use the Arduino Software (IDE) serial monitor to send strings like “5,220,70” to the board to change the light color.

  1. /*
  2.   Reading a serial ASCII-encoded string.
  3.   This sketch demonstrates the Serial parseInt() function.
  4.   It looks for an ASCII string of comma-separated values.
  5.   It parses them into ints, and uses those to fade an RGB LED.
  6.   Circuit: Common-Cathode RGB LED wired like so:
  7.   - red anode: digital pin 3
  8.   - green anode: digital pin 5
  9.   - blue anode: digital pin 6
  10.   - cathode: GND
  11.   created 13 Apr 2012
  12.   by Tom Igoe
  13.   modified 14 Mar 2016
  14.   by Arturo Guadalupi
  15.   This example code is in the public domain.
  16. */
  17. // pins for the LEDs:
  18. const int redPin = 3;
  19. const int greenPin = 5;
  20. const int bluePin = 6;
  21. void setup() {
  22.   // initialize serial:
  23.   Serial.begin(9600);
  24.   // make the pins outputs:
  25.   pinMode(redPin, OUTPUT);
  26.   pinMode(greenPin, OUTPUT);
  27.   pinMode(bluePin, OUTPUT);
  28. }
  29. void loop() {
  30.   // if there's any serial available, read it:
  31.   while (Serial.available() > 0) {
  32.     // look for the next valid integer in the incoming serial stream:
  33.     int red = Serial.parseInt();
  34.     // do it again:
  35.     int green = Serial.parseInt();
  36.     // do it again:
  37.     int blue = Serial.parseInt();
  38.     // look for the newline. That's the end of your sentence:
  39.     if (Serial.read() == '¥n') {
  40.       // constrain the values to 0 - 255 and invert
  41.       // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
  42.       red = 255 - constrain(red, 0, 255);
  43.       green = 255 - constrain(green, 0, 255);
  44.       blue = 255 - constrain(blue, 0, 255);
  45.       // fade the red, green, and blue legs of the LED:
  46.       analogWrite(redPin, red);
  47.       analogWrite(greenPin, green);
  48.       analogWrite(bluePin, blue);
  49.       // print the three numbers in one string as hexadecimal:
  50.       Serial.print(red, HEX);
  51.       Serial.print(green, HEX);
  52.       Serial.println(blue, HEX);
  53.     }
  54.   }
  55. }

スケッチ例ではカラー LED を使用していますが、点灯を試すだけなら 3個の LED を使えばいいです。シリアルモニターから 3つの数値をカンマ区切りで送信すると、その値に応じた色 (明るさ) で LED が点灯します。

シリアルポートから送られてくるデータが文字列だの数値だの考えなくても、そのまま数値として出力できました。

なお、Serial.parseInt() はタイムアウトで終了しますので、cu のように入力した数値を即送信してしまう場合は、タイムアウトするまでにすべてのデータを送信する必要があります。シリアルモニターのようにデータをまとめて送信するものであれば問題はないです。

プログラムからなら、データをまとめて最後に改行コードをつけて送信すればよいので、なんとでもなると思います。

ということで、シリアル通信で数値を送ることも難しく考える必要なんてないことがわかりました。やっぱりなぁ、日本語リファレンスだけじゃなくて、ちゃんと REFERENCE を読まないといけないんだよなぁ (^_^;)

タイトルとURLをコピーしました