作者taiwancat (月季)
看板PHP
标题Re: [请益] php可以跟串列埠通讯吗?
时间Fri Jun 23 19:52:14 2006
※ 引述《chzn (天风)》之铭言:
: 最近想要利用php写一支程式,让他可以与本机的com1 通讯
: 进而 处理一些事,可是我查书...大部分都是着重与mysql的应用
: 不知有没有板友,有写过相关的程式?
: 可以请教一下吗?谢谢!!
找了一下php官网 找到了一些资料 给你参考一下
网址:
http://www.php.net/manual/en/function.fopen.php
他里面提供了两个方法
第二个方法是直接用fopen跟你的serial port通讯
第一个方法则是利用一个3rd party程式,这支程式可以把
序列资料和TCP/IP的资料互转
我个人是没有用过,手边也没有机器可以实测
不过我想...
如果你的程式是网页的话,用第一个方法可能会比较好
如果是执行用的script,第二个可能会比较单纯
纯属猜测,也请你有心得的话 跟我们分享一下噜^^
<?php
// HOW TO USE PHP TO WRITE TO YOUR SERIAL PORT: TWO METHODS
$serproxy=true;
if ($serproxy) {
// Use this code in conjunction with SERPROXY.EXE
// (
http://www.lspace.nildram.co.uk/freeware.html)
// which converts a Serial stream to a TCP/IP stream
$fp = fsockopen ("localhost", 5331, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)";
} else {
$e = chr(27);
$string = $e . "A" . $e . "H300";
$string .= $e . "V100" . $e . "XL1SATO";
$string .= $e . "Q1" . $e . "Z";
echo $string;
fputs ($fp, $string );
fclose ($fp);
}
} elseif ($com1) {
// Use this code to write directly to the COM1 serial port
// First, you want to set the mode of the port. You need to set
// it only once; it will remain the same until you reboot.
// Note: the backticks on the following line will execute the
// DOS 'mode' command from within PHP
`mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
$fp = fopen ("COM1:", "w+");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
$e = chr(27);
$string = $e . "A" . $e . "H300";
$string .= $e . "V100" . $e . "XL1SATO";
$string .= $e . "Q1" . $e . "Z";
echo $string;
fputs ($fp, $string );
fclose ($fp);
}
}
?>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.162.72.88
1F:推 chzn:好的,谢谢你的提供,有心得 我会回来分享的! 06/24 06:57