步驟1:安裝
下載后,打開終端并輸入:
tar xfvz /Users/*Account*/Downloads/pyserial-2.6.tar.gz
cd pyserial-2.6
sudo python setup.py install
為確保所有安裝正確的設(shè)備都打開空閑并輸入在“導(dǎo)入序列號”中。如果沒有錯誤出現(xiàn),則一切正常。
您可以通過
ls /dev/tty.*
行檢查可用的端口,步驟2:對Arduino進(jìn)行編程
現(xiàn)在進(jìn)行測試,將以下草圖上傳到Arduino。我不知道這在Arduino克隆上將如何工作。
void setup() {
Serial.begin(9600); // set the baud rate
Serial.println(“Ready”); // print “Ready” once
}
void loop() {
char inByte = ‘ ’;
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}
delay(100); // delay for 1/10 of a second
}
步驟3:程序空閑
下一步在Idle中創(chuàng)建一個新窗口并創(chuàng)建以下程序。
from time import sleep
import serial
ser = serial.Serial(‘/dev/tty.usbmodem1d11’, 9600) # Establish the connection on a specific port
counter = 32 # Below 32 everything in ASCII is gibberish
while True:
counter +=1
ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
print ser.readline() # Read the newest output from the Arduino
sleep(.1) # Delay for one tenth of a second
if counter == 255:
counter = 32
請記住兩點。要確定您的Arduino連接了哪個串行端口,請查看Arduino草圖的右下角。不管是什么,都應(yīng)該是Python程序第3行中的引號。
您還可以更改Python程序第3行和Arduino程序的第2行中的波特率,只要它們保持不變即可。程序運行后,它將打印出大多數(shù)ASCII字符。首先將它們發(fā)送到Arduino,然后將其發(fā)送回Python,然后打印出來的計算機。
責(zé)任編輯:wv
-
python
+關(guān)注
關(guān)注
56文章
4812瀏覽量
85191 -
Arduino
+關(guān)注
關(guān)注
188文章
6478瀏覽量
188396
發(fā)布評論請先 登錄
相關(guān)推薦
華為云 Flexus X 實例下的場景體驗——小企業(yè)使用 Python 語言——超迅速搭建簡單公網(wǎng) API 接口服務(wù)

評論