通过 web 与客户端串口通信
Communicateur with client-sided serial port through web
我的 PHP 网站有问题(它使用 API,所以它必须是 PHP)。
本网站是在 raspberry pi b+ 上启动的,它连接到热敏打印机(通过串口),我使用了 python 脚本来测试打印机。
现在我的问题是:是否可以通过网络发送数据让树莓派打印一些数据?因此,向客户端发送一条指令,如写入端口“/dev/ttyxxx”?
感谢您的帮助
如果你的意思是:"I have a PHP application that needs to access the server's serial port":
PHP 可以访问服务器上的串行端口(在这种情况下,您的 raspberry pi)。 PHP 将其视为普通文件。
<?php
// Set timeout to 500 ms
$timeout=microtime(true)+0.5;
// Set device controle options (See man page for stty)
exec("/bin/stty -F /dev/ttyS0 19200 sane raw cs8 hupcl cread clocal -echo -onlcr ");
// Open serial port
$fp=fopen("/dev/ttyS0","c+");
if(!$fp) die("Can't open device");
// Set blocking mode for writing
stream_set_blocking($fp,1);
fwrite($fp,"foo\n");
// Set non blocking mode for reading
stream_set_blocking($fp,0);
do{
// Try to read one character from the device
$c=fgetc($fp);
// Wait for data to arive
if($c === false){
usleep(50000);
continue;
}
$line.=$c;
}while($c!="\n" && microtime(true)<$timeout);
echo "Responce: $line";
?>
如果你的意思是:"I have a website that somehow needs to send something to the client's serial port"
那么唯一的解决办法就是浏览器App。
chrome 个应用程序可以使用 Chrome Serial API。
Video Example
我想到了几个解决方案;基本上你会希望你的 php-page 解析数据并创建一个 trusted 可以打印的输出(即 PDF 文件,如果你的打印机支持的话)。
您的下一个任务是如何将此可信 输出发送到打印机。同样,存在多种解决方案。
让您的 php-脚本执行系统可执行文件,例如cat output.pdf > /dev/ttyxxx
(这里很清楚,我不知道如何从 unix 打印)。请注意,可执行文件根本 不依赖于输入,即您希望降低注入攻击等的风险。这一点要求您创建的 output.pdf 是值得信赖的。
使用 cron-job 查找输出文件并将它们发送到打印机。适用与上述相同的注意事项。这可能会更好,因为如果多个 php 会话正在尝试打印文档,它可以避免瓶颈。
围绕上述构建一个较小的框架,如果发生错误等可以报告。但是,基本上选项 1 + magic。
总而言之,将过程分为两个步骤。接受输入、解析和检查 erroneousness/malevolent 输入并为打印机创建所需输出的一种。这可以在受保护的环境中完成,如果被黑客入侵,不会暴露系统(至少不会超过通常的 php )。
然后第 2 步负责将输出发送到硬件,可以是 bash-脚本、可执行文件或 python.
我的 PHP 网站有问题(它使用 API,所以它必须是 PHP)。
本网站是在 raspberry pi b+ 上启动的,它连接到热敏打印机(通过串口),我使用了 python 脚本来测试打印机。
现在我的问题是:是否可以通过网络发送数据让树莓派打印一些数据?因此,向客户端发送一条指令,如写入端口“/dev/ttyxxx”?
感谢您的帮助
如果你的意思是:"I have a PHP application that needs to access the server's serial port": PHP 可以访问服务器上的串行端口(在这种情况下,您的 raspberry pi)。 PHP 将其视为普通文件。
<?php
// Set timeout to 500 ms
$timeout=microtime(true)+0.5;
// Set device controle options (See man page for stty)
exec("/bin/stty -F /dev/ttyS0 19200 sane raw cs8 hupcl cread clocal -echo -onlcr ");
// Open serial port
$fp=fopen("/dev/ttyS0","c+");
if(!$fp) die("Can't open device");
// Set blocking mode for writing
stream_set_blocking($fp,1);
fwrite($fp,"foo\n");
// Set non blocking mode for reading
stream_set_blocking($fp,0);
do{
// Try to read one character from the device
$c=fgetc($fp);
// Wait for data to arive
if($c === false){
usleep(50000);
continue;
}
$line.=$c;
}while($c!="\n" && microtime(true)<$timeout);
echo "Responce: $line";
?>
如果你的意思是:"I have a website that somehow needs to send something to the client's serial port" 那么唯一的解决办法就是浏览器App。
chrome 个应用程序可以使用 Chrome Serial API。 Video Example
我想到了几个解决方案;基本上你会希望你的 php-page 解析数据并创建一个 trusted 可以打印的输出(即 PDF 文件,如果你的打印机支持的话)。
您的下一个任务是如何将此可信 输出发送到打印机。同样,存在多种解决方案。
让您的 php-脚本执行系统可执行文件,例如
cat output.pdf > /dev/ttyxxx
(这里很清楚,我不知道如何从 unix 打印)。请注意,可执行文件根本 不依赖于输入,即您希望降低注入攻击等的风险。这一点要求您创建的 output.pdf 是值得信赖的。使用 cron-job 查找输出文件并将它们发送到打印机。适用与上述相同的注意事项。这可能会更好,因为如果多个 php 会话正在尝试打印文档,它可以避免瓶颈。
围绕上述构建一个较小的框架,如果发生错误等可以报告。但是,基本上选项 1 + magic。
总而言之,将过程分为两个步骤。接受输入、解析和检查 erroneousness/malevolent 输入并为打印机创建所需输出的一种。这可以在受保护的环境中完成,如果被黑客入侵,不会暴露系统(至少不会超过通常的 php )。 然后第 2 步负责将输出发送到硬件,可以是 bash-脚本、可执行文件或 python.