系统相关
首页 > 系统相关> > Ubuntu下访问串口

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工具

标签:case,__,串口,QSerialPort,break,访问,Ubuntu,serial,port
来源: https://www.cnblogs.com/hysea2019/p/10972543.html