dongque1958
2013-01-14 10:04
浏览 77
已采纳

Arduino fopen错误

I am trying to send a string through PHP to a serial port. But I get this error when I exectute it:

Warning: fopen(com8) [function.fopen]: failed to open stream: Permission denied in C:\server\web\arduino\index.php on line 15

<?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 script

// 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;
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douren2395 2013-01-15 00:52
    已采纳

    Try using the following, with the addition of + at the end of w (i.e. w+).

    'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

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

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

    点赞 打赏 评论
  • dpsu84620 2014-02-17 02:35

    sleep(2) after fopen() and "w+"

    $fp =fopen("com8", "w");
    sleep(2);
    
    点赞 打赏 评论

相关推荐 更多相似问题