嗨这是我的Arduino代码,因为我只需要循环一次,我在void loop()中使用while(1){}构造
<html>
<head></head>
<body>
<form>
<table id="pulseputtable">
<tr>
<td>
<label>Pulse rate : </label>
<input id="rate" type=text placeholder="xxx"> per minute <br><br>
<select id="age" name=age>
<option value="age1">0-12 months</option>
<option value="age2">1-18 years</option>
<option value="age3">18+ years</option>
</select>
<br><br>
<input type="button" value="Check" onclick="pulse()">
<br><br>
<label id = "check"></label>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function pulse(){
var c = document.getElementById("age");
var age = c.options[c.selectedIndex].value;
var rate = document.getElementById("rate");
switch(age){
case "age1":
if((rate>=115) && (rate<=130)){
document.getElementById("check").innerHTML="Normal";
}
else if(( rate<115)){
document.getElementById("check").innerHTML="Too low";
}
else{
document.getElementById("check").innerHTML="Too High";
}
break;
case "age2":
if((rate>=80) && (rate<=115)){
document.getElementById("check").innerHTML="Normal";
}
else if(( rate<80)){
document.getElementById("check").innerHTML="Too low";
}
else{
document.getElementById("check").innerHTML="Too High";
}
break;
case "age3":
if((rate>=60) && (rate<=80)){
document.getElementById("check").innerHTML="Normal";
}
else if(( rate<60)){
document.getElementById("check").innerHTML="Too low";
}
else{
document.getElementById("check").innerHTML="Too High";
}
break;
}
}
</script>
</body>
</html>
这是我的python代码
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 3.5和Arduino Uno与更新的驱动程序。
编辑:
嗨Julien,是的,以下代码完成了它的工作:
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()
我也进行了以下更改
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:
}
似乎没有任何效果。
我实现这一点的方法是首先将Arduino程序上传到内存,然后运行Python脚本。没有错误显示..
通过Python中的Subprocess调用执行Ardiono代码:
ser.write('1') --> ser.write(b'1')
Serial.read() == 1 --> Serial.read() == '1'
Serial.read() == 1 --> Serial.read() == 0x31
答案 0 :(得分:1)
以前的帖子,但我认为我会把我的发现发布出来,以防将来有人看到。
在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()
答案 1 :(得分:0)
试试这个:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var websayfasi: UIWebView!
var urlpath = "http://google.com.tr "
func loadAdressURL(){
let requestURL = NSURL (string:urlpath)
let request = NSURLRequest(URL: requestURL!)
websayfasi.loadRequest(request)
self.view.addSubview(websayfasi)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
对于Arduino代码,通信测试只发生一次,因为它位于设置功能中。 loop()等效于主循环中的while(1),您可以从“普通”C代码中获知。
这意味着一旦执行Python,你的arduino代码就已经在while(1)in循环()中,并且永远不会分析串行数据。
正确的Arduino代码是:
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()
答案 2 :(得分:0)
您的Python代码正在发送字符串'1',但您的arduino代码正在寻找数字1.尝试将arduino代码更改为此
Serial.read() == 0x31
和
Serial.read() == 0x30
分别是'1'和'0'的ASCII代码
你的setup()函数中的代码很可能已经在你从python脚本发送字符时运行了。
将代码放在loop()函数中,然后在循环函数中放置一些逻辑,使它只运行一次。