php 串口通信
1.安装dio
liunx
下载curl ‘http://pecl.php.net/get/dio-0.2.0.tgz’ -o dio-0.2.0.tgz (sudo snap install curl安装curl下载工具)
也可以到官网下载 http://pecl.php.net/dio(http://pecl.php.net/package/dio)
解压 sudo pecl install dio-0.1.0.tgz
pecl 没有安装pecl
到mods-available目录
cd /etc/php/7.4/mods-available/
sudo vi dio.ini 新建dio.ini —>拷贝复制xml.ini到dio.ini然后修改extension=dio.so(dio.so在ls /usr/lib/php/20190902/可以看到相当于windows系统下dio.dll)
完了到conf.d目录
cd /etc/php/7.4/cli/conf.d/
sudo ln -s /etc/php/7.4/mods-available/dio.ini 20-dio.ini
php --ini 查看dio.ini是否加入
#!/usr/bin/php
<?php //使用串口 $com = "/dev/ttyUSB0"; $baudrate = 9600; $fd = dio_open($com, O_RDWR | O_NOCTTY | O_NONBLOCK); dio_fcntl($fd, F_SETFL, O_SYNC); dio_tcsetattr($fd, array( 'baud' => 9600, 'bits' => 8, 'stop' => 1, 'parity' => 0 )); if(!$fd){ printf("Error when open".$com); } //开始 //$ff = dio_stat($fd); //写信息 dio_write($fd, "12hello!"); //读取长度 $len = 40; while(1){ //读取信息 $data = dio_read($fd, $len); if($data){ //printf("read: ".$data."\n") ; echo "re: ".$data."\r\n"; /} sleep(1); dio_close($fd); //usleep(1000000 * 0.5); } ?>