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. [Solved] Crash when i try serial->open with QSerialPort

[Solved] Crash when i try serial->open with QSerialPort

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 10.6k 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.
  • M Offline
    M Offline
    moratilla
    wrote on last edited by
    #1

    Hi, I'm trying to send/receive data via 2 computers connected through a serial port cable (rs232). I'm totally newbie with QSerialPort (even with Qt and C++) and I can't set up the connection. I googled but I didn't find any solution. The code that I'm trying is the next:

    @ QSerialPort serial;

    serial = new QSerialPort(this);
    
    qDebug() << "Number of serial ports:" << QSerialPortInfo::availablePorts().count();
    
    
    foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) {
            qDebug() << "\nPort:" << serialPortInfo.portName();
    }
    
    
    serial->setPortName("/dev/ttyS0");
    serial->setBaudRate(QSerialPort::Baud9600);
    qDebug() << "arrives there";
    serial->open(QIODevice::ReadWrite);
    qDebug() << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    

    @
    And the output is the next:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is serialPort(0x18864d0), parent's thread is QThread(0x11a9aa0), current thread is serialPort(0x18864d0)
    Number of serial ports: 1
    Port: "ttyS0"
    bool QSerialPort::setBaudRate(qint32, QSerialPort::Directions): device not open
    arrives there
    The program has unexpectedly finished.

    I tried another example that I found on the web, and it also crashes on "if (port->open(QIODevice::ReadWrite))".

    Any suggestion? And sorry if this is a dumb question.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kqt-mr-nv6
      wrote on last edited by
      #2

      bq.
      QSerialPort serial;
      serial = new QSerialPort(this);

      is 'serial' a normal object or a pointer ?
      you are assigning a dynamic memory address
      to a normal variable !

      bq.
      serial->setBaudRate(QSerialPort::Baud9600);
      qDebug() << "arrives there";
      serial->open(QIODevice::ReadWrite);

      you have to first open the port
      and then set the baud rate

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moratilla
        wrote on last edited by
        #3

        I have the same error using as a pointer (QSerialPort *serial).

        I have the same error without setBaudRate sentence.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kqt-mr-nv6
          wrote on last edited by
          #4

          ok

          change

          bq.
          QSerialPort serial;

          to
          QSerialPort *serial;

          refer this post for settings
          "http://qt-project.org/forums/viewthread/34592/":http://qt-project.org/forums/viewthread/34592/

          try 'echo' some data directly to '/dev/ttyS0' and
          check the serial port working using minicom/cutecom
          if port is found ok
          QSerialPort should work
          I am using it from since it was launched

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kqt-mr-nv6
            wrote on last edited by
            #5

            from the output

            bq.
            QObject: Cannot create children for a parent that is in a different thread.

            it seems you are doing this in a thread

            bq.
            serial = new QSerialPort(this);

            you must not create objects in a QThread using the QThread object itself as their parent

            1 Reply Last reply
            0
            • M Offline
              M Offline
              moratilla
              wrote on last edited by
              #6

              [quote author="kqt-mr-nv6" date="1383907587"]ok

              change

              bq.
              QSerialPort serial;

              to
              QSerialPort *serial;

              refer this post for settings
              "http://qt-project.org/forums/viewthread/34592/":http://qt-project.org/forums/viewthread/34592/

              try 'echo' some data directly to '/dev/ttyS0' and
              check the serial port working using minicom/cutecom
              if port is found ok
              QSerialPort should work
              I am using it from since it was launched

              [/quote]

              I have installed cutecom on both computers connected through ttyS0, both have same configuration and I can send messages from mine to my mate's comp (and he reads them correctly), but not on the other side (him to me). I don't know what this means.

              About the "this" constructor parameter, I don't have the "children-parent" error if I just type "new QSerialPort()", but I still have the same problem. I'm not sure what I'm suppossed to type on that parameter.

              We want run the same program on both computers, one module of the program send messages and other module read them. Just saying for make more understable what we want to do.

              Sorry for my bad english and thank you for your time.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kqt-mr-nv6
                wrote on last edited by
                #7

                bq.
                I can send messages from mine to my mate’s comp (and he reads them correctly), but not on the other side (him to me). I don’t know what this means.

                First check loop-back
                short 2,3 terminals on one side of the cable
                and connect other end to the system

                try to send some data, you will receive what ever you transmit
                try on both PC's

                if data is not received on both systems, then cable is not good
                otherwise there is some problem with the system port

                bq.
                I don’t have the “children-parent” error if I just type “new QSerialPort()”,

                if you pass nothing, parent is NULL so you will no more get the
                QObject error

                why are you opening serial port in a thread ?

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kuzulis
                  Qt Champions 2020
                  wrote on last edited by
                  #8
                  1. Where you take the QtSerialPort? From the git?
                  2. Do not use different threads if you don't know how use it.
                  3. Show more your code.

                  Note: The QtSeiralPort is not a thread-safe class yet!

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    moratilla
                    wrote on last edited by
                    #9

                    [quote author="kqt-mr-nv6" date="1383912692"]
                    bq.
                    I can send messages from mine to my mate’s comp (and he reads them correctly), but not on the other side (him to me). I don’t know what this means.

                    First check loop-back
                    short 2,3 terminals on one side of the cable
                    and connect other end to the system

                    try to send some data, you will receive what ever you transmit
                    try on both PC's

                    if data is not received on both systems, then cable is not good
                    otherwise there is some problem with the system port

                    bq.
                    I don’t have the “children-parent” error if I just type “new QSerialPort()”,

                    if you pass nothing, parent is NULL so you will no more get the
                    QObject error

                    why are you opening serial port in a thread ?
                    [/quote]

                    [quote author="kuzulis" date="1383916275"]1. Where you take the QtSerialPort? From the git?
                    2. Do not use different threads if you don't know how use it.
                    3. Show more your code.

                    Note: The QtSeiralPort is not a thread-safe class yet![/quote]

                    Well, i have news.

                    We tried the code on my mates comp and works fine, he is able to read with QSerialPort class from my computer (BUT i'm sending with NO QSerialPort). I copied his code to my computer and it still crashing when I try to open, when it didn't crash on the other comp.

                    We have the same machines (Ubuntu 64 bits VirtualBox) and installed Qt and QtSerialPort (from github) the same way both. My "/dev/ttyS0" seems works fine since I'm able to read the data that he sends through serialport (using cutecom).

                    Resuming:

                    • Cutecom works fine on both computers, we can receive data through rs232 from each other.
                    • The same code thats works fine on his computer (QtSerialPort object can open and read), crashes on mine when it tries to QSerialPortObject->open.
                    • Both computers have the same whole configuration.

                    Any idea of what happens here? Sorry if I can't explain better.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kqt-mr-nv6
                      wrote on last edited by
                      #10

                      try this example code and first make sure
                      that it works

                      "http://qt-project.org/doc/qt-5.1/qtserialport/terminal.html":http://qt-project.org/doc/qt-5.1/qtserialport/terminal.html

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        moratilla
                        wrote on last edited by
                        #11

                        [quote author="kqt-mr-nv6" date="1383991793"]
                        try this example code and first make sure
                        that it works

                        "http://qt-project.org/doc/qt-5.1/qtserialport/terminal.html":http://qt-project.org/doc/qt-5.1/qtserialport/terminal.html[/quote]

                        It crashes when i press connect button the same way as my project. The port is "ttyS0", the only available. The weird think about this is that I can receive data from ttyS0 if I use cutecom.

                        PS: I'm installing another Ubuntu machine and I'll try again there

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kqt-mr-nv6
                          wrote on last edited by
                          #12

                          ok, then there must be some problem with the system/os itself
                          you can check kernel debug messages,
                          using 'dmesg' command
                          try and check if any relevant info is present

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            moratilla
                            wrote on last edited by
                            #13

                            [quote author="kqt-mr-nv6" date="1383993829"]ok, then there must be some problem with the system/os itself
                            you can check kernel debug messages,
                            using 'dmesg' command
                            try and check if any relevant info is present [/quote]

                            Now ALL works fine in the new Virtual Machine. It's strange because I have the same config than in the other one. Shit happens.

                            Thank you really much for your time, the problem is solved.

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

                              2 moratilla,

                              you didn't answer this question:

                              bq. 1. Where you take the QtSerialPort? From the git?

                              Because it is important, because has been introduced a regression, see more: https://codereview.qt-project.org/#change,70634

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                moratilla
                                wrote on last edited by
                                #15

                                [quote author="kuzulis" date="1384025002"]2 moratilla,

                                you didn't answer this question:

                                bq. 1. Where you take the QtSerialPort? From the git?

                                Because it is important, because has been introduced a regression, see more: https://codereview.qt-project.org/#change,70634
                                [/quote]

                                Yes I downloaded with "git clone git://gitorious.org/qt/qtserialport.git"

                                Anyway, I don't know what was the problem because I made a new Virtual Machine, installed all the same way than in the other one and now it works fine.

                                Thank you again, br

                                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