码迷,mamicode.com
首页 > 系统相关 > 详细

Ubuntu下访问串口

时间:2019-06-04 13:05:08      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:访问   bre   mes   return   nba   readwrite   date   etc   ready   

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下访问串口

标签:访问   bre   mes   return   nba   readwrite   date   etc   ready   

原文地址:https://www.cnblogs.com/hysea2019/p/10972543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!