2

我正在尝试通过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;
    }
}
4

2 回答 2

2

尝试使用以下内容,并在w的末尾添加+(即w+)。

'w+' - 打开读写;将文件指针放在文件的开头并将文件截断为零长度。如果该文件不存在,请尝试创建它。

$fp = fopen("com8", "w+");

见: http: //php.net/manual/en/function.fopen.php

于 2013-01-15T00:52:50.173 回答
1

在 fopen() 和 "w+" 之后 sleep(2)

$fp =fopen("com8", "w");
sleep(2);
于 2014-02-17T02:35:53.373 回答