步驟1:安裝

下載后,打開終端并輸入:
tar xfvz /Users/*Account*/Downloads/pyserial-2.6.tar.gz
cd pyserial-2.6
sudo python setup.py install
為確保所有安裝正確的設備都打開空閑并輸入在“導入序列號”中。如果沒有錯誤出現,則一切正常。
您可以通過
ls /dev/tty.*
現在進行測試,將以下草圖上傳到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中創建一個新窗口并創建以下程序。
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草圖的右下角。不管是什么,都應該是Python程序第3行中的引號。
您還可以更改Python程序第3行和Arduino程序的第2行中的波特率,只要它們保持不變即可。程序運行后,它將打印出大多數ASCII字符。首先將它們發送到Arduino,然后將其發送回Python,然后打印出來的計算機。
責任編輯:wv
-
python
+關注
關注
57文章
4876瀏覽量
90025 -
Arduino
+關注
關注
190文章
6526瀏覽量
196914
發布評論請先 登錄
微店商品列表API接口指南
沒有專利的opencv-python 版本
淘寶商品詳情API接口技術解析與實戰應用
Python調用API教程
淘寶商品詳情接口(item_get)企業級全解析:參數配置、簽名機制與 Python 代碼實戰
termux如何搭建python游戲
python app不能運行怎么解決?
【Milk-V Duo S 開發板免費體驗】Milk-V DuoS之使用Arduino開發小核
《ESP32S3 Arduino開發指南》第二章 Arduino基礎知識
?如何在虛擬環境中使用 Python,提升你的開發體驗~
零基礎入門:如何在樹莓派上編寫和運行Python程序?
怎樣使用PySerial接口Python和Arduino
評論