Quick Qt5 Example for GP3

I noticed Qt5 has a QtSerialPort class. Here’s a simple example of talking to the GP3  using this library:

 

    static int state=0;
    QSerialPort port("ttyUSB8",this);  // works with /dev/ttyUSB8 or ttyUSB8
    qDebug()<<port.setBaudRate(QSerialPort::Baud57600);
    qDebug()<<port.open(QIODevice::ReadWrite);
    qDebug()<<port.write(state?"\x0C":"\x0D",1);  // Turns on/off light
    state^=1;
    port.waitForBytesWritten(1000);
    port.close();

Pretty simple. You can read data back either blocking or using slots. I’ll cook up an example of that later.

Leave a Reply