標(biāo)題:
Arduino Uno 學(xué)習(xí)筆記 在terminal控制--putty , cu
[打印本頁]
作者:
piaolin
時(shí)間:
2015-10-30 12:10
標(biāo)題:
Arduino Uno 學(xué)習(xí)筆記 在terminal控制--putty , cu
如何讓Uno讀取鍵盤輸入,改變Status LED的明暗並且回傳訊息給電腦呢?有三個(gè)方式:
1: Arduino 的 Serial monitor
2:puttty,,, 不知道怎麼用 telnet 連接到USB...
3:CU ,和putty有共同的問題,沒法exit,而且一次只讀一個(gè)字元
程式碼如下:
const unsigned int LED_PIN = 13;
const unsigned int BAUD_RATE = 9600;
void setup(){
pinMode(LED_PIN,OUTPUT);
Serial.begin(BAUD_RATE);
}
void loop(){
if (Serial.available()>0){
int command = Serial.read();
if (command == '1'){
digitalWrite(LED_PIN,HIGH);
Serial.println("LED on");
} else if (command == '2') {
digitalWrite(LED_PIN,LOW);
Serial.println("LED off");
} else {
Serial.print("Unknown command:");
Serial.println(command,HEX);
}
}
}
以上這段程式碼的意思就是 如果有data進(jìn)來(Serial.available() > 0) ,就進(jìn)入if 判斷式,如果是1,Status LED就亮;如果是2,Status LED就暗;如果都不是,就print "Unknown command:" ,同時(shí)在下一行把輸入的內(nèi)容以16進(jìn)位 print出來。
1: 打開Arduino 的 Tools -> Serial monitor來測(cè)試。輸入1,按send ,燈就亮了。輸入2 後面加中文字,燈還是會(huì)變暗,後面的中文字的code point就已16進(jìn)位print出來?赡苊總(gè)loop只能讀一個(gè)字元,才會(huì)有這樣的情況。
2:Putty就不多作介紹了。
3:cu ,這命令我是第一次用,網(wǎng)上的介紹不多。連接Uno的指令如下:
cu -l /dev/ttyACM0 ,和Putty一樣,沒法exit。只好kill掉了。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1