qt串口

qt串口通信:
#ifdef _TTY_POSIX
#include "posix_qextserialport.h"
#define QextBaseType Posix_QextSerialPort
#else
#include "win_qextserialport.h"
#define "QextBaseTypeport.h"
#define QextBaseType Win_QexSerialPort
#endif
QextSerialBase类中 QueryMode读取串口的方式:
Polling :建立定时器 读取串口信息
EventDrrivent: 一旦有数据发出readyRead()信号
设置串口为事件驱动模式
mycom = new Win_QextSerialPort("COM1",QextSerialBase::EventDriven);
mycom->open(QIODevice::ReadWrite);
mycom->setBaudRate(BAUD9600);
mycom->setDataBits(DATA_8);
mycom->serParity(PAR_NONE);
mycom->setStopBits(STOP_1);
mycom->setFlowControl(FLOW_OFF);数据流控制
mycom->setTimeout(500);
connect(mycom,SIGNAL(readyRead()),this,SLOT(readMycom()));

void Widget::readMycom()
{
 if(mycom->buteAvailable()>= 8)
 {
  QByteArray temp = mycom->readAll();
  ui->textBrowser->insertPlainText(Temp);
 }
}
使用Polling模式:
mycom = new Win_QextSerialPort("Com1",QextSerialBase::Polling);
readTimer = new QTimer(this);
readTimer->start(100);
mycom->setTimeout(10);  将数据放入串口缓冲区
connect(readTimer,SIGNAL(timeout()),this,SLOT(readMyCom()));

Linux :
mycom = new Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling)

www.yafeilinux.com 文档
 
 
posted @ 2019-09-15 18:13  countryboy666  阅读(283)  评论(0编辑  收藏  举报