pid_read()


int pid_read ( int $pid, int/string &$buf [ , int $len ] )

Description

pid_read() attempts to read up to $len bytes from the port or peripheral $pid into the buffer $buf.

※ available F/W version : all

Parameters

Return values

On success, the number of bytes read is returned, otherwise PHP error. It is not an error if this number is smaller than the number of bytes requested. This may happen in some cases, for example fewer bytes are actually available right now.

Example

<?php
$buf = "";
$pid = pid_open("/mmap/uart0");      // open UART0

// set the device to 115200bps, no parity, 8 databit, 1stop bit
pid_ioctl($pid, "set baud 115200");
pid_ioctl($pid, "set parity 0");
pid_ioctl($pid, "set data 8");
pid_ioctl($pid, "set stop 1");
pid_ioctl($pid, "set flowctrl 0");

while(1)
{
    $rlen = pid_read($pid, $buf, 10);  // read maximum 10 bytes from the $pid into $buf
    if($rlen > 0)  // if there is any received data
    {
        $wlen = pid_write($pid, $buf, $rlen);  // write $rlen bytes of the $buf to the $pid
        echo "$rlen bytes received and echoed\r\n";
    }
}
pid_close($pid);
?>
<?php
$ch = 0;
$pid = pid_open("/mmap/uart0");  // open UART0

// set the device to 115200bps, no parity, 8 databit, 1stop bit
pid_ioctl($pid, "set baud 115200");
pid_ioctl($pid, "set parity 0");
pid_ioctl($pid, "set data 8");
pid_ioctl($pid, "set stop 1");
pid_ioctl($pid, "set flowctrl 0");
while(1)
{
    $rlen = pid_read($pid, $ch, 1);  // read 1 byte from the $pid into $ch
    if($rlen > 0)  // if there is received data
    {
        printf("received data: 0x%02x(%c).\r\n", $ch, $ch);  // print the data
    }
}
pid_close($pid);
?>

See also

pid_open() / pid_close() / pid_write() / pid_ioctl()

Remarks

None