php PHP串口数据从Arduino返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13114275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP serial port data return from Arduino
提问by sisko
I wonder if there is a way to accomplish reading my serial port via PHP - that works :-)
我想知道是否有办法通过 PHP 完成读取我的串行端口 - 有效:-)
In practising my Arduino skills, I developed a simple LED ON/OFF sketch. It works by entering onor offin the serial monitor.
在练习我的 Arduino 技能时,我开发了一个简单的 LED 开/关草图。它通过在串行监视器中输入打开或关闭来工作。
Next step, I put together a webpage to act as an GUI interface to click a link and perform the on and off function above. This webbased GUI works via PHP. I am using the PHP SERIALclass to interact with the serial port my Arduino is using.
下一步,我整理了一个网页作为 GUI 界面来单击链接并执行上面的开和关功能。这个基于 Web 的 GUI 通过 PHP 工作。我正在使用PHP SERIAL类与我的 Arduino 使用的串行端口进行交互。
The issue is I need to find a way of getting feedback from the serial port. Using the Arduino IDE serial monitor, I can see my printed messages in response to each of my serial input and I need to retrieve the same feedback in my PHP code.
问题是我需要找到一种从串行端口获取反馈的方法。使用 Arduino IDE 串行监视器,我可以看到响应我的每个串行输入的打印消息,并且我需要在我的 PHP 代码中检索相同的反馈。
The PHP Serial class offers a readPort()function but I does not return my data.
PHP Serial 类提供了一个readPort()函数,但我不返回我的数据。
UPDATE[2]:
更新[2]:
ARDUINO:
阿杜诺:
const int greenPin = 2;
const int bluePin = 3;
const int redPin = 4;
int currentPin = 0; //current pin to be faded
int brightness = 0; //current brightness level
void setup(){
Serial.begin(9600);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redPin, OUTPUT);
}
void loop(){
//if there's any serial data in the buffer, read a byte
if( Serial.available() > 0 ){
int inByte = Serial.read();
//respond only to the values 'r', 'g', 'b', or '0' through '9'
if(inByte == 'r')
currentPin = redPin;
if(inByte == 'g')
currentPin = greenPin;
if(inByte == 'b')
currentPin = bluePin;
if(inByte >= '0' && inByte <= '9'){
//map the incoming byte value to the range of the analogRead() command
brightness = map(inByte, '0', '9', 0, 255);
//set the current pin to the current brightness:
analogWrite(currentPin, brightness);
}
Serial.print("Current Pin : ");
Serial.println(currentPin);
Serial.print("Brightness : ");
Serial.println(brightness);
}//close serial check
}
PHP/HTML:
PHP/HTML:
<?php
require("php_serial.class.php");
// include("php_serial.class.php");
// Let's start the class
$serial = new phpSerial();
// First we must specify the device. This works on both Linux and Windows (if
// your Linux serial device is /dev/ttyS0 for COM1, etc.)
$serial->deviceSet("/dev/ttyACM0");
// Set for 9600-8-N-1 (no flow control)
$serial->confBaudRate(9600); //Baud rate: 9600
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1")
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1")
$serial->confFlowControl("none");
// Then we need to open it
$serial->deviceOpen();
// Read data
$read = $serial->readPort();
print "<pre>";
print_r($read);
print "</pre>";
// Print out the data
echo $read;
// print exec("echo 'r9g9b9' > /dev/ttyACM0");
print "RESPONSE(1): {$read}<br><br>";
// If you want to change the configuration, the device must be closed.
$serial->deviceClose();
?>
<?php
if( isset($_REQUEST['LED']) )
response();
?>
<form action='index.php' method='POST'>
<select id='led' name='LED'>
<option id='nil'>-</option>
<option id='red'>RED</option>
<option id='green'>GREEN</option>
<option id='blue'>BLUE</option>
<option id='all'>ALL</option>
</select>
<input type='submit' value='SET'>
</form>
<?php
print "Hi, Earthlings!";
function response(){
$CMDString = "";
$execute = false;
if( isset($_REQUEST['LED']) ){
switch ($_REQUEST['LED']) {
case 'RED':
$CMDString = 'r9';
$execute = true;
exec("echo 'r9g0b0' > /dev/ttyACM0");
print "<br>FOUND: {$_REQUEST['LED']}";
break;
case 'GREEN':
$CMDString = 'g9';
$execute = true;
print "<br>FOUND: {$_REQUEST['LED']}";
break;
case 'BLUE':
$CMDString = 'b9';
$execute = true;
print "<br>FOUND: {$_REQUEST['LED']}";
break;
case 'ALL':
$CMDString = 'r9g9b9';
$execute = true;
print "<br>FOUND: {$_REQUEST['LED']}";
break;
default:
print exec("echo 'r0g0b0' > /dev/ttyACM0");
$execute = false;
break;
}
if($execute){
print exec("echo '{$CMDString}' > /dev/ttyACM0");
print "<br><br>executing: {$CMDString}";
}
}
}
?>
采纳答案by Lukasz Kujawa
I assume you work on linux.
我假设你在 linux 上工作。
First setup your serial port:
首先设置你的串口:
stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
Then you can use good old fashion fread/fwrite
然后你可以使用好的老式 fread/fwrite
$fp =fopen("/dev/ttyACM0", "w+");
if( !$fp) {
echo "Error";die();
}
fwrite($fp, $_SERVER['argv'][1] . 0x00);
echo fread($fp, 10);
fclose($fp);
There is only one thing you have to remember. Arduino will restart on every connection. If you don't know that It will confuse you. For instance if you connect (fopen) and instantly send data Arduino will miss it because it's booting (which takes a sec or two). Experiment with sleep to give it some time. If you want to disable restarting use 10uF capacitor from GRD to RST.
你只需要记住一件事。Arduino 将在每次连接时重新启动。如果你不知道它会让你感到困惑。例如,如果您连接 (fopen) 并立即发送数据,Arduino 将错过它,因为它正在启动(需要一两秒)。尝试睡眠,给它一些时间。如果要禁用重新启动,请使用从 GRD 到 RST 的 10uF 电容器。
Good luck
祝你好运
ps. you can troubleshoot with "screen"
附:您可以使用“屏幕”进行故障排除
screen /dev/ttyACM0 9600
Post about setting PHP with Arduino http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/and one more here http://systemsarchitect.net/arduino-and-php-serial-communication-with-a-protocol/.
发布有关使用 Arduino 设置 PHP 的信息http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/在这里还有一个http://systemsaritect.net/arduino-and-php-串行通信协议/。
回答by Lukasz Kujawa
/dev/ttyUSB0 running with user root, if your using apache try chown /dev/ttyUSB0 to apache or user is logged in.
/dev/ttyUSB0 以 root 用户运行,如果您使用 apache 尝试 chown /dev/ttyUSB0 到 apache 或用户已登录。
$ sudo chown apache2:apache2 /dev/ttyACM0
OR
$ sudo chown yourusername:yourusername /dev/ttyACM0
and then try again, or try logout from ubuntu and login as root user.
然后再试一次,或者尝试从 ubuntu 注销并以 root 用户身份登录。
回答by opc0de
You are probably trying to read when is no data on the serial port. You need to implement JavaScriptcode to call PHPcode that can read at regular intervals.
您可能正在尝试读取串行端口上没有数据的情况。您需要实现JavaScript代码来调用可以定期读取的PHP代码。
When you have got data, you should process it.
当你有数据时,你应该处理它。
readPort()has worked fine for me. If the baud rate, parity, etc. is adjusted properly, then you should not have any problem reading the serial port.
readPort()对我来说效果很好。如果波特率、奇偶校验等调整得当,那么读取串口应该没有问题。
Here is a sample of using the library for Arduino. It worked for me some time ago:
这是使用 Arduino 库的示例。前一段时间它对我有用:
<?php
include "php_serial.class.php";
// Let's start the class
$serial = new phpSerial();
// First we must specify the device. This works on both Linux and Windows (if
// your Linux serial device is /dev/ttyS0 for COM1, etc.)
$serial->deviceSet("/dev/ttyUSB0");
// Set for 9600-8-N-1 (no flow control)
$serial->confBaudRate(9600); //Baud rate: 9600
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1")
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1")
$serial->confFlowControl("none");
// Then we need to open it
$serial->deviceOpen();
// Read data
$read = $serial->readPort();
// Print out the data
echo $read;
// If you want to change the configuration, the device must be closed.
$serial->deviceClose();
?>

