python+arduino控制DC M

2024-09-30 16:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,这是我的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代码

^{pr2}$

通讯没有进行。任何洞察力都会有所帮助。我正在使用python3.5和Arduino Uno和更新的驱动程序。在

编辑:

你好,Julien,是的,下面的代码可以完成它的工作:

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中通过子进程调用执行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 --")

Tags: 代码readoutputifserialintlowdelay
3条回答

您的Python代码正在发送字符串“1”,但是您的arduino代码正在查找数字1。尝试将arduino代码改为

Serial.read() == 0x31

以及

^{pr2}$

它们分别是“1”和“0”的ASCII代码

从python脚本发送字符时,setup()函数中的代码很可能已经运行。在

将代码放在loop()函数中,然后在loop函数中放置一些逻辑,使其只运行一次。在

试试这个:

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代码中知道。在

Reference manual for setup

Reference manual for loop

这意味着一旦执行Python,arduino代码就已经在while(1)in loop()中,并且永远不允许分析串行数据。在

正确的Arduino代码为:

^{pr2}$

但我想我会把我的发现贴在上面,以防将来有人看到。在

在void setup()下的arduino文件中,确保包括

Serial.begin(9600);

否则无法建立连接。在

以下是我在python中使用1或0打开和关闭电机的完整工作代码:

Arduino代码:

^{pr2}$

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()

相关问题 更多 >