Ubuntu下访问串口
Posted hysea2019
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu下访问串口相关的知识,希望对你有一定的参考价值。
Ubuntu下访问串口
1、首先查看串口列表
dmesg | grep ttyS*
2、增加规则文件
在/etc/udev/rules.d目录下创建tty.rules文件,增加以下内容
KERNEL=="ttyS[0-9]*",MODE:="0777",GROUP:="dialout"
3、保存并重启电脑
4、编写代码,QT代码如下
void Init(const char *szCom, int nBaud, int oeCheck, int nDatabit, int nStopbit)
strcpy(m_szCom, szCom);
m_nBaud = m_nBaud;
m_oeCheck = oeCheck;
m_nDatabit = nDatabit;
m_nStopbit = nStopbit;
serial_port_t = new QSerialPort();
switch (nBaud)
case 1200:
serial_port_t->setBaudRate(QSerialPort::Baud1200);
break;
case 2400:
serial_port_t->setBaudRate(QSerialPort::Baud2400);
break;
case 9600:
serial_port_t->setBaudRate(QSerialPort::Baud9600);
break;
case 38400:
serial_port_t->setBaudRate(QSerialPort::Baud38400);
break;
case 115200:
serial_port_t->setBaudRate(QSerialPort::Baud115200);
break;
case 192000:
serial_port_t->setBaudRate(QSerialPort::Baud19200);
break;
default:
serial_port_t->setBaudRate(QSerialPort::Baud9600);
break;
switch (oeCheck)
case 0:
serial_port_t->setParity(QSerialPort::OddParity);
break;
case 1:
serial_port_t->setParity(QSerialPort::EvenParity);
break;
case 2:
serial_port_t->setParity(QSerialPort::NoParity);
break;
default:
serial_port_t->setParity(QSerialPort::NoParity);
break;
switch (nDatabit)
case 5:
serial_port_t->setDataBits(QSerialPort::Data5);
break;
case 6:
serial_port_t->setDataBits(QSerialPort::Data6);
break;
case 7:
serial_port_t->setDataBits(QSerialPort::Data7);
break;
case 8:
serial_port_t->setDataBits(QSerialPort::Data8);
break;
default:
serial_port_t->setDataBits(QSerialPort::Data8);
break;
switch (nStopbit)
case 1:
serial_port_t->setStopBits(QSerialPort::OneStop);
break;
case 2:
serial_port_t->setStopBits(QSerialPort::TwoStop);
break;
default:
serial_port_t->setStopBits(QSerialPort::OneStop);
break;
serial_port_t->setFlowControl(QSerialPort::NoFlowControl);
if (!serial_port_t->open(QIODevice::ReadWrite))
CLogWrite::LogWriteEX(LOG_ERROR, __FUNCTION__, __LINE__, 999999, "Open COM: %s Failed %d %d %d %d",
szCom, nBaud, oeCheck, nDatabit, nStopbit);
return;
else
CLogWrite::LogWriteEX(LOG_ERROR, __FUNCTION__, __LINE__, 999999, "Open COM: %s Succeed %d %d %d %d",
szCom, nBaud, oeCheck, nDatabit, nStopbit);
connect(serial_port_t, &QSerialPort::readyRead, this, &CLasairManage::read_date);
2、minicom工具
以上是关于Ubuntu下访问串口的主要内容,如果未能解决你的问题,请参考以下文章