[英]python + arduino controlling DC Motor
嗨,這是我的Arduino代碼,因為我只想要循環一次,所以我在void loop()中使用了while(1){}構造
int motorPin = 3;
int motorDir = 12;
int motorBr = 9;
void setup() {
//pinMode(motorPin, OUTPUT);
pinMode(motorBr, OUTPUT);
pinMode(motorDir, OUTPUT);
if (Serial.available() > 0) {
if(Serial.read() == '1') {
digitalWrite(motorBr, LOW);
digitalWrite(motorDir, HIGH);
digitalWrite(motorPin, HIGH);
delay(500);
digitalWrite(motorBr, HIGH);
} else if(Serial.read() == '0') {
digitalWrite(motorBr, LOW);
digitalWrite(motorDir, LOW);
digitalWrite(motorPin, HIGH);
delay(500);
digitalWrite(motorBr, HIGH);
}
}
}
void loop() { while(1) {}
}
這是我的python代碼
import serial
import time
ser = serial.Serial('COM3', 9600, timeout=1)
time.sleep(2)
#I am forcing the script to write 1 to Arduino to make the motor turn
ser.write(b'1')
ser.flush()
time.sleep(2)
ser.close()
通訊沒有發生。 任何見識都應該有所幫助。 我正在將Python 3.5和Arduino Uno與更新的驅動程序配合使用。
編輯:
嗨,朱利安,是的,以下代碼可以完成工作:
int motorPin = 3;
int motorDir = 12;
int motorBr = 9;
void setup() {
// put your setup code here, to run once:
//pinMode(motorPin, OUTPUT);
pinMode(motorBr, OUTPUT);
pinMode(motorDir, OUTPUT);
digitalWrite(motorBr, LOW);
digitalWrite(motorDir, HIGH);
digitalWrite(motorPin, HIGH);
delay(500);
digitalWrite(motorBr, HIGH);
delay(2000);
digitalWrite(motorBr, LOW);
digitalWrite(motorDir, LOW);
digitalWrite(motorPin, HIGH);
delay(500);
digitalWrite(motorBr, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
}
我還做了以下更改
ser.write('1') --> ser.write(b'1')
Serial.read() == 1 --> Serial.read() == '1'
Serial.read() == 1 --> Serial.read() == 0x31
似乎沒有任何作用。
我完成此操作的方法是先將Arduino程序上傳到內存,然后運行Python腳本。 也沒有錯誤顯示。
通過Python中的Subprocess調用執行Ardiono代碼:
import subprocess
actionLine = "upload"
projectFile = "C:/Users/Tomography/Desktop/DCM2/DCM2.ino"
portname = "COM3"
boardname = "arduino:avr:uno"
#I added the ardiono.exe to path, the command automatically sources the
Command = "arduino" + " --" + actionLine +" --board " + boardname + " --port " + portname + " " + projectFile
print(Command)
result = subprocess.call(Command)
if result != 0:
print("\n Failed - result code = %s --" %(result))
else:
print("\n-- Success --")
舊帖子,但我認為我會把我的發現發布在這里,以防將來有人看到。
在void setup()下的arduino文件中,確保包括
Serial.begin(9600);
否則將無法建立連接。
這是我用來在python中使用1或0打開和關閉電機的完整工作代碼:
Arduino代碼:
void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}
void loop() //This will be executed over and over
{
if (Serial.available() > 0) {
if(Serial.read() == '1') {
analogWrite(motorPin, 50);
}
else if(Serial.read() == '0') {
analogWrite(motorPin, 0);
}
}
}
Python代碼:
import serial
import time
ser = serial.Serial('COM3', 9600) //established connection
time.sleep(2)
ser.write(b'1') ##sends '1' to serial
time.sleep(5) ##motor runs for this period
ser.write(b'0') ##sends '0' to serial on arduino to turn motor off
ser.close()
嘗試這個 :
import serial
import time
ser = serial.Serial('COM3', 9600, timeout=1) #here you may add write_timeout=1 to avoid indefinite blocking on failing flush
time.sleep(2)
ser.write('1')
ser.flush() #force the physical write
#time.sleep(2) #no need to sleep as flush was blocking
ser.close()
對於Arduino代碼,對通訊的測試僅在設置功能中進行一次。 loop()與主循環中的while(1)等價,您可以從“正常” C代碼中知道。
這意味着一旦執行Python,您的arduino代碼就已經在loop()中的while(1)中,並且絕不會允許它分析串行數據。
正確的Arduino代碼為:
int motorPin = 3;
int motorDir = 12;
int motorBr = 9;
void setup() //this is executed only once at init
{
//pinMode(motorPin, OUTPUT);
pinMode(motorBr, OUTPUT);
pinMode(motorDir, OUTPUT);
}
void loop() //This will be executed over and over
{
if (Serial.available() > 0) {
// here '1' (the character) is important as 1 is the number
// and '1' equals 0x31 (ASCII)
if(Serial.read() == '1') {
digitalWrite(motorBr, LOW);
digitalWrite(motorDir, HIGH);
digitalWrite(motorPin, HIGH);
delay(500);
digitalWrite(motorBr, HIGH);
} else if(Serial.read() == '0') {
digitalWrite(motorBr, LOW);
digitalWrite(motorDir, LOW);
digitalWrite(motorPin, HIGH);
delay(500);
digitalWrite(motorBr, HIGH);
}
}
}
您的Python代碼正在發送字符串'1',但是您的arduino代碼正在尋找數字1。嘗試將arduino代碼更改為此
Serial.read() == 0x31
和
Serial.read() == 0x30
這些分別是“ 1”和“ 0”的ASCII碼
在您從python腳本發送字符時,setup()函數中的代碼很可能已經運行。
將代碼放在loop()函數中,然后在循環函數中放置一些邏輯,使其僅運行一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.