dio 扩展 https://pecl.php.net/package/dio
linux代码
<?php
$fd = dio_open ( '/dev/ttyS0' , O_RDWR | O_NOCTTY | O_NONBLOCK );
//dio_fcntl ( $fd , F_SETFL , O_SYNC );
if ( dio_fcntl ( $fd , F_SETLK , Array( "type" => F_WRLCK )) == - 1 ) {
// the file descriptor appears locked
echo "The lock can not be cleared. It is held by someone else.\n" ;
} else {
echo "Lock successfully set/cleared\n" ;
}
dio_tcsetattr ( $fd , array(
'baud' => 9600 ,
'bits' => 8 ,
'stop' => 1 ,
'parity' => 0
));
while ( 1 ) {
$data = dio_read ( $fd , 9999 );
if ( $data ) {
echo bin2hex($data)."\n";
}
usleep(300000);
}
windows 代码
<?php
exec('mode com1: baud=9600 data=8 stop=1 parity=n xon=on');
// execute 'help mode' in command line of Windows for help
$fd = dio_open('com1:', O_RDWR);
while ( true ) {
$data = dio_read ( $fd , 256 );
if ($data) {
echo bin2hex($data)."\n";
}
//usleep(10000);
}
网友评论