• 3488阅读
  • 2回复

QserialPort练习简单运用成果 [复制链接]

上一主题 下一主题
离线harold00124
 

只看楼主 倒序阅读 楼主  发表于: 2016-11-07
小弟是个刚接触QT的新手 C++也是因为工作才重新拿起书本学习
portfors232.rar (6 K) 下载次数:63

老板出给我的作业是1K以下传输量的全双工RS232传输程式

由于传输量不大所以我没有用多线程的方式..话说多线程的QserialPort我到现在还是看不懂

使用设定
选择好RT 与TX后按下Set Port会将combbox选取的值设定到TXRX的设定值内并且清空所有textEdit的内容
设定好TXRX后 在lineEdit内输入想要传送的资料 会把资料传送到TX Port 并且在传送textEdit

还请大神多多关照 如可以的话可以稍为指点小弟多线程QserialPort就更千恩万谢了


以下代码区
文件



#include <QtSerialPort/QSerialPort>
#include <QSerialPortInfo>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);


    ~MainWindow();

private slots:
    void portreaddate();

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_comboBox_activated(const QString &arg1);

    void Setcom(int iBautRate, int iDataBits,QString chParity,QString chStopBits);

    void on_comboBox_6_activated(const QString &arg1);

private:
    Ui::MainWindow *ui;
    QComboBox *BuatRate;
    QComboBox *DataBits;
    QComboBox *Party;
    QComboBox *StopBit;
};

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
QSerialPort *serial;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
        {
            ui->comboBox->addItem(info.portName());//添加串口
            ui->comboBox_6->addItem(info.portName());
        }
    serial=new QSerialPort(this);


    BuatRate=ui->comboBox_2;
    DataBits=ui->comboBox_3;
    Party=ui->comboBox_4;
    StopBit=ui->comboBox_5;

    connect(serial,SIGNAL(readyRead()),this,SLOT(portreaddate()));

}

MainWindow::~MainWindow()
{
    delete ui;
    serial->close();
}
void MainWindow::portreaddate(){
    QByteArray Rx;
    Rx=serial->readAll();
    ui->textEdit->setText(ui->textEdit->toPlainText()+Rx);
}

void MainWindow::on_pushButton_clicked()
{
    Setcom(BuatRate->currentIndex(),
           DataBits->currentIndex(),
           Party->currentText(),
           StopBit->currentText());
}


void MainWindow::on_pushButton_2_clicked()
{\

    serial->write(ui->lineEdit->text().toStdString().data());
    ui->textEdit_2->setText(ui->textEdit_2->toPlainText()+ui->lineEdit->text().toStdString().data());
}

void MainWindow::Setcom(int iBautRate,int iDataBits,QString chParity,QString chStopBits)
{
    serial->clear();
    ui->textEdit->clear();
    ui->textEdit_2->clear();
    serial->close();
    serial->setPortName(ui->comboBox->currentText());
    serial->open(QIODevice::ReadWrite);


    switch (iBautRate) {
    case 2400:
        serial->setBaudRate(QSerialPort::Baud2400);
        break;
    case 4800:
        serial->setBaudRate(QSerialPort::Baud4800);
        break;
    case 9600:
        serial->setBaudRate(QSerialPort::Baud9600);
        break;
    case 19200:
        serial->setBaudRate(QSerialPort::Baud19200);;
        break;
    case 38400:
        serial->setBaudRate(QSerialPort::Baud38400);
        break;
    case 57600:
        serial->setBaudRate(QSerialPort::Baud57600);
        break;
    case 115200:
        serial->setBaudRate(QSerialPort::Baud115200);
        break;
    }


    switch (iDataBits) {
    case 8:
        serial->setDataBits(QSerialPort::Data8);
        break;
    case 7:
        serial->setDataBits(QSerialPort::Data7);
        break;
    case 6:
        serial->setDataBits(QSerialPort::Data6);
        break;
    case 5:
        serial->setDataBits(QSerialPort::Data5);
        break;}

    if(chParity=="None") {
        serial->setParity(QSerialPort::NoParity);
     }
    else if(chParity=="Odd"){
        serial->setParity(QSerialPort::OddParity);
     }
    else if(chParity=="Even"){
        serial->setParity(QSerialPort::EvenParity);
     }
    else if(chParity=="Mark"){
        serial->setParity(QSerialPort::MarkParity);
     }
    else if(chParity=="Space"){
        serial->setParity(QSerialPort::SpaceParity);
     }


    if(chStopBits=="OnStop"){
        serial->setStopBits(QSerialPort::OneStop);
     }
    else if (chStopBits=="OneAndHalfStop"){
        serial->setStopBits(QSerialPort::OneAndHalfStop);
     }
    else if(chStopBits=="TwoStop") {
        serial->setStopBits(QSerialPort::TwoStop);
     }

}

void MainWindow::on_comboBox_6_activated(const QString &arg1)
{

}

void MainWindow::on_comboBox_activated(const QString &arg1)
{

}


实际执行状况
window.open('http://www.qtcn.org/bbs/attachment/Mon_1611/92_176460_558a0cebc9bfae6.jpg?316');" style="max-width:700px;max-height:700px;" onload="if(is_ie6&&this.offsetWidth>700)this.width=700;" >

离线白马

只看该作者 1楼 发表于: 2016-12-15
离线abrhai

只看该作者 2楼 发表于: 11-22
快速回复
限100 字节
 
上一个 下一个