Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray corrupts my data ???Why???
Qt 6.11 is out! See what's new in the release blog

QByteArray corrupts my data ???Why???

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 7.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    achmed
    wrote on last edited by
    #1

    The below mentioned code works fine when placed inside of my MainWindow.cpp from where it is being called.

    But as soon as I move it to a class in a separate file the data being sent from the serial port corrupts.
    I Have narrowed it down to the <relay_arr.prepend("ch");> and <relay_arr.append("_onx");> commands.
    If I comment out these two lines the <Ch_on> is transmitted correctly.
    I'm not sure if this might be worth mentioning. The class does not inherit from any other classes.

    @QByteArray relay_arr;

    void RelayClass::RelayOn(int Ch_on)
    {
    relays.setPortName("COM1");
    relays.setBaudRate(QSerialPort::Baud9600);
    relays.setDataBits(QSerialPort::Data8);
    relays.setParity(QSerialPort::NoParity);
    relays.setStopBits(QSerialPort::OneStop);
    relays.setFlowControl(QSerialPort::NoFlowControl);
    relays.open(QIODevice::ReadWrite);

    relay_arr.clear();
    relay_arr.setNum(Ch_on);
    relay_arr.prepend("ch");
    relay_arr.append("_onx");
    relays.write(relay_arr);
    relays.waitForBytesWritten(-1);
    
    relays.close();
    

    }@

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vital
      wrote on last edited by
      #2

      try it:

      @relays.open(QIODevice::ReadWrite);
      relays.setBaudRate(QSerialPort::Baud9600);@

      (call setBaudRate method after port opening)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        achmed
        wrote on last edited by
        #3

        Nope... Still same problem.
        For some reason I don't grasp, This function tends to randomly give problems. It now seems to have stopped functioning in the main .cpp file as well, and I know it did work before.

        I tried cleaning the project.
        Changing Build directories.
        Deleting Build directories.
        Tested on other PCs.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          You should wait and check for the device to be ready to write.

          (Z(:^

          1 Reply Last reply
          0
          • A Offline
            A Offline
            achmed
            wrote on last edited by
            #5

            Ok. I have simplified the code to the bare bone, and still getting the same problem.
            But from what I can notice the problem does no longer seem to be related to the QByteArray.
            The code bellow also displays corrupted data.

            @#include <QtSerialPort>
            QSerialPort relays;

            void MainWindow::on_pushButton_clicked()
            {
            relays.setPortName("COM1");
            relays.setDataBits(QSerialPort::Data8);
            relays.setParity(QSerialPort::NoParity);
            relays.setStopBits(QSerialPort::OneStop);
            relays.setFlowControl(QSerialPort::NoFlowControl);
            relays.open(QIODevice::ReadWrite);
            relays.setBaudRate(QSerialPort::Baud9600);

            relays.write("all_offx");
            relays.waitForBytesWritten(-1);
            
            relays.close();
            

            }@

            1 Reply Last reply
            0
            • A Offline
              A Offline
              achmed
              wrote on last edited by
              #6

              bq. You should wait and check for the device to be ready to write. bq.

              The only function I can find that comes close to this is <WaitForReadyRead>

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                @
                if (relays.open(QSerialPort::ReadWrite) {
                // Set port settings here
                QByteArray relay_arr("ch" + QByteArray::number(Ch_on) + "_onx");
                relays.write(relay_arr);
                }
                @

                (Z(:^

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  achmed
                  wrote on last edited by
                  #8

                  Thanks Sierdzio.
                  I Will give your code a try tomorrow.

                  Although I have found a semi sort of solution.

                  If I add a 1second delay after opening the ports and also a 1second delay before closing them, then it works.
                  This tells me that the <relays.waitForBytesWriten(-1)> does not work.
                  As far as I know this function is supposed to pause until all data has been written to the device. Can anybody maybe comment on this statement?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kuzulis
                    Qt Champions 2020
                    wrote on last edited by
                    #9
                    1. What is version of QtSerialPort?
                    2. What is serial port type?
                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      achmed
                      wrote on last edited by
                      #10

                      Hi kuzulis. Im not sure where to check the version number for QtSerialPort.
                      The Serial port is a RS232 device.

                      Sierdzio. I implemented your above mentioned code.
                      No difference. The only thing I fount to work is if I add a 1sec delay after opening and before closing the port.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kuzulis
                        Qt Champions 2020
                        wrote on last edited by
                        #11

                        2 achmed

                        bq.
                        Im not sure where to check the version number for QtSerialPort.

                        From where you took the QtSerialPort?

                        bq. The Serial port is a RS232 device.

                        This is USB/Serial converter, on-board device or something else? What chip?

                        bq. The code bellow also displays corrupted data.

                        What do you see when do writing of relays.write("all_offx") ?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          achmed
                          wrote on last edited by
                          #12

                          Someone else did the installation of Qt on my PC.
                          Not sure where they got it from.

                          We are using more that one Type/Brand/Chip. They are all giving the same problem.

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kuzulis
                            Qt Champions 2020
                            wrote on last edited by
                            #13

                            bq. They are all giving the same problem.

                            What is a problem? I repeat a question again: what do you see when do writing of relays.write(“all_offx”) on other side?

                            bq. Not sure where they got it from.

                            It is in your interests to know it. Without it further reasonings are senseless.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              achmed
                              wrote on last edited by
                              #14

                              [quote author="kuzulis" date="1415359623"]What is a problem? I repeat a question again: what do you see when do writing of relays.write(“all_offx”) on other side?
                              [/quote]

                              As Mentioned in the first post. The output data is corrupted.
                              Example: ^%$%^&*(
                              In Other words unreadable.

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                kuzulis
                                Qt Champions 2020
                                wrote on last edited by
                                #15

                                This can not be in principle. Can you check same, but without closing of device?

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  achmed
                                  wrote on last edited by
                                  #16

                                  [quote author="achmed" date="1415284517"]If I add a 1second delay after opening the ports and also a 1second delay before closing them, then it works.[/quote]

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved