读取数字I/O状态时使用pid_read函数,将所有端口的状态一次性读取或是使用pid_ioctl函数的get命令读取单一端口信息。 单一端口的信息的情况,不仅可以查看输入/输出状态,也可以读取设定类型。
pid_read($pid, VALUE); // read multiple states(in 32bits unit)
pid_ioctl($pid, "get N ITEM"); // read a single state(in a bit unit)
在单一信息读取中可使用的ITEM如下:
ITEM | 说明 | |
---|---|---|
mode | Return the port status in string type |
I/O pin: "in", "out", "led_xxx" and so on |
pins while using by UART, SPI or I2C: "hdev" | ||
Designated to output pin of ST: "st_out" | ||
input | Return the input port status in integer (0: LOW, 1: HIGH) | |
output | Return the output port status in integer (0: LOW, 1: HIGH) |
下面例子是将UIO0的多个端口设定为输入端口后,读取状态值并输出。
$value = 0;
$pid = pid_open("/mmap/uio0"); // open UIO0
pid_ioctl($pid, "set 0-7 mode in_pu"); // set port 0 ~ 7 to input(pull-up)
pid_read($pid, $value); // read digital I/O status(32bits unit)
printf("0x%x\r\n", $value); // output example: 0xffffffff
下面例子是将UIO0的0号端口的基本状态为多个端口设定为输入端口后,读取状态值并输出。
$pid = pid_open("/mmap/uio0"); // open UIO0
pid_ioctl($pid, "set 0 mode out high"); // set port 0 to output
$mode = pid_ioctl($pid, "get 0 mode"); // read a digital I/O mode
$output = pid_ioctl($pid, "get 0 output"); // read a digital I/O state
printf("%s, %d\r\n", $mode, $output); // output example: out, 1
※ 利用pid_ioctl的get命令读取单一端口的状态时,相应端口的类型为输入端口的情况输出"get N input",输出端口时使用"get N output"。
为了输出数字I/O值利用pid_write函数,在多个端口上一次性输出,或使用pid_ioctl的set命令来输出单一端口。
pid_write($pid, VALUE); // write to multiple ports(32 bits unit)
pid_ioctl($pid, "set N output TYPE"); // write to a single port(a bit unit)
The 下面例子是将UIO0的多个端口设定为输出端口后写任意值,重新读取数字I/O的状态值输出的例子。
$value = 0;
$pid = pid_open("/mmap/uio0"); // open UIO0
pid_ioctl($pid, "set 0-7 mode out"); // set port 0 ~ 7 to output
pid_read($pid, $value); // read status
pid_write($pid, ($value & 0xffffff00) | 0x00000055); // write 0x00000055
pid_read($pid, $value); // read status
printf("0x%0x\r\n", $value); // output example: 0x00000055
下面例子是将UIO0的0号设定为基本状态为LOW的输出端口,输出HIGH后读取输入/输出状态来输出。
$pid = pid_open("/mmap/uio0"); // open UIO0
pid_ioctl($pid, "set 0 mode out low"); // set port 0 to output(LOW)
pid_ioctl($pid, "set 0 output high"); // write HIGH
$output = pid_ioctl($pid, "get 0 output"); // read state of port 0
printf("%d\r\n", $output); // output: 1
下面例子是在0号端口设定输出限制的情况与不是的情况,比较是否适用HIGH输出的例子。
$pid = pid_open("/mmap/uio0"); // open UIO0
pid_ioctl($pid, "set 0 mode out low"); // set port 0 to output(LOW)
pid_ioctl($pid, "set 0 lock"); // enable port 0 to output lock
pid_ioctl($pid, "set 0 output high"); // write HIGH to port 0
$output1 = pid_ioctl($pid, "get 0 output"); // read state of port 0
pid_ioctl($pid, "set 0 unlock"); // disable the output lock
pid_ioctl($pid, "set 0 output high"); // write HIGH to port 0 again
$output2 = pid_ioctl($pid, "get 0 output"); // read state of port 0
printf("%d, %d\r\n", $output1, $output2); // output: 0, 1