php - 从 RS232 读取并发送命令 trought php url
问题描述
我需要创建一个项目,根据从 RS232 端口读取的信息,我需要执行适当的 url 来更新 REMOVE 服务器上的 mysql 数据库。
我认为最好的硬件是连接到互联网的树莓派或迷你电脑。这是真的吗?
唯一的问题是我不知道如何开始为此目的编写脚本。什么语言?
我只使用 php/javascript :(
例如,如果我通过 RS232 端口接收数据:
110111 ------> then execute url http://www.test.com/update.php?id=5&qty=1
100011 ------> then execute url http://www.test.com/update.php?id=6&qty=3
etc...
Url 仅用于更新网络服务器上的 mysql 数据库,所以我不需要得到任何结果。
任何人都可以建议我如何开始,如果有一些适合我的目的的例子,在线?
更新:我试图更好地解释我的目的:我们有一个现金箱,在哪里打印收据时,还发送有关所购买产品的信息,通过 rs232 输出。例如 PRODUCT XX, QUANTITY 5, PRICE 40.00, ID = 1 有了这些信息,我需要同步/更新一个在线 mysql 数据库。在线更新我的数据库的唯一方法是执行/访问 url,例如:http ://www.test.com/update.php?id=1&qty= 5 所以...我知道 PHP 无法读取低级硬件,所以我可以'不要直接用 PHP 读取 com 端口。我的想法是连接一个覆盆子(带有一个 rs232 端口)并创建一个读取 rs232 并基于收到的信息(我使用的 ID 和 QUANTITY)的 pyton 脚本,创建适当的 url 并执行它。
解决方案
如果你想使用 PHP,我发现了一些东西,但该网站是德语的: https ://www.mikrocontroller.net/topic/101642
我会尝试为您翻译代码和注释。
<?php
//function to select the serial port
function rs232init($com,$bautrate)
{
`mode $com: BAUD=$bautrate PARITY=N data=8 stop=1 xon=off`;
}
//function for sending
function send($comport,$character)
{
$fp = fopen ("$comport", "w+");
if (!$fp)
{
echo "Port is not opened for sending!";
}
else
{
fputs ($fp, $character);
fclose ($fp);
}
}
//function for reading
function read($comport2,$seconds)
{
$buffer = "";
$fp2 = fopen ("$comport2", "r+");
if (!$fp2)
{
echo "Port is not opened for reading!";
}
else
{
sleep($seconds);
$buffer .= fgets($fp2, 4096);
}
return $buffer;
fclose ($fp2);
}
// Example calls
rs232init("com2","9600"); // open Com2 with bautrate 9600
sending("com2","hello"); // send “hello” with comport2
$read = read("com2","2"); // read 2 seconds from Comport2 and write the result in variable $read
echo $read ; „“ // print variable $read
?>
我不知道该代码是否有效,但我希望能对您有所帮助。
推荐阅读
- java - 发送的数据未通过 $_POST ('it is empty' 因此结果为空') 到达,但已建立连接
- reactjs - 由 App 类中的其他组件使用 props 组件
- react-native - 如何在本机反应中找到状态变量的长度?
- eclipse - Team Explorer Everywhere:无法执行 HistoryAction。java.lang.NullPointerException
- c# - storyQ 将参数传递给函数
- c# - 如何在 .net core 中使用 Microsoft Graph 访问共享邮箱
- node.js - 反应写入文件:没有任何作用(fs,文件系统......)
- oracle - 存储过程在从源视图更新表时存在性能问题
- java - 输入字符串的 NumberFormatException:“9.014”,尝试设置 formField 的值时
- elasticsearch - Elasticsearch 日志解析模块