QSerialPort opening & closing



  • we are writing simple serial communication application using QSerialPort in qt5.

    we have connected to a serial port, then disconnecting it in software.
    When we try to reconnect to same port that is still connected tp PC, it fails to open serial port.

    It is opened with:

    if (serPort->open(QIODevice::ReadWrite)) {	....	}
    else {	......	}
    
    

    To close serial port

    serPort->close();
    

    is used.

    shall i add additional checks in code.

    with thanks,



  • When serPort->open returns false, can you check what serPort->error() returns?



  • @o6a6r9v1p
    I doubt QSerialPort::close() clears out the port's settings (like setPort()), but you could at least examine that before you try to open() again....



  • @VRonin
    i may be wrong in putting problem.

    i have used a button to connect/disconnect serial port.
    when I press button1, i am able to connect to selected port first time.

    and i am closing the same port after few seconds.

    Then i tried to open same port again, it gives error, i did not go into type of error.

    when i try to connect to other serial ports in PC, it connects correctly(only first time, later on same issue).

    thanks



  • Hi VRonin,

    checked as you told. I got following result:

    Connected....  :  "COM10"
    Baudrate.....  :  9600
    Disconnected....  :  "COM10"
    Baudrate.....  :  QSerialPort::SerialPortError(PermissionError)
    Failed....  :  "COM10"
    

    what is to be looked at.

    Thanks



  • @JonB ,
    you mean portname, baud rate etc.
    we have to store them, compare & do open again, is it like that.

    Thanks



  • From http://doc.qt.io/qt-5/qserialport.html#SerialPortError-enum:

    An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open.

    Are you opnening the same serial in multiple threads/processes?



  • @VRonin
    cross check the code and come back.
    Thank you



  • @VRonin
    open() is used only once in the program. I am sure about it.

    Is the second part of your quote "the reason" for this problem.

     a user not having enough permission and credentials to open
    

    thanks.



  • @JonB
    I tried to check port data in debug mode. snap shot is given below:

    Init Entry Begin       ********** 
    Port     ....  :  "COM10"
    Baudrate.....  :  9600
    Databits.....  :  QSerialPort::DataBits(Data8)
    Stopbits.....  :  QSerialPort::StopBits(OneStop)
    Parity  .....  :  QSerialPort::Parity(NoParity)
    FlowControl..  :  QSerialPort::FlowControl(NoFlowControl)
    Init Entry  End      ********** 
    Connected....  : 
    Port     ....  :  "COM10"
    Baudrate.....  :  9600
    Databits.....  :  QSerialPort::DataBits(Data8)
    Stopbits.....  :  QSerialPort::StopBits(OneStop)
    Parity  .....  :  QSerialPort::Parity(NoParity)
    FlowControl..  :  QSerialPort::FlowControl(NoFlowControl)
    

    the above is the first part, ouput obtained when it opens COM10 first time.
    "Init Entry" shows data when we enter the function.
    followed by data we get connection to COM10.

    
    Init Entry Begin       ********** 
    Port     ....  :  "COM10"
    Baudrate.....  :  9600
    Databits.....  :  QSerialPort::DataBits(Data8)
    Stopbits.....  :  QSerialPort::StopBits(OneStop)
    Parity  .....  :  QSerialPort::Parity(NoParity)
    FlowControl..  :  QSerialPort::FlowControl(NoFlowControl)
    Init Entry  End      ********** 
    Disconnected....  :  "COM10"
    

    The second part shows data we get when we disconnect COM10 port.

    
    Init Entry Begin       ********** 
    Port     ....  :  "COM10"
    Baudrate.....  :  9600
    Databits.....  :  QSerialPort::DataBits(Data8)
    Stopbits.....  :  QSerialPort::StopBits(OneStop)
    Parity  .....  :  QSerialPort::Parity(NoParity)
    FlowControl..  :  QSerialPort::FlowControl(NoFlowControl)
    Init Entry  End      ********** 
    Error.....  :  QSerialPort::SerialPortError(PermissionError)
    Failed....  : 
    Port     ....  :  "COM10"
    Baudrate.....  :  9600
    Databits.....  :  QSerialPort::DataBits(Data8)
    Stopbits.....  :  QSerialPort::StopBits(OneStop)
    Parity  .....  :  QSerialPort::Parity(NoParity)
    FlowControl..  :  QSerialPort::FlowControl(NoFlowControl)
    

    The third part shows data we get when we reconnect to COM10 again.

    As you suspected, data is not changing.



  • @JonB
    As you told, it is close() problem. i did it as given in qt examples.
    It is working now.

    Thanks to ALL forum members.


Log in to reply
 

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