data.txtに設定した文字列をUSB-Serialに出力し続けるプログラムです。 text dataを出力する装置の模擬装置として使用します。 data.txtはmbedのlocalに保管しておきます。
Dependencies: mbed BufferedSerial
<mbed側の準備>
serial_dummyはmbedのlocalに保管した data.txt のtext dataをUSB-serialから出力し続けるプログラムです。 text data を出力する装置の模擬として使用します。
data.txtの例は次のところにあります。 /media/uploads/suupen/data.txt
data.txt には出力する文字列のほかに、 1.USB-serial のbaudrate 9600[bps]の例
B9600
2.送信文字列の送信時間間隔 1000[ms]の例
T1000
を設定できます。 '>B','>T' のコマンドは、設定した次の行の文字列から有効になります。
<パソコン側の準備>
windowsの場合パソコンにUSB-serialのdriverをインストールする必要があります。 driver は次のページからダウンロードして下さい。 https://mbed.org/handbook/Windows-serial-configuration
Revision 0:7c6ad253848a, committed 2014-07-19
- Comitter:
- suupen
- Date:
- Sat Jul 19 02:12:20 2014 +0000
- Child:
- 1:c6e8a41200d9
- Commit message:
- ????????(data.txt)?????????USB-serial???????????????; text data ?????????????????????
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Jul 19 02:12:20 2014 +0000 @@ -0,0 +1,124 @@ +/** + serial data 送信ツール + + V00.01 : 140719 + + + <使い方> +"data.txt" に送信する文字列を設定し、mbedに保管する。 +mbedに通電するかRESET SW を押すとUSB-serialから文字列を出力する + +<data.txtの設定方法> +1.コマンド + USB-serialのbaudrate + 9600[bps]を設定する場合 + >B9600 + >b9600 + + 文字列間の送信間隔 + 1000[ms]を設定する場合 + >T1000 +2.送信文字列 + asciiコードで文字を記述していく + 送信は一行毎となる + +3.data.txt設定例 +次の行から----------------- +>B9600 +>T100 +123456789 +>T200 +abcdefghi +>T300 +ABCDEFGHI + +>B9600 +>T1000 +123 +abc +ABC +ひとつ前の行まで------------- + +<USB-Serialの通信設定値> +baudrate : data.txtで設定可能(初期値=9600[bps] +bits : 8bit +parity : none +stopbit : 1bit + + +*/ +#include "mbed.h" +#include "ctype.h" + +LocalFileSystem local("local"); // local file systemの設定 +Serial pc(USBTX, USBRX); // usb-serialの設定 + + +DigitalOut ledopen(LED1); // 1:file open 0:file close +//DigitalOut ledout(LED2); // 1: serial out +//DigitalOut lederror(LED4); + +char buffer[500]; // 読みだしたデータの保管先 + + +int main() +{ + FILE *fp; + int ans; + char *p; + int timer; // serial送信時間間隔 ( 1/1 [ms]/bit) + int baud; // usb-serialのbaudrate ( 1/1 [bps]/bit) + + fp = fopen("/local/data.txt", "r"); + ledopen = 1; + + + while(1) { + ans = fscanf(fp,"%s",buffer); + if(ans != -1) { + p = buffer; + // 文字列 or 制御コマンド + if(*p++ == '>') { + // コマンドとして処理する + switch(*p++) { + case 'T': + case 't': + // serial data の送信間隔時間を設定 + timer = 0; + while(isdigit(*p) == 1) { + timer *= 10; + timer += (*p++ - '0'); + } + break; + case 'B': + case 'b': + // baudrate 設定 + baud = 0; + while(isdigit(*p) == 1) { + baud *= 10; + baud += (*p++ - '0'); + } + pc.baud(baud); + break; + default: + // nothing + break; + } + + } else { + // 出力文字列として処理する + pc.printf("%s\n",buffer); + wait_ms(timer); + } + } else { + // 読みだすデータがなくなったら、先頭に戻す + fclose(fp); + ledopen = 0; + + wait_ms(timer); + + fp = fopen("/local/data.txt", "r"); + ledopen = 1; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Jul 19 02:12:20 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/04dd9b1680ae \ No newline at end of file