IM920で受信したデータをそのまま返すプログラム

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
taquto
Date:
Fri Jul 30 18:09:55 2021 +0000
Parent:
0:337a66b6778a
Commit message:
This program enables F303K8's LED to blink when it receives a particular command from IM920.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 337a66b6778a -r ad1d414d86fb main.cpp
--- a/main.cpp	Thu May 20 06:54:16 2021 +0000
+++ b/main.cpp	Fri Jul 30 18:09:55 2021 +0000
@@ -1,34 +1,47 @@
 //参考サイト:http://www.maroon.dti.ne.jp/koten-kairo/works/dsPIC/serial6.html
 //mbedシリアル通信参考サイト:https://os.mbed.com/users/okini3939/notebook/Serial_jp/
 //IM920とマイコンだけで通信を行うプログラム。
-//データを受信した場合に受信した文字列を返す。
+//'b0'(blast-off(0ff)の頭文字)を受信したとき、LEDが点滅し始める。
 
 #include "mbed.h"
+DigitalOut myled(LED1);
 
 char temp;
-char str[100];
+char str[10];
+
+int k=0;
 int i=0;
+//int n=0;
 
 int main()
 {
+    Serial pc(USBTX, USBRX,38400);//ボーレートを落とすと,USB側からのデータが正確に出力されない.
     Serial im920(PA_9,PA_10,19200);//TX(IM920_RX), RX(IM920_TX)
 
+    myled = 0;
+
     while(1) {
-        if(im920.readable()){//IM920からのデータがある場合
+        //pc.printf("%d\n\r",k);
+
+        if(im920.readable()) { //IM920からのデータがある場合
             temp = im920.getc();//一文字読み込む
-            if(temp != '\r'){//読み込み文字が改行で無い場合
+            if(temp != '\r') { //読み込み文字が改行で無い場合
                 str[i] = temp;
                 i++;
+            } else if(temp == '\r') { //読み込み文字が改行の場合
+                if(str[i-2] == 'B') {
+                    if(str[i-1] == '0') {
+                        k = 1;
+                    }
+                }
             }
-            else if(temp == '\r'){//読み込み文字が改行の場合
-                im920.printf("TXDA ");
-                for(int j=0;j<i;j++){
-                    im920.putc(str[i]);
-                    }
-                im920.printf("\n\r");
-                i = 0;
-                }
+        }
+
+        if(k == 1) {
+            myled = 1;
+            wait(0.2);
+            myled = 0;
+            wait(1.0);
         }
     }
-
 }