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



  • 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.



  • 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



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

    I have the same error without setBaudRate sentence.



  • 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



  • 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



  • [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.



  • 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. 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 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?

    1. Do not use different threads if you don't know how use it.
    2. 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.





  • [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



  • 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 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.



  • 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 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.