问题
Ubuntu22.04.1
Linux版本5.19.0-32-generic
运行Qt串口通信 m_serialPort->open(QIODevice::ReadWrite) 时,总是失败。
打印失败原因
if (!this->m_serialPort->open(QIODevice::ReadWrite))
{
qDebug() << __FUNCTION__ << "open fail,error info: " << this->m_serialPort->errorString();
return;
}
else
{
//设置串行端口属性
}
open fail,error info: Permission denied
由“Permission denied”可知,是权限问题。
串口权限设置
将当前用户加入到 dialout 用户组,并重启系统。
查看串口信息
$ ls -l /dev/ttyUSB0
crw-rw---- 1 root dialout 4, 64 Jun 2 18:39 /dev/ttyUSB0
查看当前用户名
$ whoami
ncyf
当前用户加入到dialout用户组
$ sudo usermod -aG dialout username
最后重启系统(重要)
$ shutdown -r now