Important: Please read the Qt Code of Conduct -

I need to transmit data in 8-bit format via rs232. I am getting errors . please kindly help me.

  • void SerialConnect::write(const QString data) {
        QBitArray dataArray1(8);
        qint64 bytesWritten = serial->write(dataArray1);
        if (bytesWritten == (-1)) {
            qDebug() << "Error writing data to serial port: " << serial->errorString();
        qDebug() << "SerialConnect::write(QString) Text sent as QByteArray: " << dataArray1;
        qDebug() << "Number of bytes written: " << bytesWritten;

  • Hi, arrays start with [0] not [1]. Also I don't think Qt's serialport class supports writing a QBitArray, try using a QByteArray :-)

  • @hskoglund
    thanks for the reply , But i need to transmit data as 8-bit . Is it possible to send in bytes. By converting bits as hexa decimals or decimals

  • As you see in the docs you can send char buffer through serial port.
    Probably you want to have something like:

    //0-indexed bit position
    void setBit(uint bit, bool value, char& buffer)
        if(bit < 8)
            buffer |= (value << bit);

    Since char is one byte you can imagine how this function works:

    char data = 0b00000000;
    setBit(3, true, data);
    //now data is 0b00001000;

    Now to the sending part:

    //1st option
    QByteArray byteToSend(1, data); //data from previous part
    //2nd option
    char byteToSend[1] = {data};
    serial->write(byteToSend, 1);

    Of course you can define byte you wanna write in the beginning assuming your example you can have something like:

    char dataArray = 0b00100110;

    and send this one somehow.
    For more bitwise operators you can check here: or just google it :)

  • thank you very much mr michelson . Its is working , i am able to transmit the bit data. And could please tell me how to send the data on after another, Because rs232 serialport can not transmit all at a time. Could you please write me a sample code.
    thanks in advance.

  • You can just pass larger QByteArray or char* with all the data you need.
    For user controlled flow you can try sending new data as above after eg. button click (so somewhere in apropriate slot probably).
    Or just

    QByteArray data = ...//fill
    data = ...//again fill

    and so on (mayby in loop?)

Log in to reply