我正在使用网络控制流动站,并使用串行端口与Arduino进行通信。我写了一些只使用fwrite()
的PHP,并将一个ASCII 1或一个ASCII 2写入串口。 Arduino正在听该端口,并根据听到的内容做一些事情。我知道我的PHP正在工作,因为每当我告诉它发送东西时,Arduino都会收到它。这里是Arduino代码:Arduino串行读取
//This listens to the serial port (USB) and does stuff based on what it is hearing.
int motor1Pin = 13; //the first motor's port number
int motor2Pin = 12; //the second motor's port number
int usbnumber = 0; //this variable holds what we are currently reading from serial
void setup() { //call this once at the beginning
pinMode(motor1Pin, OUTPUT);
//Tell arduino that the motor pins are going to be outputs
pinMode(motor2Pin, OUTPUT);
Serial.begin(9600); //start up serial port
}
void loop() { //main loop
if (Serial.available() > 0) { //if there is anything on the serial port, read it
usbnumber = Serial.read(); //store it in the usbnumber variable
}
if (usbnumber > 0) { //if we read something
if (usbnumber = 49){
delay(1000);
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW); //if we read an ASCII 1, stop
}
if (usbnumber = 50){
delay(1000);
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, HIGH); //if we read an ASCII 2, drive forward
}
usbnumber = 0; //reset
}
}
所以这应该是相当直接的。现在,当我发送ASCII 1或ASCII 2时,我正在测试的LED(在引脚13上)亮起并保持打开状态。但是,如果我发送另一个ASCII 1或2,它将关闭,然后再打开。我们的目标是只有当ASCII 1是最后发送的东西并保持到2是最后发送的东西时才打开它。
编辑:这是我的PHP:
<?php
$verz="0.0.2";
$comPort = "com3"; /*change to correct com port */
if (isset($_POST["rcmd"])) {
$rcmd = $_POST["rcmd"];
switch ($rcmd) {
case Stop:
$fp =fopen($comPort, "w");
fwrite($fp, chr(1)); /* this is the number that it will write */
fclose($fp);
break;
case Go:
$fp =fopen($comPort, "w");
fwrite($fp, chr(2)); /* this is the number that it will write */
fclose($fp);
break;
default:
die('???');
}
}
?>
<html>
<head><title>Rover Control</title></head>
<body>
<center><h1>Rover Control</h1><b>Version <?php echo $verz; ?></b></center>
<form method="post" action="<?php echo $PHP_SELF;?>">
<table border="0">
<tr>
<td></td>
<td>
</td>
<td></td>
</tr>
<tr>
<td>
<input type="submit" value="Stop" name="rcmd"><br/>
</td>
<td></td>
<td>
<input type="submit" value="Go" name="rcmd"><br />
</td>
</tr>
<tr>
<td></td>
<td><br><br><br><br><br>
</td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
我改变了这一点,现在led灯根本不亮。这意味着它不是正确读取ascii 1或2吗? – blueintegral 2009-05-22 18:17:01
您可以记录/打印您收到的内容吗? 也发布你的PHP代码 - 这将有助于缩小问题的范围。 – 2009-05-22 18:20:16
如果我尝试使用内置于arduino环境中的串行监视器,PHP会抱怨com端口无法写入, – blueintegral 2009-05-22 18:32:51