确认UART状态信息


通过pid_ioctl函数的get命令可确认UART的各种状态。

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

可确认的UART状态信息

ITEM 说明 返还值 返还 形式
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

确认UART状态信息例

UART的当前设定值可按如下确认。

$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

发送缓冲区中剩余的数据大小

可以这样计算UART发送缓冲区中剩余的数据大小。

发送缓冲区中剩余的数据大小 = 发送缓冲区大小 - 发送缓冲区空间

此例子的0号UART上传送10字节,计算发送缓冲区数据大小来输出。

$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);

接收数据大小

UART接收的数据大小可如下进行确认。

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

确认至特定字符串接收的数据大小

在rxlen命令后面输入特定字符串($string)时pid_ioctl函数在相应字符串到达前返还0,相应字符串到达时返还该字符串为止的接收数据大小。

接收缓冲区空余空间

可以这样计算UART接收缓冲区中剩余的数据大小。

>接收缓冲区空余空间 = 接收缓冲区大小 - 接收数据大小

此例将计算UART 0号的接收缓冲区空间输出。

$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);