使用 PHP 从命令行读取/写入数据
我有一台服务器(debian lenny)、光谱分析仪,并且我已经从 Steve 下载了源代码集合Sharples 的 网站。有了这些。我可以连接到频谱分析仪 - 输入命令并获得响应。
例如:
my-atom:~/vxi11# ./vxi11_cmd 135.123.106.59
Input command or query ('q' to exit): *IDN?
我得到:Rohde&Schwarz,FSV-7,102004/007,1.50 SP1
我可以使用 PHP 连接到此服务器、编写命令并读取响应吗?我正在考虑套接字,但我不确定是否是最好的选择。
任何帮助将不胜感激。
谢谢, 彼得
I have a server (debian lenny), spectral analyzer and I have downloaded collection of source codes from Steve Sharples's site. With these s. coudes I am able to connect to spectral analyzer - type command and get the responce.
For example:
my-atom:~/vxi11# ./vxi11_cmd 135.123.106.59
Input command or query ('q' to exit): *IDN?
and I get: Rohde&Schwarz,FSV-7,102004/007,1.50 SP1
Am I able to connect to this server, write command and read the responce using PHP? I was thinking about sockets, but I am not sure if the best choice.
Any help would be most appreciated.
Thanks,
Petr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 exec、shell_exec, 系统,或 proc_open。
我推荐 proc_open,它允许您为 stdin、stdout 和 stderr 设置管道。
查看 proc_open 链接。
You can execute external programs using exec, shell_exec, system, or proc_open.
I would recommend proc_open, which allows you to set up pipes for stdin, stdout, and stderr.
Check out the proc_open link.
你也可以这样做:
Also you can do like:
您可以将 shell_exec 与 netcat 结合使用:
注意
-q 1
选项,这会导致 netcat 等待 1 秒以获得答案。您不需要“设置”命令的等待时间。另外,*IDN?
不需要等待时间,但许多其他查询命令做。使用 netcat 进行此连接与裸机连接非常危险。您可能想要编写一个捕获常见错误的 shell 脚本。然后您可以使用 shell_exec 调用该脚本。
当然,最好的方法是采用 Steve Sharples 免费库 并修改他的 cmd适合您自己需求的示例。
You can combine shell_exec with netcat:
Note the
-q 1
option, which causes netcat to wait for 1 second for the answer. You don't need this wait time on "set" commands. Also,*IDN?
does not need the wait time, but many other query commands do.Using netcat for this connection is dangerously close to the bare metal. You might want to write a shell-script that catches common errors. You can then call that script with shell_exec instead.
The best way of course would be to take Steve Sharples free library and modify his cmd example to suit your own needs.