回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>嗨,这是我的Arduino代码,因为我只需要循环一次,所以我在void loop()中使用了while(1){}构造</p>
<pre><code>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) {}
}
</code></pre>
<p>这是我的python代码</p>
^{pr2}$
<p>通讯没有进行。任何洞察力都会有所帮助。我正在使用python3.5和Arduino Uno和更新的驱动程序。在</p>
<p>编辑:</p>
<p>你好,Julien,是的,下面的代码可以完成它的工作:</p>
<pre><code>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:
}
</code></pre>
<p>我还做了以下更改</p>
<pre><code>ser.write('1') --> ser.write(b'1')
Serial.read() == 1 --> Serial.read() == '1'
Serial.read() == 1 --> Serial.read() == 0x31
</code></pre>
<p>似乎没有任何效果。在</p>
<p>我的方法是首先将Arduino程序上传到内存中,然后运行Python脚本。也没有出现错误。。在</p>
<p>在Python中通过子进程调用执行Ardiono代码:</p>
<pre><code>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 --")
</code></pre>