วันพฤหัสบดีที่ 17 กันยายน พ.ศ. 2558

qt database ตัวอย่าง 2

ก่อนเขียน code ต้องแก้ไข .pro ด้วยนะ
QT += sql




#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql>
#include <QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("/home/kong/test-console-application/test");
    if (!db.open()){
        qDebug() << db.lastError() << endl;
    }
    else{
        qDebug()<<"open database" << endl;
    }

    qDebug() << db.tables();
    QSqlQuery query;
    query.exec("select * from hose");

    while(query.next()){
        int hose_id = query.value(0).toInt();
        qDebug() << "hose_id : " << hose_id << endl;
        int pump_id = query.value(1).toInt();
        qDebug() << "pump_id : " << pump_id << endl;
        int hose_number = query.value(2).toInt();
        qDebug() << "hose_number : " << hose_number << endl;
    }

    return a.exec();
}

qt sqlite ตัวอย่าง 1

ก่อนเขียน code ต้องแก้ไข .pro ด้วยนะ
QT += sql


#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    serial = new QSerialPort();
    serial->setPortName("ttyS0");

    if (!QSqlDatabase::drivers().contains("QSQLITE")){
        qDebug()<< "not support database" << endl;
    }
    else{
        qDebug() << "support QSQLITE"<< endl;
    }

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test");
    if (!db.open()){
        qDebug()<<"can't open database"<< endl;
    }
    else{
        qDebug()<<"open database" << endl;
    }

    return a.exec();
}

ลง sqlite3

sudo apt-get install sqlite3 libsqlite3.dev
หลังจากลงเสร็จแล้ว ให้พิมพ์ sqlite3 จะเห็นเวอร์ชัน 3.8.7.4
ถ้าต้องการออก ให้พิมพ์ .quit

ต่อมาเราต้องการ ui เพื่อให้ใช้งาน database ได้ง่ายๆ ก็ต้องลง sqlite man จาก software center


วันอังคารที่ 8 กันยายน พ.ศ. 2558

serial port with custom baurate (non-standard baudrate) example

หมายเหตุ
ใน file.pro ให้เพิ่ม code ต่อไปนี้
QT += serialport

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QCoreApplication>
#include <QTextStream>
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>
#include <QFile>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //ui->setupUi(this);
    serial = new QSerialPort(this);
    QTextStream out(stdout);

    serial->setPortName("ttyS0");

    if (serial->isOpen()){
        serial->close();
        out << "serial close" << endl;
    }

    //out << serial->portName() << endl;
    if (serial->open(QIODevice::ReadWrite)) {
        serial->setBaudRate(8563);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::EvenParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);

        serial->write("Hello world!!!");
        serial->waitForByteWritten(100);
        //out << "Test" << endl;
    }
    else{
        //out << "Can't open comport" << endl;
        QMessageBox::critical(this, tr("Error"), serial->errorString());
    }
    //serial->close();
    //out << "serial close2" << endl;
}

MainWindow::~MainWindow()
{
    delete ui;
}

รัน serial port

ในการเขียนโปรแกรมควบคุม serial port จะติดปัญหาว่าเมื่อรันโปรแกรมแล้วจะติด cannot open device หรือ permission deny ถึงแม้ว่าเราจะ sudo แล้วก็ตาม

มีวิธีแก้ปัญหาคือ

1. เขียนโปรแกรมให้เปิด device โดยใช้สิทธิ์ root

2. เข้า terminal แล้วพิมพ์ chmod o+rx /dev/ttyS0 แล้วเข้า terminal sudo ./xxx เหมือนเดิม
เพื่อที่จะเปิดสิทธิ์ให้ user ทั่วไปสามารถใช้ /dev/ttyS0  read / execute ได้