有谁用过“http://www.yafeilinux.com/?p=820”编的lincom吗?串口打不开是什么原因?
时间:2011-11-06
来源:互联网
我完全按照“http://www.yafeilinux.com/?p=820”编写的linux下的串口调试程序lincom,串口为“/dev/ttyS0”,运行是没有问题的,但是不能接收和发送数据。调试发现串口好像没打开,另外用minicom测试串口是可以正常收发数据的,说明串口本身是没有问题的,问题是在用Qt编写的lincom程序。但是我是严格按照yafeilinux介绍的编写的,不知各位网友有没有出现过这种问题?是怎么解决的?下面是wiget.cpp的代码。
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
myCom = new
Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling );
//定义串口对象,指定串口名和查询模式,这里使用事件驱动Polling
myCom->open(QIODevice::ReadWrite);
//以读写方式打开串口
myCom->setBaudRate(BAUD9600);
//波特率设置为9600
myCom->setDataBits(DATA_8);
//设置数据位为8
myCom->setParity(PAR_NONE);
//奇偶校验设置为无校验
myCom->setStopBits(STOP_1);
//停止位设置为1位
myCom->setFlowControl(FLOW_OFF);
//数据流控制设置为无流控制
myCom->setTimeout(500);
//延时设置,延时500MS
readTimer = new QTimer(this);
readTimer->start(100);
connect(readTimer,SIGNAL(timeout()),this,SLOT(readMyCom()) );
//信号和槽函数关联,延时一段时间,进行读串口操作
}
//实现槽函数
void Widget::on_pushButton_clicked() //发送数据
{
myCom->write(ui->lineEdit->text().toAscii() );
//以ASCII码形式将数据写入串口
}
void Widget::readMyCom() //读取串口数据并显示出来
{
QByteArray temp=myCom->readAll();
//读取串口缓冲区临时变量temp
ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}
Widget::~Widget()
{
delete ui;
}
void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
myCom = new
Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling );
//定义串口对象,指定串口名和查询模式,这里使用事件驱动Polling
myCom->open(QIODevice::ReadWrite);
//以读写方式打开串口
myCom->setBaudRate(BAUD9600);
//波特率设置为9600
myCom->setDataBits(DATA_8);
//设置数据位为8
myCom->setParity(PAR_NONE);
//奇偶校验设置为无校验
myCom->setStopBits(STOP_1);
//停止位设置为1位
myCom->setFlowControl(FLOW_OFF);
//数据流控制设置为无流控制
myCom->setTimeout(500);
//延时设置,延时500MS
readTimer = new QTimer(this);
readTimer->start(100);
connect(readTimer,SIGNAL(timeout()),this,SLOT(readMyCom()) );
//信号和槽函数关联,延时一段时间,进行读串口操作
}
//实现槽函数
void Widget::on_pushButton_clicked() //发送数据
{
myCom->write(ui->lineEdit->text().toAscii() );
//以ASCII码形式将数据写入串口
}
void Widget::readMyCom() //读取串口数据并显示出来
{
QByteArray temp=myCom->readAll();
//读取串口缓冲区临时变量temp
ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}
Widget::~Widget()
{
delete ui;
}
void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
作者: guofucaigfc 发布时间: 2011-11-06
1.QextSerialBase::Polling 这是轮询机制不是事件驱动机制,更正一下。
2.终端看看/etc/dev下面串口ttys0应该是有人占用吧,还有你用的是U转串的线吧,所以啊你要建立一个符号链接吧USB口和ttys*进行连接 *号是一个未被占用的号码,随便都行只要没被占用就好。建立符号链接的命令ln 参数是s.
希望能够帮到你
2.终端看看/etc/dev下面串口ttys0应该是有人占用吧,还有你用的是U转串的线吧,所以啊你要建立一个符号链接吧USB口和ttys*进行连接 *号是一个未被占用的号码,随便都行只要没被占用就好。建立符号链接的命令ln 参数是s.
希望能够帮到你
作者: xiaoliang_1 发布时间: 2011-11-06
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28