Getting Status of UART


To get various states of UART, get command of pid_ioctl function is required.

$return = pid_ioctl($pid, "get ITEM");

Available UART States

ITEM Description Return Value Return Type
baud baud rate[bps] e.g. 9600 Integer
parity parity 0 / 1 / 2 / 3 / 4 Integer
data data bit[bit] 8 / 7 Integer
stop stop bit[bit] 1 / 2 Integer
flowctrl flowctrl 0 / 1 / 2 / 3 Integer
txbuf size of send buffer[Byte] e.g. 1024 Integer
txfree free send buffer size[Byte] e.g. 1024 Integer
count tx total size of transmitted data[Byte] e.g. 65535 Integer
rxbuf size of receive buffer[Byte] e.g. 1024 Integer
rxlen received data size[Byte] e.g. 10 Integer
count rx total size of data received[Byte] e.g. 65535 Integer

example of getting UART states

Checking current information of UART is as follows:

<?php
$pid = pid_open("/mmap/uart0");               // open UART 0
$baud = pid_ioctl($pid, "get baud");          // get baud rate
$parity = pid_ioctl($pid, "get parity");      // get parity
$data = pid_ioctl($pid, "get data");          // get data bit
$stop = pid_ioctl($pid, "get stop");          // get stop bit
$flowctrl = pid_ioctl($pid, "get flowctrl");  // get flow control mode
echo "baud = $baud\r\n";                      // output e.g.: baud = 9600
echo "parity = $parity\r\n";                  // output e.g.: parity = 0
echo "data = $data\r\n";                      // output e.g.: data = 8
echo "stop = $stop\r\n";                      // output e.g.: stop = 1
echo "flowctrl = $flowctrl\r\n";              // output e.g.: flowctrl = 0
?>

Remaining Data Size in Send Buffer

Remaining data size in send buffer can be calculated as follows:

remaining data size in send buffer = size of buffer - free size of buffer

example

This example shows how to check remaining data size of send buffer.

<?php
$txlen = -1;
$data = "0123456789";
$pid = pid_open("/mmap/uart0");              // open UART 0
pid_ioctl($pid, "set baud 9600");            // baud rate: 9600 bps
pid_ioctl($pid, "set parity 0");             // parity: none
pid_ioctl($pid, "set data 8");               // data bit: 8
pid_ioctl($pid, "set stop 1");               // stop bit: 1
pid_ioctl($pid, "set flowctrl 0");           // flow control: none
pid_write($pid, $data);                      // write data to UART
while($txlen)
{
    $txbuf = pid_ioctl($pid, "get txbuf");   // get size of send buffer
    $txfree = pid_ioctl($pid, "get txfree"); // get remaining size of send buffer
    $txlen = $txbuf - $txfree;               // calculate remaining data size
    echo "tx len = $txlen\r\n";              // prints the size
    usleep(1000);
}
pid_close($pid);
?>

Received Data Size

The following shows how to get received data size of UART.

$rxlen = pid_ioctl($pid, "get rxlen[ $string]");

Getting received data size with a string

If a string is specified after "get rxlen" command, pid_ioctl function returns 0 until the string comes into UART. If the specified string comes, it returns the whole data size including the string.

Remaining Size of Receive Buffer

Remaining size of receive buffer can be calculated as follows:

>remaining size of receive buffer = size of buffer - received data size

example

This example shows how to get remaining size of receive buffer.

<?php
$rdata = "";
$pid = pid_open("/mmap/uart0");         // open UART 0
pid_ioctl($pid, "set baud 9600");       // baud rate: 9600 bps
pid_ioctl($pid, "set parity 0");        // parity: none
pid_ioctl($pid, "set data 8");          // data bit: 8
pid_ioctl($pid, "set stop 1");          // stop bit: 1
pid_ioctl($pid, "set flowctrl 0");      // flow control: none
$rxbuf = pid_ioctl($pid, "get rxbuf");  // get size of receive buffer
$rxlen = pid_ioctl($pid, "get rxlen");  // get received data size
$rxfree = $rxbuf - $rxlen;              // get remaining size of receive buffer
echo "rxfree = $rxfree\r\n";            // print the size
pid_close($pid);
?>