我正在使用網絡控制流動站,並使用串行端口與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