Last active
December 1, 2017 09:30
-
-
Save yusureabc/ce652c8e07634be6d41a02f2b207abd0 to your computer and use it in GitHub Desktop.
PHP实现连接设备、通讯和发送命令的方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 16进制转换 | |
* @param string $hex | |
* @return string 返回值 | |
*/ | |
function hexToStr($hex) { | |
$string = ""; | |
for ($i = 0; $i < strlen($hex) - 1; $i+=3) { | |
$string.=chr(hexdec($hex[$i] . $hex[$i + 1])); | |
} | |
return $string; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 采用RS232串口技术连接设备,实现通讯和发送命令 | |
* @param [type] $com 串口号 | |
* @param [type] $baudrate 波特率 | |
* @param [type] $in 发送命令 | |
*/ | |
function rs232_connect($com, $baudrate, $in) { | |
@set_time_limit(10); | |
//使用 'windows下设置窜口参数; | |
exec("mode " . $com . " BAUD=" . $baudrate . " PARITY=n DATA=8 STOP=1 odsr=off"); | |
//打开端口串口$com | |
$f = @fopen($com, 'w+'); | |
//判断串口是否正常打开 | |
if(!$f) { | |
die("Error when open $com"); | |
} | |
//向串口发送数据; | |
fwrite($f, $in); | |
//关闭端口 | |
fclose($f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 采用php socket技术使用TCP/IP连接设备 | |
* @param string $service_port 连接端口 | |
* @param string $address 发送IP地址 | |
* @param string $in 发送命令 | |
* @return string/boolean 返回值 | |
*/ | |
function Send_socket_connect($service_port, $address, $in) { | |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('could not create socket!'); | |
$timeout = 2; | |
//设置超时时间 | |
$time = time(); | |
//设置非阻塞模式 | |
@socket_set_nonblock($socket); | |
//超时判断 | |
while (!@socket_connect($socket, $address, $service_port)){ | |
$err = socket_last_error($socket); | |
//连接成功,跳出循环 | |
if ($err === 10056) { | |
break; | |
} | |
//连接失败,判断超时时间,停止 | |
if ((time() - $time) >= $timeout) { | |
socket_close($socket); | |
return false; | |
exit(); | |
} | |
//刷新频率(250毫秒) | |
usleep(250000); | |
} | |
//设置阻塞模式 | |
@socket_set_block($socket); | |
//发送命令到设备 | |
socket_write($socket, $in, strlen($in)); | |
//接收设备命令返回数据 | |
$buffer = socket_read($socket, 1024, PHP_NORMAL_READ); | |
//关闭连接 | |
socket_close($socket); | |
//输出返回值 | |
return $buffer; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 采用php socket技术使用UDP协议连接设备 | |
* @param string $service_port 连接端口 | |
* @param string $address 连接IP地址 | |
* @param string $in 发送命令 | |
* @return string/boolean 返回值 | |
*/ | |
function Send_socket_xdcoder_udp($service_port, $address, $in) { | |
//采用php socket技术使用UDP协议连接设备 | |
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); | |
@socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2, "usec" => 0)); | |
//发送命令 | |
@socket_sendto($socket, $in, strlen($in), 0, $address, $service_port); | |
@socket_recvfrom($socket, $buffer, 1024, MSG_WAITALL, $address, $service_port); | |
//关闭连接 | |
socket_close($socket); | |
if (!empty($buffer)) { | |
return $buffer; | |
} else { | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 采用php socket技术使用TCP/IP连接设备 | |
* @param string $service_port 连接端口 | |
* @param string $address 发送IP地址 | |
* @param string $in 发送命令 | |
* @return string/boolean 返回值 | |
*/ | |
function Send_socket_connect($service_port, $address, $in) { | |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('could not create socket!'); | |
$timeout = 2; | |
//设置超时时间 | |
$time = time(); | |
//设置非阻塞模式 | |
socket_set_nonblock($socket); | |
//超时判断 | |
while (!@socket_connect($socket, $address, $service_port)){ | |
$err = socket_last_error($socket); | |
//连接成功,跳出循环 | |
if ($err === 10056) { | |
break; | |
} | |
//连接失败,判断超时时间,停止 | |
if ((time() - $time) >= $timeout) { | |
socket_close($socket); | |
return false; | |
exit(); | |
} | |
//刷新频率(250毫秒) | |
usleep(250000); | |
} | |
//设置阻塞模式 | |
socket_set_block($socket); | |
//发送命令到设备 | |
socket_write($socket, $in, strlen($in)); | |
//接收设备命令返回数据 | |
$buffer = socket_read($socket, 1024, PHP_NORMAL_READ); | |
//关闭连接 | |
socket_close($socket); | |
//输出返回值 | |
return $buffer; | |
} | |
/* Toggle */ | |
// $data = '{"id":1,"method":"toggle","params":[]}' . "\r\n"; | |
/* Flash Notify( White ) */ | |
// $data = '{"id":1,"method":"start_cf","params":["cf", 6, 0,"600,2,4000,70,400,2,4000,1"]}' . "\r\n"; | |
/* Flash Notify( Red ) */ | |
// $data = '{"id":1,"method":"start_cf","params":["cf", 6, 0,"600,1,16723245,70, 400,1,16723245,1"]}' . "\r\n"; | |
/* Flash Notify( Green ) */ | |
// $data = '{"id":1,"method":"start_cf","params":["cf", 6, 0,"600,1,9699131,70, 400,1,9699131,1"]}' . "\r\n"; | |
/* Flash Notify( Blue ) */ | |
$data = '{"id":1,"method":"start_cf","params":["cf", 6, 0,"600,1,7792639,70, 400,1,7792639,1"]}' . "\r\n"; | |
/* Flash Notify( Yellow ) */ | |
// $data = '{"id":1,"method":"start_cf","params":["cf", 6, 0,"600,1,16771675,70, 400,1,16771675,1"]}' . "\r\n"; | |
/* Flash Notify( Purple ) */ | |
// $data = '{"id":1,"method":"start_cf","params":["cf", 6, 0,"600,1,11903999,70, 400,1,11903999,1"]}' . "\r\n"; | |
// $data = '{"id":1,"method":"set_name","params":["test_my_bulb"]}' . "\r\n"; | |
$result = Send_socket_connect( '55443', '192.168.10.42', $data ); | |
print_r( $result ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment