我正在尝试通过PHP将字符串发送到串行端口。但是当我执行它时出现此错误:
警告:fopen(com8) [function.fopen]:无法打开流:第 15 行 C:\server\web\arduino\index.php 中的权限被拒绝
<?php
if(isset($_GET['command'])){
$fp =fopen("com8", "w");
$i = ($_GET['command'] > 0) ? $_GET['command'] : 0;
echo "<p>Wrote $i </p>";
fwrite($fp, chr($i));
sleep(3);
fclose($fp);
}
?>
Arduino 脚本
// Includes
#include <Servo.h>
// Aanmaken van de variabelen voor in de code
int ledPin = 13;
int motorPin = 12;
int USBnumber = 0;
Servo stuurServo; // Create servo object to control a servo.
int pos = 90; // Variable to store the servo position.
// Make the first setup.
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(motorPin, OUTPUT);
stuurServo.attach(11);
Serial.begin(9600);
stuurServo.write(pos);
}
void loop()
{
if (Serial.available() > 0) {
USBnumber = Serial.read();
}
if (USBnumber > 0) {
if (USBnumber == 1){ // Blink LED
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
}else if(USBnumber == 2){ // Motor on for five seconds
digitalWrite(motorPin, HIGH);
delay(5000);
digitalWrite(motorPin, LOW);
delay(500);
}else if(USBnumber == 3){ // Control servo +10 graden
if(pos != 180){
pos + 10;
stuurServo.write(pos);
delay(500);
}
}else if(USBnumber == 4){ // Control servo -10 graden
if(pos != 0){
pos - 10;
stuurServo.write(pos);
delay(500);
}
}else if(USBnumber == 5){ // Control servo left
pos = 0;
stuurServo.write(pos);
delay(500);
}else if(USBnumber == 6){ // Control servo right
pos = 180;
stuurServo.write(pos);
delay(500);
}else{
delay(500);
}
USBnumber = 0;
}
}