Qt 串口通讯

整理一下关于Qt串口通讯

程序下载地址:http://download.csdn.net/detail/u012166958/9561553

串口通讯使用的类为

QSerialPort Class
公共类型:
enum BaudRate { Baud1200, Baud2400, Baud4800, Baud9600, ..., UnknownBaud } //波特率
enum DataBits { Data5, Data6, Data7, Data8, UnknownDataBits } //数据位
enum Direction { Input, Output, AllDirections } //输入(输出)方向
flags Directions
enum FlowControl { NoFlowControl, HardwareControl, Software

enum Parity { NoParity, EvenParity, OddParity, SpaceParity, MarkParity, UnknownParity }//奇偶校验
enum PinoutSignal { NoSignal, TransmittedDataSignal, ReceivedDataSignal, DataTerminalReadySignal, ..., SecondaryReceivedDataSignal }
flags PinoutSignalsenum SerialPortError { NoError, DeviceNotFoundError, PermissionError, OpenError, ..., UnknownError }
enum StopBits { OneStop, OneAndHalfStop, TwoStop, UnknownStopBits } //停止位

函数
void QSerialPort::setPort(const QSerialPortInfo & serialPortInfo)
void QSerialPort::setPortName(const QString & name) //设置端口名称
qint32 baudRate(Directions directions = AllDirections) const
bool setBaudRate(qint32 baudRate, Directions directions = AllDirections)//设置波特率
DataBits
dataBits() const
bool setDataBits(DataBits dataBits)  //数据位设置
Parity parity() const
bool setParity(Parity parity)  //设置奇偶校验
StopBits stopBits() const
bool  setStopBits(StopBits stopBits)/设置停止位
FlowControl <span style="font-family: Arial, Helvetica, sans-serif;">flowControl() const</span>
bool setFlowControl(FlowControl flowControl) //设置硬件流
bool QSerialPort::open(OpenMode mode)<span style="white-space:pre"> </span>//打开串口
打开方式
QIODevice::NotOpen  0x0000  The device is not open.
QIODevice::ReadOnly 0x0001  The device is open for reading.
QIODevice::WriteOnly  0x0002 The device is open for writing. Note that this mode implies Truncate.
QIODevice::ReadWrite  ReadOnly | WriteOnly  The device is open for reading and writing.
QIODevice::Append  0x0004 The device is opened in append mode so that all data is written to the end of the file.
QIODevice::Truncate  0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost. 
QIODevice::Text  0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed. virtual 
void close()//关闭
QByteArray QIODevice::readAll()//读函数
qint64 QIODevice::write(const char * data) 
qint64 QIODevice::write(const QByteArray & byteArray)

qint64 QIODevice::write(const char * data, qint64 maxSize)//写函数
1、与编写网络一样在 .pro 文件中添加
QT += serialport

 
 
2、使用的库
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
 

</pre><pre name="code" class="html">#ifndef SERIALPORT_H
#define SERIALPORT_H
#include <QMainWindow>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QByteArray>
#include <QTimer>
namespace Ui {
class SerialPort;
}
class SerialPort : public QMainWindow
{
    Q_OBJECT
public:
    explicit SerialPort(QWidget *parent = 0);
    ~SerialPort();
private slots:
    void on_OpenPushButton_clicked();
    void on_ClosePushButton_clicked();
    void on_SendPushButton_clicked();
    void Updates();
private:
    Ui::SerialPort *ui;
    QSerialPort *mySerialPort;
    QByteArray requestData;
    QTimer *timer;
    void sendData();
};
#endif // SERIALPORT_H

#include "serialport.h"
#include "ui_serialport.h"
#include <QDebug>
SerialPort::SerialPort(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SerialPort)
{
    ui->setupUi(this);
    ui->baudRatecomboBox->setCurrentIndex(5);
    ui->DataBitsComboBox->setCurrentIndex(3);
    ui->parityComboBox->setCurrentIndex(0);
    ui->stopBitsComboBox->setCurrentIndex(0);
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        qDebug()<<"Name        :"<<info.portName();
        qDebug()<<"Description :"<<info.description();
        qDebug()<<"Manufacturer:"<<info.manufacturer();
        QSerialPort serial;
        serial.setPort(info);
        if(serial.open(QIODevice::ReadWrite))
        {
            ui->ProtsComboBox->addItem(info.portName());
            serial.close();
        }
    }
}
SerialPort::~SerialPort()
{
    mySerialPort->close();
    delete ui;
}
void SerialPort::on_OpenPushButton_clicked()
{
    mySerialPort = new QSerialPort();
    qDebug()<<ui->ProtsComboBox->currentText();
    mySerialPort->setPortName(ui->ProtsComboBox->currentText());
    mySerialPort->open(QIODevice::ReadWrite);
    mySerialPort->setBaudRate(ui->baudRatecomboBox->currentText().toInt());
    //数据位
    int dataBitNumber = ui->DataBitsComboBox->currentText().toInt();
    switch(dataBitNumber)
    {
        case 5:
            mySerialPort->setDataBits(QSerialPort::Data5);
             break;
        case 6:
            mySerialPort->setDataBits(QSerialPort::Data6);
            break;
        case 7:
            mySerialPort->setDataBits(QSerialPort::Data7);
            break;
        case 8:
            mySerialPort->setDataBits(QSerialPort::Data8);
            break;
         default:
            break;
    }
    //
    QSerialPort::NoParity;
    QSerialPort::Baud1200;
    mySerialPort->setParity(QSerialPort::NoParity);//无奇偶校验
    if(ui->stopBitsComboBox->currentText() == "1")
    {
        mySerialPort->setStopBits(QSerialPort::OneStop);
    }
    else if(ui->stopBitsComboBox->currentText() =="1.5")
    {
        mySerialPort->setStopBits(QSerialPort::OneAndHalfStop);
    }
    else if(ui->stopBitsComboBox->currentText() =="2")
    {
        mySerialPort->setStopBits(QSerialPort::TwoStop);
    }
    mySerialPort->setFlowControl(QSerialPort::NoFlowControl);
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(Updates()));
    timer->start(1000);
}
void SerialPort::on_ClosePushButton_clicked()
{
    mySerialPort->close();
}
void  SerialPort::Updates()
{
    qDebug()<<"this is a test!";
    requestData = mySerialPort->readAll();
    if(requestData !=NULL)
    {
        ui->datasTextEdit->append(requestData);
    }
    requestData.clear();
}
void SerialPort::on_SendPushButton_clicked()
{
    mySerialPort->write(ui->sendLineEdit->text().toLatin1());
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值