pid_write()


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

Description

pid_write() writes $len bytes from the $buf to the port or peripheral $pid

※ available F/W version : all

Parameters

Return values

On success, the number of bytes written is returned. Otherwise: PHP error

Examples

<?php
$rbuf = "";
$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, $rbuf, 10);  // read maximum 10 bytes from the $pid into $rbuf
    if($rlen > 0)  // if there is any received data
    {
        $wlen = pid_write($pid, $rbuf, $rlen);  // write $rlen bytes of the $rbuf to the $pid
        echo "$rlen bytes received and $wlen bytes echoed\r\n";
    }
}
pid_close($pid);
?>
<?php
$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");

$ch = '0';
$wlen = pid_write($pid, $ch, 1);  // write the $ch to the UART0
echo "\$wlen = $wlen\r\n";  // sent length is 1

$str = "Hello";
$wlen = pid_write($pid, $str);  // write the $str to the UART0
echo "\$wlen = $wlen\r\n";  // sent length is 5.

$ch = '0';
$wlen = pid_write($pid, $ch);   // write the $ch to the UART0
echo "\$wlen = $wlen\r\n";  // sent length is 1.

$i = 1;
$wlen = pid_write($pid, $i);    // write the $i to the UART0
echo "\$wlen = $wlen\r\n";  // sent length is 8 (value: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00).

pid_close($pid);
?>

See also

pid_open() / pid_close() / pid_read() / pid_ioctl()

Remarks

None