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

Problem connecting to the serial port



  • I have a problem opening the COM1 and communication to the xbee, I came writing even a single character even read what I get, is that a person of good will could help me if please? this is urgent, thank you.
    here is my error message:
    "
    bool __thiscall QSerialPort::setBaudRate(int,class QFlags<enum QSerialPort::Direction>): device not open
    Message written
    "


  • Lifetime Qt Champion

    Hi,

    The question might seem silly but: did you call open on your serial device before setting its baud rate ?



  • yes I opened the front port to speed configuration, see the function:

    serial->open(QIODevice::ReadWrite);

    serial->setBaudRate(9600);
    
    if (serial->portName()!= "COM1") 
    

    {

        serial->close();
    
        serial->setPortName("COM1");
    
        if (!serial->open(QIODevice::ReadWrite)) 
    

    {

            processError(tr("Can't open %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
    
            return;
    
        }
    
        if (!serial->setBaudRate(QSerialPort::Baud19200)) 
    

    {
    processError(tr("Can't set rate 19200 baud to port %1, error code %2")
    .arg(serial->portName()).arg(serial->error()));

            return;
    
    
        }
    
        if (!serial->setDataBits(QSerialPort::Data8)) 
    

    {

            processError(tr("Can't set 8 data bits to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
    
        }
    
        if (!serial->setParity(QSerialPort::NoParity))
    

    {

            processError(tr("Can't set no patity to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
    
        }
    
        if (!serial->setStopBits(QSerialPort::OneStop))
    

    {

            processError(tr("Can't set 1 stop bit to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
    
            return;
    
        }
    
        if (!serial->setFlowControl(QSerialPort::NoFlowControl)) 
    

    {

            processError(tr("Can't set no flow control to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
    
            return;
    
        }
    }
    
    
    qDebug() << "Message written";
    
    this->msgRequest = "O";
    
    serial->write(msgRequest);
    
    serialTimer->start(400);


  • and I still have the same error;


  • Lifetime Qt Champion

    Please, enclose your code in coding tags (one @ at the beginning and one at the end) That will make it readable.

    You don't check that your first open did succeed



  • le code est :

    @ serial->open(QIODevice::ReadWrite);
    serial->setBaudRate(9600);

    if (serial->portName()!= "COM1") {
    
        serial->close();
        serial->setPortName("COM1");
    
        if (!serial->open(QIODevice::ReadWrite)) {
            processError(tr("Can't open %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
        }
    
        if (!serial->setBaudRate(QSerialPort::Baud19200)) {
            processError(tr("Can't set rate 19200 baud to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
        }
    
        if (!serial->setDataBits(QSerialPort::Data8)) {
            processError(tr("Can't set 8 data bits to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
        }
    
        if (!serial->setParity(QSerialPort::NoParity)) {
            processError(tr("Can't set no patity to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
        }
    
        if (!serial->setStopBits(QSerialPort::OneStop)) {
            processError(tr("Can't set 1 stop bit to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
        }
    
        if (!serial->setFlowControl(QSerialPort::NoFlowControl)) {
            processError(tr("Can't set no flow control to port %1, error code %2")
                         .arg(serial->portName()).arg(serial->error()));
            return;
        }
    }
    
    
    qDebug() << "Message written";
    this->msgRequest = "O";
    serial->write(msgRequest);
    serialTimer->start(400);@


  • you say that my first successful opening is to say ? and the fact that this feature is linked to a button QPusButton I said, I do not understand why I do not.


  • Lifetime Qt Champion

    @serial->open(QIODevice::ReadWrite);@

    This one, you just don't check that it worked,



  • I still have the same problem



  • it still does not work, what should I do?


  • Lifetime Qt Champion

    Did you check whether you have an error ? If so which one ?



  • Yes there is a mistake, because it does not even written on the port, it says that the port is not open, I can not communicate to a PC during a test of communication that I 'I write, I do not understand, help me please!


  • Lifetime Qt Champion

    Like I said:

    @serial->open(QIODevice::ReadWrite);@

    You don't check whether this call to open fails or not. Add the necessary if and and in the else part, print the error you are getting from serial.


Log in to reply