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. QSerialPort opening & closing
Forum Updated to NodeBB v4.3 + New Features

QSerialPort opening & closing

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 5.8k Views
  • 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.
  • O Offline
    O Offline
    o6a6r9v1p
    wrote on last edited by
    #1

    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,

    JonBJ 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      O 1 Reply Last reply
      3
      • O o6a6r9v1p

        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,

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

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

        O 3 Replies Last reply
        2
        • VRoninV VRonin

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

          O Offline
          O Offline
          o6a6r9v1p
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • O Offline
            O Offline
            o6a6r9v1p
            wrote on last edited by
            #5

            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

            VRoninV 1 Reply Last reply
            0
            • JonBJ JonB

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

              O Offline
              O Offline
              o6a6r9v1p
              wrote on last edited by
              #6

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

              Thanks

              1 Reply Last reply
              0
              • O o6a6r9v1p

                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

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                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?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                O 2 Replies Last reply
                3
                • VRoninV VRonin

                  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?

                  O Offline
                  O Offline
                  o6a6r9v1p
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    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?

                    O Offline
                    O Offline
                    o6a6r9v1p
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      O Offline
                      O Offline
                      o6a6r9v1p
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • JonBJ JonB

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

                        O Offline
                        O Offline
                        o6a6r9v1p
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        1

                        • Login

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