2009-05-22 129 views
4

我正在使用网络控制流动站,并使用串行端口与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> 

回答

1

如果是C,那么你有分配,而不是在两个测试中比较,所以都是true,所以所有的写操作完成每一次。编译为高警告级别(如GCC中的-Wall -pedantic)。试试这个:


int a = 0; 
if (a == 1) printf("a is not one: %d\n", a); 
if (a = 1) printf("a is one: %d\n", a); 

从您发布的PHP代码(我不是一个专家),它看起来,你是作为一个字符,这是不ASCII 49写入二进制1,但ASCII 1(SOH),同对于2.尝试将其更改为在PHP代码'1'

这里的一些文章的链接上Controlling the Serial Port with PHP(C代码或1) - 我用Google搜索,没有质量的想法 - 但看起来并不像它足以只是写入一个整数到“com1” - 这是我的域名,所以祝你好运:)

+0

我改变了这一点,现在led灯根本不亮。这意味着它不是正确读取ascii 1或2吗? – blueintegral 2009-05-22 18:17:01

+0

您可以记录/打印您收到的内容吗? 也发布你的PHP代码 - 这将有助于缩小问题的范围。 – 2009-05-22 18:20:16

+0

如果我尝试使用内置于arduino环境中的串行监视器,PHP会抱怨com端口无法写入, – blueintegral 2009-05-22 18:32:51

1

正如尼古拉提到,它看起来像你正在做的任务(=)鼠她比你的“if”语句中的比较(==)。

一个好习惯,一些C程序员进入是把右值就比较的左侧,这样编译器会产生一个错误,如果你不小心使用了赋值运算符,而不是比较操作:

 
if (50 == usbnumber) { // This is okay. 
    ... 
} 

if (50 = usbnumber) { // The compiler will generate an error here. 
    ... 
} 

不管你使用什么样的编译器标志或警告级别,它都可以工作,因为赋值给右值是非法的。

我应该补充说,如果你需要比较两个左值,这个“安全网”不起作用。

0

可能为时已晚,但我认为你的问题是serial.read()一次只能读取一个字符。如果你从PC发送“49”,当你呼叫usbnumber = serial.read()你会得到第一个循环的“4”和第二个循环的“9”。这些都不满足条件,所以没有做任何事情和usbnumber被重置为0.

要修复,您可以更改序列号。可条件是

if (Serial.available() == 2) 

,然后做一些类似下面的转换为数字:

usbnumber = Serial.read() * 10 + Serial.read(); 

另一种选择是使用TextFinder库 - 我已经在我的网站上写了一个简短的教程http://mechariusprojects.com/thunderbolt/?p=60

机甲

0

,我发现你的答案寻找其他的东西,反正我只是面对(我猜)你有同样的问题。

如果你还没有解决它,我认为问题不是你的代码,而是arduino板上的自动重置机制。 也就是说:每次在串口上建立一个新的连接时,arduino板都会复位,这允许在通过串口编程时加载新的固件。

要验证这一点,请尝试在setup()函数中闪烁LED,如果每次加载页面时闪烁,这都证实了我的论点。

看一看这里:http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

我通过坚持+ 5V和RESET之间的120欧姆的电阻来解决。只要记住每次你想上传一个新的固件到你的主板时就把它删除。