步驟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
請記住兩點(diǎn)。要確定您的Arduino連接了哪個串行端口,請查看Arduino草圖的右下角。不管是什么,都應(yīng)該是Python程序第3行中的引號。
您還可以更改Python程序第3行和Arduino程序的第2行中的波特率,只要它們保持不變即可。程序運(yùn)行后,它將打印出大多數(shù)ASCII字符。首先將它們發(fā)送到Arduino,然后將其發(fā)送回Python,然后打印出來的計(jì)算機(jī)。
責(zé)任編輯:wv
-
python
+關(guān)注
關(guān)注
56文章
4848瀏覽量
88915 -
Arduino
+關(guān)注
關(guān)注
190文章
6508瀏覽量
194704
發(fā)布評論請先 登錄
淘寶商品詳情接口(item_get)企業(yè)級全解析:參數(shù)配置、簽名機(jī)制與 Python 代碼實(shí)戰(zhàn)

液晶顯示屏接口怎樣選?
【Milk-V Duo S 開發(fā)板免費(fèi)體驗(yàn)】Milk-V DuoS之使用Arduino開發(fā)小核
STM32開發(fā)板一般都會配置arduino接口,是不是基本上也都會支持arduino?
DLP6500能否用Python編程進(jìn)行開發(fā),是否有API接口?
使用Python實(shí)現(xiàn)xgboost教程
I2C總線與Arduino的接口示例
華為云 Flexus X 實(shí)例下的場景體驗(yàn)——小企業(yè)使用 Python 語言——超迅速搭建簡單公網(wǎng) API 接口服務(wù)

評論