我使用PHP-DIO扩展与指纹识别器进行通信。设备创建手指图像并通过RS232串口发回。它很长,64520字节的纯数据,而不是字符串。
我的问题是,我有时并不是所有字节,只有50-60000。 使用Linux中的GTKTerm,或Windows中的RealTerm运行良好,我的短命令(指纹识别,用户注册等)也能很好地工作。
为什么有时会将数据读取50-60k字节,有时为什么会停止?
我的串口驱动程序代码:
protected function openPort(){
try
{
$c = stream_context_create(array('dio' =>
array('data_rate' => $this->baudRate,
'data_bits' => $this->bits,
'stop_bits' => $this->spotBit,
'parity' => 0,
'is_canonical' => 0,
)));
$bbSerialPort = null;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'LIN'){
$bbSerialPort = fopen('dio.raw:///dev/ttyUSB0', 'r+', false, $c);
exec('stty -F /dev/ttyUSB0 115200 line 0 min 60 time 0 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke eof undef flush undef start undef stop undef');
}
if(!$bbSerialPort){
var_dump("Could not open Serial port {$this->portName} ");
exit;
}
$this->serialResource = $bbSerialPort;
return 1;
}
catch (Exception $e){
exit($e->getMessage());
}
}
public function sendCommand($command){
fwrite($this->serialResource, call_user_func_array('pack',$command) );
stream_set_blocking($this->serialResource, 0);
$runForSeconds = new DateInterval("PT3S");
$endTime = (new DateTime())->add($runForSeconds);
$answer = "";
while (new DateTime() < $endTime) {
$data = fread($this->serialResource,1);
if ($data !== "") {
$tmp = unpack('H*', $data);
$answer .= $tmp[1];
$endTime = (new DateTime())->add($runForSeconds);
}
}
return $answer;
}