Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] QtSerialPort



  • Hi,

    I need some help from someone to resolve one issue I have in my application.

    I'm using Windows 7 and Qt 5.1 beta, because it has qtserialport built in.

    I need to receive a data array, from a PIC18F, for example: 0 | 1 | 3 | 121 | 50 | 99 other is 0 | 2 | 2 | 10 | 150

    to detect the start i use 0, then message identifier (1 or 2), next the number of data bytes and finally the data bytes (data will change). I receive a complete data message every 10ms.

    Because i need to receive several data and display it in a GUI app, i decided to use QThread, that will emit a signal when i receive the complete data message.

    I modified the blockingslave example to do it. I managed to do so, using:

    • readthread.h

    @ signals:
    void newdata(const QString &s);@

    • readthread.cpp

    @emit this->newdata(msg);@

    msg is a QString.

    In my gui implementation:

    • mainwindow.h

    @private slots:
    void processData(const QString &s);@

    • mainwindow.cpp

    @thread.start();

    connect(&thread, SIGNAL(newdata(QString)),this, SLOT(processData(QString)));
    

    @

    @void MainWindow::processData(const QString &s)
    {
    QByteArray msg = s.toLocal8Bit();

    ui->txt1_1->setText(QByteArray::number(msg.at(0)));

    ui->txt1_2->setText(QByteArray::number(msg.at(1)));

    ui->txt1_3->setText(QByteArray::number(msg.at(2)));

    // ui->txt1_4->setText(QByteArray::number(msg.at(3)));

    // ui->txt1_5->setText(QByteArray::number(msg.at(4)));
    }@

    My problem is that when i update my Qtextedit, if i only show msg.at(0), i get 0, if i show msg.at(1), it changes 1/2 as it supposed to, if i show msg.at(2), it should show 2 or 3, but it shows sometimes 2, sometimes 3, sometimes 0, sometimes 30, if i try to show any of the other data, the program crashes and shows the following output:

    ASSERT: "uint(i) < uint(size())" in file ............\Qt\Qt5.1.0\5.1.0-beta1\mingw47_32\include/QtCore/qbytearray.h, line 401
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.

    But if i Debug the program and see the variable msg, in this variabe i have the correct values. So the problem might when i update the Qtextedit.

    Can someone help me with it?

    Thanks



  • I don't know if it might be the problem but my thought is that QString is not the correct type to use for the message. Since 0 (decimal) is the end-of-string character or string termination char I think that when you convert the string to a QByteArray the byte array will only contain the chars up to the point of the 1st 0d. Why do you not send a QByteArray directly through the signal?

    When constructing the QByteArray for sending through the signal, make sure to use the constructor that uses a size to set the number of bytes, otherwise you might have the same problem again.



  • [quote author="Badger" date="1370501839"]I don't know if it might be the problem but my thought is that QString is not the correct type to use for the message. Since 0 (decimal) is the end-of-string character or string termination char I think that when you convert the string to a QByteArray the byte array will only contain the chars up to the point of the 1st 0d. Why do you not send a QByteArray directly through the signal?

    When constructing the QByteArray for sending through the signal, make sure to use the constructor that uses a size to set the number of bytes, otherwise you might have the same problem again. [/quote]

    Thanks for your input.
    I think the problem might not be that, because, when i put a breakpoint and debug the code, i get the array as i supposed to.

    I've attached some images, of my debug. I have a variable: transactionCount which is incremented every time i run:

    @void MainWindow::processData(const QString &s)
    {

    QByteArray msg = s.toLocal8Bit();

    ui->txt1_1->setText(QByteArray::number(msg.at(0)));

    ui->txt1_2->setText(QByteArray::number(msg.at(1)));

    ui->txt1_3->setText(QByteArray::number(msg.at(2)));

    // ui->txt1_4->setText(QByteArray::number(msg.at(3)));

    // ui->txt1_5->setText(QByteArray::number(msg.at(4)));

    ui->trafficLabel->setText(tr("Traffic, transaction #%1:").arg(++transactionCount));
    ui->statusLabel->setText(tr("Status: Running, connected to port %1.").arg("COM3"));

    }@

    "Debug_1":https://www.dropbox.com/s/lc7ovufebrywk1h/Debug_1.png

    "Debug_2":https://www.dropbox.com/s/xdg8btf4nj5dcdw/Debug_2.png

    "Debug_3":https://www.dropbox.com/s/ek733eczc4y8j0t/Debug_3.png

    "Debug_4":https://www.dropbox.com/s/do7wuray0nt1y2u/Debug_4.png

    "Debug_5":https://www.dropbox.com/s/91485n8ufo8r10j/Debug_5.png

    "Debug_6":https://www.dropbox.com/s/34y45oq16s1q992/Debug_6.png

    So debugging my code i get what i'm supposed to: 0 |1|3|121|65|99 or 0|2|2|30|110

    I want to show the int numbers.

    When i run with breakpoint in debug mode i get:

    Debugging starts
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x400 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x112 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x112 in read in psymtab, but not in symtab.)
    (Internal error: pc 0x112 in read in psymtab, but not in symtab.)
    Debugging has finished

    I ran it till transactionCount was 6, like in the pictures.

    It's weird because i get this error but, the variable msg is like i wanted.

    Any ideas?



  • I decided to try my code in Ubuntu and in Ubuntu it works.


Log in to reply