Post on 30-Dec-2015
description
How to Develop Qt ProgramSimple Examples (5)
2011. 2 . 2Mr. Kwon
• I2C Control EEPROM
• Serial Communication Simple Serial Communication Program
GPS Receiver
• UDP Socket Timer Server/Client
• Mp3 Player
Contents
Simple Qt Example for MINI6410
I2C -EEPROM
I2C-EEPROM
Create New Project (Abstract)
Qt4 Application Type Projects >> Qt4 Gui Application
Project Name I2C
Project Location /nfs
Class Name MyWidget
Base Class QWidget
Window Size (LCD) 420 X 240 (Width-Height)
•Create New Project
Design UI
Window Size : 420 X 240 Design the form below with: - QLabel - QPushButton - QTextEdit
QLabel
PushButton
TextEdit
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class (Details)
#-------------------------------------------------## Project created by QtCreator 2011-02-08T10:49:57##-------------------------------------------------
TARGET = I2CTEMPLATE = app
SOURCES += main.cpp\ mywidget.cpp
HEADERS += mywidget.h
FORMS += mywidget.ui
I2C.pro
Implementing the MyWidget Class (Details)
mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H
#include <QWidget>#include <QMessageBox>#include <QFileDialog>
#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <getopt.h>#include <errno.h>#include <string.h>
namespace Ui { class MyWidget;}
class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();
protected: void changeEvent(QEvent *e);
private: Ui::MyWidget *ui; int m_fd; int fd; int value;
private slots: void on_close_clicked(); void on_read_clicked(); void on_write_clicked(); void update();};#endif // MYWIDGET_H
Implementing the MyWidget Class (Details)
main.cpp
#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}
Implementing the MyWidget Class (Details)
mywidget.cpp (1/4)
#include "mywidget.h"#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this); value=0;
setWindowTitle("I2C EEPROM on MINI6410");
m_fd = ::open("/dev/i2c/0",O_RDONLY); if(m_fd<0){ QMessageBox::information(this, "ERROR","Fail to open /dev/I2C"); return; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (2/4)
MyWidget::~MyWidget(){ delete ui; ::close(m_fd);}
void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (3/4)
void MyWidget::on_write_clicked(){ ::system("i2c -w >> temp.txt");
ui->textEdit->clear(); QFile file("temp.txt"); if(file.open((QFile::ReadOnly | QFile::Text))) { ui->textEdit->setPlainText(file.readAll()); } file.remove("temp.txt");
}
Implementing the MyWidget Class (Details)
mywidget.cpp (4/4)
void MyWidget::on_read_clicked(){ ::system("i2c -r >> temp.txt");
ui->textEdit->clear(); QFile file("temp.txt"); if(file.open((QFile::ReadOnly | QFile::Text))) { ui->textEdit->setPlainText(file.readAll()); } file.remove("temp.txt");}
void MyWidget::on_close_clicked(){ close();}
Run on Host PC
Run on MINI 6410
Simple Qt Example for MINI6410
Serial Communication
Serial Communication
Create New Project (Abstract)
Qt4 Application Type Projects >> Qt4 Gui Application
Project Name SerialCom
Project Location /nfs
Class Name MyWidget
Base Class QWidget
Window Size (LCD) 420 X 240 (Width-Height)
•Create New Project
Design UI
Window Size : 420 X 240 Design the form below with: - QLabel - QPushButton - QTextEdit
QLabel
PushButton
TextEdit
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class (Details)
#-------------------------------------------------## Project created by QtCreator 2011-02-04T19:08:46##-------------------------------------------------
TARGET = SerialComTEMPLATE = app
SOURCES += main.cpp\ mywidget.cpp
HEADERS += mywidget.h
FORMS += mywidget.ui
SerialCom.pro
Implementing the MyWidget Class (Details)
mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H
#include <QWidget>#include <QSocketNotifier>#include <QTimer>#include <QMessageBox>#include <QStringList>
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <linux/fs.h>#include <errno.h>#include <string.h>#include <termio.h>
namespace Ui { class MyWidget;}
class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();
protected: void changeEvent(QEvent *e);
private: Ui::MyWidget *ui;
int m_fd; QSocketNotifier *m_notifier; int openSerialPort();
Implementing the MyWidget Class (Details)
mywidget.hprivate slots: void on_send_clicked(); void remoteDataIncoming();};
#endif // MYWIDGET_H
Implementing the MyWidget Class (Details)
main.cpp
#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}
Implementing the MyWidget Class (Details)
mywidget.cpp (1/5)
#include "mywidget.h"#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this); m_fd = openSerialPort(); if(m_fd<0){ QMessageBox::warning(this, tr("Error"), tr("Fail to Open Serial Port!")); return; } m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this); connect(m_notifier, SIGNAL(activated(int)), this, SLOT(remoteDataIncoming())); setWindowTitle("Serial Communication");}
Implementing the MyWidget Class (Details)
mywidget.cpp (2/5)
MyWidget::~MyWidget(){ delete ui;
if(m_notifier){ delete m_notifier; m_notifier=0; }
if(m_fd>=0){ ::close(m_fd); m_fd=-1; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (3/5)
void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (4/5)
void MyWidget::on_send_clicked(){ QString text(ui->send_text->toPlainText()); if(text.isEmpty()){ return; } ::write(m_fd, text.toLatin1(), text.length()); ui->send_text->clear();}void MyWidget::remoteDataIncoming(){ char c; if(read(m_fd, &c, sizeof c) != 1){ QMessageBox::warning(this, tr("Error"), tr("Receive Error")); return; } ui->recv_text->insertPlainText(QString(char(c)));}
Implementing the MyWidget Class (Details)
mywidget.cpp (5/5)
int MyWidget::openSerialPort(){ int fd=-1; const char *devName="/dev/ttySAC0"; fd = ::open(devName, O_RDWR | O_NONBLOCK); if(fd<0){ return -1; } termios serialAttr; memset(&serialAttr, 0, sizeof serialAttr); serialAttr.c_iflag = IGNPAR; serialAttr.c_cflag = B9600 | HUPCL | CS8 | CREAD | CLOCAL; serialAttr.c_cc[VMIN] = 1; if(tcsetattr(fd, TCSANOW, &serialAttr) != 0){ return -1; } return fd;}
Run on Host PC
Run on MINI 6410
Simple Qt Example for MINI6410
GPS Receiver
GPS Receiver
GPS
GPS Module (UIGGUB01-R003) -Integrated Antena-Serial Interface- High Speed Acquisition (10Hz)- Small Size, Efficient Power Man-agement - Max. update : 4Hz- Sensitivity : Acquisition : -160 dBm Tracking : -160 dBm Cold starts : -160 dBm- Operating Temp. -40°C to 85°C- Protocol : NMEA
How to Connect GPS and Host PC
GPS Host PC
GPS Data Format (NMEA)
(GPRMS : Recommended Minimum Specific GNSS Data)
Create New Project (Abstract)
Qt4 Application Type Projects >> Qt4 Gui Application
Project Name MyGPS
Project Location /nfs
Class Name MyWidget
Base Class QWidget
Window Size (LCD) 420 X 240 (Width-Height)
•Create New Project
Design UI
Window Size : 420 X 240 Design the form below with: - Qlabel - QTextEdit
QLabel
TextEdit
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class (Details)
#-------------------------------------------------## Project created by QtCreator 2011-02-08T15:16:01##-------------------------------------------------
TARGET = MyGPSTEMPLATE = app
SOURCES += main.cpp\ mywidget.cpp
HEADERS += mywidget.h
FORMS += mywidget.ui
MyGPS.pro
Implementing the MyWidget Class (Details)
mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H
#include <QWidget>#include <QSocketNotifier>#include <QTimer>#include <QMessageBox>#include <QStringList>
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <linux/fs.h>#include <errno.h>#include <string.h>#include <termio.h>
namespace Ui { class MyWidget;}
class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();
protected: void changeEvent(QEvent *e);
private: Ui::MyWidget *ui;
int m_fd; QSocketNotifier *m_notifier; int fd; int openSerialPort();
Implementing the MyWidget Class (Details)
mywidget.hprivate slots: void remoteDataIncoming();};
#endif // MYWIDGET_H
Implementing the MyWidget Class (Details)
main.cpp
#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}
Implementing the MyWidget Class (Details)
mywidget.cpp (1/6)
#include "mywidget.h"#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this); setWindowTitle("My GPS"); m_fd = openSerialPort(); if(m_fd<0){ QMessageBox::warning(this, tr("Error"), tr("Fail to Open Serial Port!")); return; } m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this); connect(m_notifier, SIGNAL(activated(int)), this, SLOT(remoteDataIncoming())); openSerialPort();}
Implementing the MyWidget Class (Details)
mywidget.cpp (2/6)
MyWidget::~MyWidget(){ delete ui;
if(m_notifier){ delete m_notifier; m_notifier=0; }
if(m_fd>=0){ ::close(m_fd); m_fd=-1; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (3/6)
void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (4/6)
int MyWidget::openSerialPort(){ int fd=-1; const char *devName="/dev/ttyS1"; fd = ::open(devName, O_RDWR | O_NONBLOCK); if(fd<0){ return -1; } termios serialAttr; memset(&serialAttr, 0, sizeof serialAttr); serialAttr.c_iflag = IGNPAR; serialAttr.c_cflag = B9600 | HUPCL | CS8 | CREAD | CLOCAL; serialAttr.c_cc[VMIN] = 1; if(tcsetattr(fd, TCSANOW, &serialAttr) != 0){ return -1; } return fd;}
Implementing the MyWidget Class (Details)
mywidget.cpp (5/6)
void MyWidget::remoteDataIncoming(){ char c;
if(read(m_fd, &c, sizeof c) != 1){ QMessageBox::warning(this, tr("Error"), tr("Receive Error")); return; } else{ ui->status->setText("ttyS1 Connected"); }
QString tString = "";
tString = ui->textEdit_2->toPlainText();
Implementing the MyWidget Class (Details)
mywidget.cpp (6/6)
if(char(c)==13){ ui->textEdit_2->clear(); tString.clear(); } else{ tString.append(char(c)); ui->textEdit_2->setText(tString); } }
Run on Host PC
Run on MINI 6410
Simple Qt Example for MINI6410
UDP Socket Timer Server/Client
UDP Socket Timer Server
Create New Project (Abstract)
Qt4 Application Type Projects >> Qt4 Gui Application
Project Name UDPTimeServer
Project Location /nfs
Class Name MyWidget
Base Class QWidget
Window Size (LCD) 320 X 240 (Width-Height)
•Create New Project
Design UI
Window Size : 320 X 240 Design the form below with: - QLabel - QLineEdit - QPushButton
QLabel
LineEdit
PushButton
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class (Details)
#-------------------------------------------------## Project created by QtCreator 2011-02-08T19:46:56##-------------------------------------------------
QT += network
TARGET = UDPTimerServerTEMPLATE = app
SOURCES += main.cpp\ mywidget.cpp
HEADERS += mywidget.h
FORMS += mywidget.ui
UDPTimeServer.pro
Implementing the MyWidget Class (Details)
mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H
#include <QWidget>#include <QTimer>#include <QUdpSocket>#include <QDateTime>
namespace Ui { class MyWidget;}
class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();
protected: void changeEvent(QEvent *e);
private: Ui::MyWidget *ui;
QUdpSocket *udpSocket; QTimer *timer; int portNumber;
private slots: void start_clicked(); void brodcastingDiagram();};
#endif // MYWIDGET_H
Implementing the MyWidget Class (Details)
main.cpp
#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}
Implementing the MyWidget Class (Details)
mywidget.cpp (1/6)
#include "mywidget.h"#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this);
timer = new QTimer(this); udpSocket = new QUdpSocket();
connect(ui->start, SIGNAL(clicked()), this , SLOT(start_clicked())); connect(ui->stop, SIGNAL(clicked()), this, SLOT(close())); connect(timer, SIGNAL(timeout()), this, SLOT(brodcastingDiagram()));
setWindowTitle("Time UDP Server");}
Implementing the MyWidget Class (Details)
mywidget.cpp (2/6)
MyWidget::~MyWidget(){ delete ui;}
void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (3/6)
void MyWidget::start_clicked(){ portNumber = QString (ui->lineEdit->text()).toInt(); ui->start->setEnabled(false); timer->start(1000);}
Implementing the MyWidget Class (Details)
mywidget.cpp (3/6)
void MyWidget::brodcastingDiagram(){ QDateTime currentTime = QDateTime::currentDateTime(); QString time = currentTime.toString();
QByteArray datagram(time.toAscii()); ui->time_show->setText(time);
udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, portNumber);}
Run on Host PC
Run on MINI 6410
UDP Client
Create New Project (Abstract)
Qt4 Application Type Projects >> Qt4 Gui Application
Project Name UDPClient
Project Location /nfs
Class Name MyWidget
Base Class QWidget
Window Size (LCD) 420 X 240 (Width-Height)
•Create New Project
Design UI
Window Size : 420 X 240 Design the form below with: - QLabel - QLineEdit - QTextEdit - QPushButton
QLabel
TextEdit
PushButton
LineEdit
Implementing the MyWidget Class
mywidget.h
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class
mywidget.cpp
Implementing the MyWidget Class (Details)
#-------------------------------------------------## Project created by QtCreator 2011-02-08T20:13:19##-------------------------------------------------
QT += network
TARGET = UDPClientTEMPLATE = app
SOURCES += main.cpp\ mywidget.cpp
HEADERS += mywidget.h
FORMS += mywidget.ui
UDPClient.pro
Implementing the MyWidget Class (Details)
mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H
#include <QWidget>#include <QTimer>#include <QUdpSocket>#include <QTextCodec>
namespace Ui { class MyWidget;}
class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();
protected: void changeEvent(QEvent *e);
private: Ui::MyWidget *ui; QUdpSocket *udpSocket; int portNumber;
private slots: void processPendingdatagram(); void startClient();};
#endif // MYWIDGET_H
Implementing the MyWidget Class (Details)
main.cpp
#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}
Implementing the MyWidget Class (Details)
mywidget.cpp (1/6)
#include "mywidget.h"#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this);
//codec = QTextCodec::codecForName("eucKR");
udpSocket = new QUdpSocket(this); connect(ui->start, SIGNAL(clicked()), this, SLOT(startClient())); connect(ui->close, SIGNAL(clicked()), this, SLOT(close()));
setWindowTitle("Time UDP Client");}
Implementing the MyWidget Class (Details)
mywidget.cpp (2/6)
MyWidget::~MyWidget(){ delete ui;}
void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}
Implementing the MyWidget Class (Details)
mywidget.cpp (3/6)
void MyWidget::processPendingdatagram(){ while(udpSocket->hasPendingDatagrams()){ QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size());
ui->textEdit->append(datagram.data()); ui->start->setEnabled(false); }}void MyWidget::startClient(){ portNumber = QString(ui->lineEdit->text()).toInt(); udpSocket->bind(portNumber); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingdatagram()));}
Run on Host PC
Run on MINI 6410