通过 USB COM PORT 从 Arduino UNO 检索数据 [英] Retrieving data from Arduino UNO via USB COM PORT

查看:55
本文介绍了通过 USB COM PORT 从 Arduino UNO 检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Arduino UNO 开展一个项目.我的任务是从 Arduino 接收数据并使用 PHP 将其保存到数据库中(首先,我试图在 php 代码中获取数据,保存到数据库非常容易).

I am working on a project with Arduino UNO. My task is to receive data from the Arduino and save it to the database using PHP (first, I am trying to get data in php code, saving to database is quite easy).

我使用的是 Windows 操作系统.使用WAMP,php版本:5.5.12

I am using Windows operating system. Using WAMP, php version: 5.5.12

问题是:我不知道如何通过 COM 端口正确读取数据.

The problem is: I cannot figure out how to properly read data via COM port.

Arduino 示例代码:

Arduino sample code:

float temp;
int tempPin = 0;

void setup()
{
 Serial.begin(9600);
 temp = analogRead(tempPin);
 temp = temp * 0.48828125;
 Serial.print(temp);
}

void loop()
{

}

背后的PHP代码(arduino使用的是COM3):

PHP code behind (arduino is using COM3):

<?php

   exec("MODE COM3: BAUD=9600 PARITY=N DATA=8 STOP=1");

    $portAddress = 'COM3:';

    // Open connection on port
    $port = fopen($portAddress, 'rb+');

    stream_set_timeout($port, 0, 100);
  stream_set_blocking($port,0);

    // Necessary when the Arduino reset after the connection
    sleep(2);


    $msg = fread($port, 1);

    // Close connection
    fclose($port);

    echo $msg;
?>

问题是,当我第一次运行 php 代码时,它看起来像是在不停地循环并且没有打开连接.如果我尝试刷新页面,我会得到

the problem is that when I first run the php code, it looks like it is looping non stop and not opening the connection. If I try to refresh the page, I get

我曾尝试在论坛上查找其他问题,但找不到类似的情况.有人可以指导我在php代码中获取值的解决方案吗?

I have tried looking up other questions on the forum but I could not find a similar situation. Could someone guide me to the solution of taking the value in php code?

推荐答案

首先,我要补充:

delay(1000);
Serial.write(temp);

到 Arduino 草图中的主程序循环,为什么?- 没有这个,你的程序只会写一次变量 temp ,然后永远什么都不做,所以添加它以使测试更容易.

To your main program loop in the Arduino sketch, why? - Without this, your program will just write the variable temp once and then do nothing forever, so add that to make testing easier.

接下来,您的 PHP 代码.看起来很简单,您是否使用提升的权限运行 WAMP 服务器?PHP 正在尝试在硬件级别访问 Windows 中的 COM,您需要提升权限.您正在使用 rb+ 所以传输处理被禁用,它可能证明可以通过不同的操作类型工作,因为 PHP 和机器之间存在层,例如可能篡改数据的 WAMP 服务器.您的时间无法正常工作,系统不同步,没有握手就无法预测 Arduino 的传输,您需要循环并等待接收到传输后再进行处理,这可能会对 WAMP 造成严重破坏服务器.

Next, your PHP code. It seems like something simple, are you running the WAMP server with elevated permissions? PHP is trying to access COM in windows on the hardware level, you'll need elevated permissions. You're using rb+ so transmission processing is disabled, it might prove to work through the different action types, because there are layers between PHP and the machine, like the WAMP server that could be tampering with the data. Your timing isn't going to work, the system isn't synchronous, the Arduino's transmission can't be anticipated without a handshake, you need to loop and wait until a transmission is received before processing it, which might wreak havoc with the WAMP server.

这篇关于通过 USB COM PORT 从 Arduino UNO 检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆