查看当前设备节点的权限:
可以看到该设备节点需要root权限,普通用户无法读写。所以应用程序无法打开设备节点。
运行udevadm命令,获取该设备的信息。
uas@localhost:/dev$ udevadm info -a /dev/ttySTM2
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/platform/soc/40019000.serial/tty/ttySTM2':
KERNEL=="ttySTM2"
SUBSYSTEM=="tty"
DRIVER==""
looking at parent device '/devices/platform/soc/40019000.serial':
KERNELS=="40019000.serial"
SUBSYSTEMS=="platform"
DRIVERS=="stm32-usart"
ATTRS{driver_override}=="(null)"
looking at parent device '/devices/platform/soc':
KERNELS=="soc"
SUBSYSTEMS=="platform"
DRIVERS==""
ATTRS{driver_override}=="(null)"
looking at parent device '/devices/platform':
KERNELS=="platform"
SUBSYSTEMS==""
DRIVERS==""
创建undev规则,设置该设备节点普通用户可读写。
sudo echo KERNEL==\"ttySTM2\", MODE=\"0666\" > /etc/udev/rules.d/10-ttySTM2.rules
重启,然后可以看到该设备节点可读写权限生效。
也可以创建组来对权限进行管理,
addgroup ttySTM
adduser uas ttySTM
echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\", GROUP=\"ttySTM\" > /etc/udev/rules.d/10-ttySTM2.rules