douhunbei0166 2014-10-13 12:14
浏览 61

从php到arduino的串行通信

i want to pass a number from php to arduino. currently, i am able to communicate between php and arduino via serial port. however, when i try to pass a number(int) from php to arduino, the arduino can't identify it. then i try to pass an character(char) it is able to detect. i have tried multiple times and i don't know what is wrong with the code. below is my code.

PHP CODE

$X = $_POST['x'];
$Y = $_POST['y'];

$query = "INSERT INTO target_position (X,Y,Date) VALUES('$X','$Y',now())";
$insert = mysql_query($query);

$serial = new phpSerial();
$serial ->deviceSet("COM6");    //Port number
$serial ->confbaudRate(9600);
$serial ->confParity("none");
$serial ->confCharacterLength(8);
$serial ->confStopBits(1);
$serial ->confFlowControl("none");
$serial ->deviceOpen();
$serial ->sendMessage('1');
//$serial ->sendMessage('$Y');
//$jinhong =$serial ->readPort();
$serial ->deviceClose();

//echo $jinhong;

here is the arduino code

int led =13;
int test=0;

void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}
void loop()
{
   while(Serial.available()==0);
   test=Serial.read();

   if(test=='1')
   {
      digitalWrite(led,HIGH);
      delay(500);
      digitalWrite(led,LOW);
      delay(500);
      digitalWrite(led,HIGH);
      delay(500);
      digitalWrite(led,LOW);
   }
}

Anyone can tell me what is wrong? The led doesn't light up at when i send 1. however when i send 'A', the led is up.

  • 写回答

2条回答 默认 最新

  • dshgdhdfcas30210 2014-10-13 22:37
    关注

    as a quick check can you do this to make sure you're dealing with the right data types (i.e. if you're sending a string or an int and checking for either or.

    void loop(){
    
        if (Serial.available()) {
    
            char ch = Serial.read();
    
            if(ch == '1'){
    
                // your led logic here
    
            }
    
        }
    
    }
    

    ok now to try to cut the long story short. i tested this stuff myself and the following code is functional. What I noticed was that the serial monitor in the arduino IDE needed to be open for this to work as expected. When it wasn't open the correct data wasnt being read nor was the PHP script exiting. I'm thinking the problem here is that your arduino environment is trying to communicate to the device over the same serial port that your PHP is trying to talk over too. You'll probably have to investigate how to disable that communication while youre trying to use your PHP script(s)

    <?php
    include 'PhpSerial.php';
    
    $serial = new PhpSerial;
    
    $serial->deviceSet("/dev/tty.usbserial-A9IL9BF7");
    
    $serial->confBaudRate(9600);
    $serial->confParity("none");
    $serial->confCharacterLength(8);
    $serial->confStopBits(1);
    $serial->confFlowControl("none");
    
    $serial->deviceOpen();
    
    $read = $serial->readPort();
    
    $serial ->sendMessage('1');
    
    $serial ->deviceClose();
    
    ?>
    
    
    int led = 13;
    
    void setup() {
        pinMode(led, OUTPUT);
        Serial.begin(9600);     
    }
    
    void loop() {
    
        if (Serial.available() == 1) {
    
            char ch = Serial.read();
    
            if (ch == '1'){
    
                digitalWrite(led,HIGH);
                delay(500);
                digitalWrite(led,LOW);
                delay(500);
                digitalWrite(led,HIGH);
                delay(500);
                digitalWrite(led,LOW);
    
            }
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧