linux qt 操作串口。得不到数据v解决办法
linux qt 操作串口。。。得不到数据v
先看代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myCom = new Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling);
//定义串口对象,指定串口名和查询模式,这里使用事件驱动EventDriven
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,这个在Windows下好像不起作用
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
//信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
myCom->write(ui->lineEdit->text().toAscii());
QString str=ui->lineEdit->displayText();
qDebug() << "read: "<<str;
//以ASCII码形式将数据写入串口
}
void MainWindow::readMyCom() //读取串口数据并显示出来
{
qDebug() << "read:begin ";//这一句老是打印不出来????????????!!!!难道一直没执行这句。。。[color=#808000][/color]
QByteArray temp = myCom->readAll();
//读取串口缓冲区的所有数据给临时变量temp
ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}。。
我是按网上yafeilinux 教程做的,程序运行无误,但读不出数据,。。求高人指点。。。windows下能搞到数据v
------解决方案--------------------
readMyCom()
把它放到一个定时器里
------解决方案--------------------
Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling);
-------------------------
看一下这个参数是否正确
先看代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myCom = new Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling);
//定义串口对象,指定串口名和查询模式,这里使用事件驱动EventDriven
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,这个在Windows下好像不起作用
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
//信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
myCom->write(ui->lineEdit->text().toAscii());
QString str=ui->lineEdit->displayText();
qDebug() << "read: "<<str;
//以ASCII码形式将数据写入串口
}
void MainWindow::readMyCom() //读取串口数据并显示出来
{
qDebug() << "read:begin ";//这一句老是打印不出来????????????!!!!难道一直没执行这句。。。[color=#808000][/color]
QByteArray temp = myCom->readAll();
//读取串口缓冲区的所有数据给临时变量temp
ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}。。
我是按网上yafeilinux 教程做的,程序运行无误,但读不出数据,。。求高人指点。。。windows下能搞到数据v
------解决方案--------------------
readMyCom()
把它放到一个定时器里
------解决方案--------------------
Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling);
-------------------------
看一下这个参数是否正确