วันอาทิตย์ที่ 8 พฤศจิกายน พ.ศ. 2558

qt thread

https://youtu.be/JaGqGhRW5Ks
https://www.youtube.com/watch?v=yazMHbIew0Q
https://www.youtube.com/watch?v=yazMHbIew0Q&t=444s

วันจันทร์ที่ 19 ตุลาคม พ.ศ. 2558

วันศุกร์ที่ 2 ตุลาคม พ.ศ. 2558

ต่อข้อมูล string (qt append string)

การเชื่อมต่อ string เป็นเรื่องยากสำหรับผมมาก เพราะว่าหาตัวอย่างยังไงก็หาไม่เจอ
จริงๆแล้วมันไม่ได้ยากหรอก แต่เราใช้คำในการค้นหา ไม่ตรงกับความต้องการ(อีกแล้ว)

การเพิ่มข้อมูล string หรือการต่อข้อมูล string (Append string) สามารถทำได้ดังนี้

#include <QDebug>
#include <QString>
.
.
.
QString your_name = "Niran";
QString message = "Your name is " + your_name;
qDebug() << message;

หรือ
QString message = "Your name is ";
message += your_name;
qDebug() << message;

หรือ
QString message = "Your name is ";
message.append(your_name);
qDebug() << message;

ที่มา
http://www.qtcentre.org/threads/33603-how-to-concatenate-a-variable-with-a-string

convert int to string

วันนี้เขียนโปรแกรม ที่จำเป็นต้อง convert จาก int เป็น string
ตอนแรกก็งงอยู่พักใหญ่ ทำยังไงหว่า เลยถาม google เหมือนเดิม
แต่ถามครั้งแรกมันแสดงผลไรมาไม่รู้เยอะแยะ ไม่ตรงกับที่เราต้องการ
ก็ต้องเปลี่ยนคำถามใหม่ (สอนให้รู้ว่า ต้องตั้งคำถามให้ตรงคำตอบ) ก็จะได้คำตอบที่ต้องการ 555

วิธีเปลี่ยนจาก int เป็น string คือ
int i=42;
QString s = QString::number(i);

ที่มา
http://stackoverflow.com/questions/3211771/how-to-convert-int-to-qstring

วันพฤหัสบดีที่ 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 ได้

วันจันทร์ที่ 31 สิงหาคม พ.ศ. 2558

ย้าย software ไปใส่เครื่องใหม่

แค่อยากแบ่งประสบการณ์เฉยๆนะ ไม่ต้องคิดมาก ผิดถูกไม่แน่ใจ หรือหากใครเห็นว่าผมทำอะไรไม่ถูก อยากแบ่งปันความรู้ ก็เชิญแนะนำได้เลยครับ

หลังจากที่ compile เสร็จแล้ว เราจะได้ไฟล์นึงมา ที่เป็น application ให้เราสั่งใช้งานได้ ยกตัวอย่างเช่น sudo ./test-serial เป็นต้น
วันนี้ได้ลองย้าย appllication จากเครื่องหนึ่งไปอีกเครื่องนึง(ที่ลง qt ไว้แล้ว) ขั้นแรกที่ต้องทำคือ chmod +x test-serial แต่เมื่อทำเสร็จแล้ว ดันมี error ว่า ./test-serial: error while loading shared librries: libQt5SerialPort.so.5: cannot open share object file: No such file or directory

เมื่อวิธีนี้ใช้ไม่ได้ จึงลองวิธีอื่่นดู
ลองด้วยการเขียนโปรแกรม qt แล้วคอมไพล์ใหม่อีกรอบ (เขียนใหม่ทั้งหมด) แต่ยัง error อยู่

หาข้อมูลไปมา พบว่าเป็นที่วิธีการลง qt ถ้าลงจาก software center ดูเหมือนว่ามันจะลงให้ไม่ครบ
เลยลงแบบ download มาเองจากหน้าเวป ใช้เวลานานมากเนื่องจากเน็ตกาก เลยเปลี่ยนวิธีอีก เป็น download แบบ offline มาลง ก็ดูดีกว่าลงแบบ online, download เสร็จก็ install ใช้เวลาแปปเดียวเอง.
และเมื่อสร้างโปรเจคใหม่ก็ไม่มี error ด้วย.

แต่เมื่อทดลองเอาไฟล์เก่ามารันดู รันไม่ผ่าน คงเป็นเพราะลง qt คนละเวอร์ชันกัน.

สรุป
ยังไม่สามารถย้ายไฟล์ที่คอมไพล์มาจากเครื่องอื่น มาใช้กับเครื่องใหม่ ที่ลง qt คนละเวอร์ชันได้.
วิธีแก้ไข ยังไม่มี แต่คาดว่าให้หา .so มาลง(ที่เครื่องฟ้อง error) ให้ครบ น่าจะใช้งานได้เหมือนกัน.

เวลาลง qt แนะนำให้ download แบบ offline มา install จะประหยัดเวลากว่ามาก.(เฉพาะสถานที่ที่เน็ตกากนะ)

แต่ก็ติดปัญหาอีก เพราะว่าเมื่อเราปิดโปรแกรมแล้วเปิดใหม่ ไม่สามารถเปิดโปรเจคได้ เนื่องจากติด permission ต้องแก้ไขด้วย

sudo -s chmod o+w /home/pi-lan/.config/QtProject/qtcreator/*

แต่ก็ยังติด .pro.user permission ไม่หาย

เลยลองลงใหม่ แต่ลงแบบไม่ต้องมี sudo นำหน้า ก็ยังไม่สามารถเปิดได้ เลยลองสร้างโปรเจคใหม่ และเขียนใหม่หมด จึงสามารถใช้งานได้ แต่ติดปัญหาอีก ไม่สามารถใช้งาน QtSerial ได้

จึง suodo apt-get update
sudo apt-get upgrade
แล้ว reboot ก็ยังไม่ได้ผล

สรุป
ให้เข้า terminal แล้วไปยังโฟลเดอร์ /home/pi-lan/Qt5.2.0/tools/Qtcreator/bin แล้วพิมพ์
sudo ./qtcreator
เพื่อให้เราเปิดโปรแกรมโดยใช้สิทธิ์เป็น root แล้วจะสามารถเห็นและใช้ทุกฟังก์ชันได้

วันอังคารที่ 25 สิงหาคม พ.ศ. 2558

console example

console คือ หน้าจอ command

ตัวอย่างการเขียนโปรแกรมออก console

#include <QCoreApplication>
#include <QTextStream>

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

    out << "Test" << endl;

    return a.exec();

}


qvboxlaout example

QVBoxLayout คือ เครื่องมือในการช่วยเรียง object ต่างๆ ที่อยู่ใน UI ในแนวตั้ง

ตัวอย่างการใช้งาน QVBoxLayout

#include "mainwindow.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QPushButton>

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

    QWidget *window = new QWidget;

    QPushButton *button1 = new QPushButton("One");
    QPushButton *button2 = new QPushButton("Two");
    QPushButton *button3 = new QPushButton("Three");
    QPushButton *button4 = new QPushButton("Four");
    QPushButton *button5 = new QPushButton("Five");

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);

    window->setLayout(layout);
    window->show();

    //MainWindow w;
    //w.show();

    return a.exec();

}

ที่มา
http://doc.qt.io/qt-5/qvboxlayout.html

วันจันทร์ที่ 24 สิงหาคม พ.ศ. 2558

เริ่มต้นด้วยการ install ubuntu

เริ่มต้นจากการลง linux ubuntu ก่อนเลย

1. ด้วยการ download usb-installer มา
2. และตามด้วยการ download ubuntu 15.04-64bit.iso

3. จากนั้นทำตามนี้

http://www.pendrivelinux.com/universal-usb-installer-easy-as-1-2-3/#button

รอสักหน่อย

4. จากนั้นเอา usb ไปเสียบเครื่องที่เราจะลง ubuntu แล้วเปิดเครื่อง
5. ตั้งค่าเครื่องให้ boot จาก usb
5. ลงๆไปให้เสร็จ

ให้ download qt-opensource-linux-x64-5.2.1 จาก http://www.qt.io/download-open-source/



chmod 777 qt-opensource-linux-x64-5.2.1.run
จากนั้นลงโปรแกรม qt  โดยใช้คำสั่ง sudo ./qt-opensource-linux-x64-5.2.1.run
ลองเปิดโปรเจคใหม่ ลอง run ดูเลย โดยที่ไม่ได้เพิ่มเติมโค้ดใดๆ ปรากฎว่าขึ้น error : cannot find -lGL
ลอง sudo apt-get update, sudo apt-get upgarde ดู ก็ไม่หาย
หาในเน็ตดู บอกว่า sudo apt-get install libglu1-mesa-dev

แล้วลองรันดูใหม่ ใช้ได้เลย ผ่านฉลุย

ใส่ serialport เข้าไปใน .pro ก็ผ่านฉลุย

แต่เมื่อใส่ code terminal-serial ลงไป รันแล้วขึ้นแต่หน้าวินโดว์เปล่าๆ โชคดีเขียนโค้ดใหม่ก็เจอจุดที่ทำให้เป็นแบบนี้ ก็คือ ลืม copy mainwindow.h มา ลองคอมไพล์ใหม่หมด ก็รันขึ้นแต่ติด permission ต้องไปรัน sudo ใน terminal รันได้แต่ติด error device not open ก็แก้ไขโค้ดใหม่ เอาพวก error ย้ายมาไว้หลัง serial->open สามารถใช้งานได้ทั้งหมด รวมถึง custom baudrate ด้วย