Qt Serial
.pro#-------------------------------------------------
#
# Project created by QtCreator 2015-12-10T22:35:58#
#-------------------------------------------------
QT += core gui serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = SerialPlayqtTEMPLATE = appSOURCES += main.cpp\mainwindow.cpp
HEADERS += mainwindow.hFORMS += mainwindow.ui
*****************************************************************
.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QtSerialPort/QSerialPort>#include <QDebug>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_bt_connect_clicked();void readData();void on_bt_connect_2_clicked();private:
Ui::MainWindow *ui;QSerialPort *serial;};#endif // MAINWINDOW_H
*****************************************************************
ui->setupUi(this);
//---- COMPORT ------------------------------------------------foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {QStringList list;list << info.portName();ui->portnamebox->addItem(list.first(),list);}
*****************************************************************
void MainWindow::on_bt_connect_clicked(){if(serial->isOpen()){
serial->close();ui->bt_connect->setText("Connect");}
else{
serial->setPortName(ui->portnamebox->currentText());serial->setBaudRate(QSerialPort::Baud115200);serial->setDataBits(QSerialPort::Data8);serial->setFlowControl(QSerialPort::NoFlowControl);serial->setParity(QSerialPort::EvenParity);serial->setStopBits(QSerialPort::OneStop);if( serial->open(QIODevice::ReadWrite)){
ui->bt_connect->setText("Disconnect");}
else{
// QMessageBox::critical(this,tr("Error"),serial->errorString());QMessageBox::information(this,"Wrong com port" ,"Please check comport");}
}
}void MainWindow::readData(){responseData.append(serial->readAll());ui->logstext->append(responseData.toHex().toUpper());ReadTruck01(responseData);
qDebug() << "recive = " << responseData.append(serial->readAll()) ;}void MainWindow::on_bt_connect_2_clicked(){qDebug() << "click <----";QByteArray data;data[0] = 0x01;data[1] = 0x03;data[2] = 0x00;data[3] = 0x00;data[4] = 0x00;data[5] = 0x5F;serial->write(data);}
0 ความคิดเห็น:
แสดงความคิดเห็น
สมัครสมาชิก ส่งความคิดเห็น [Atom]
<< หน้าแรก