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. Cannot open any (linux) serial ports
QtWS25 Last Chance

Cannot open any (linux) serial ports

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 501 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 21 Apr 2022, 19:41 last edited by
    #1

    The attached code "finds" three kinds of serial ports but fails to open any.
    Are the port names valid ?
    Did I missed something in my code ?

    "ttyACM0"
    "ttyUSB0"
    "ttyS0"

       QString text;
        
        QSerialPortInfo info;
        QList<QSerialPortInfo> portList;
        QSerialPort serial;
        QElapsedTimer timer;
        BT_Utility_Library *BTUL = new BT_Utility_Library();
        ui->listWidget_3->clear();
        text = "Scan for avaiable serial ports  ";
       
        BTUL->FLOWCHART(
                    text ,
                    Q_FUNC_INFO,
                    ui->listWidget_3,
                    __LINE__
                    );
        portList = info.availablePorts();
        for(int i=0;i<portList.size();i++)
        {
            ui->listWidget_3->addItem(portList[i].portName());
            text = portList[i].portName();
            BTUL->FLOWCHART(
                        text ,
                        Q_FUNC_INFO,
                        ui->listWidget_3,
                        __LINE__
                        );
        }
        
        // try open all serial ports
        for(int i=0;i<portList.size();i++)
        {
            serial.setPortName(portList[i].portName() );
            text = "serial port  ";
            text += portList[i].portName() ;
            if(serial.open(QIODevice::ReadWrite))
            {
                text += " open OK " ;
            }
            else
            {
                text += " ! FAILED to open";
            }
        
            BTUL->FLOWCHART(
                        text ,
                        Q_FUNC_INFO,
                        ui->listWidget_3,
                        __LINE__
                        );
        }
        return;
    
    J 2 Replies Last reply 21 Apr 2022, 19:46
    0
    • A Anonymous_Banned275
      21 Apr 2022, 19:41

      The attached code "finds" three kinds of serial ports but fails to open any.
      Are the port names valid ?
      Did I missed something in my code ?

      "ttyACM0"
      "ttyUSB0"
      "ttyS0"

         QString text;
          
          QSerialPortInfo info;
          QList<QSerialPortInfo> portList;
          QSerialPort serial;
          QElapsedTimer timer;
          BT_Utility_Library *BTUL = new BT_Utility_Library();
          ui->listWidget_3->clear();
          text = "Scan for avaiable serial ports  ";
         
          BTUL->FLOWCHART(
                      text ,
                      Q_FUNC_INFO,
                      ui->listWidget_3,
                      __LINE__
                      );
          portList = info.availablePorts();
          for(int i=0;i<portList.size();i++)
          {
              ui->listWidget_3->addItem(portList[i].portName());
              text = portList[i].portName();
              BTUL->FLOWCHART(
                          text ,
                          Q_FUNC_INFO,
                          ui->listWidget_3,
                          __LINE__
                          );
          }
          
          // try open all serial ports
          for(int i=0;i<portList.size();i++)
          {
              serial.setPortName(portList[i].portName() );
              text = "serial port  ";
              text += portList[i].portName() ;
              if(serial.open(QIODevice::ReadWrite))
              {
                  text += " open OK " ;
              }
              else
              {
                  text += " ! FAILED to open";
              }
          
              BTUL->FLOWCHART(
                          text ,
                          Q_FUNC_INFO,
                          ui->listWidget_3,
                          __LINE__
                          );
          }
          return;
      
      J Offline
      J Offline
      JonB
      wrote on 21 Apr 2022, 19:46 last edited by JonB
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 21 Apr 2022, 19:48 last edited by
        #3

        Hi,

        Yes they are valid.

        What is likely happening is that your user may not be allowed to use them. You should check the groups it belongs to and see if the list contains the group that can access these devices.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • A Anonymous_Banned275
          21 Apr 2022, 19:41

          The attached code "finds" three kinds of serial ports but fails to open any.
          Are the port names valid ?
          Did I missed something in my code ?

          "ttyACM0"
          "ttyUSB0"
          "ttyS0"

             QString text;
              
              QSerialPortInfo info;
              QList<QSerialPortInfo> portList;
              QSerialPort serial;
              QElapsedTimer timer;
              BT_Utility_Library *BTUL = new BT_Utility_Library();
              ui->listWidget_3->clear();
              text = "Scan for avaiable serial ports  ";
             
              BTUL->FLOWCHART(
                          text ,
                          Q_FUNC_INFO,
                          ui->listWidget_3,
                          __LINE__
                          );
              portList = info.availablePorts();
              for(int i=0;i<portList.size();i++)
              {
                  ui->listWidget_3->addItem(portList[i].portName());
                  text = portList[i].portName();
                  BTUL->FLOWCHART(
                              text ,
                              Q_FUNC_INFO,
                              ui->listWidget_3,
                              __LINE__
                              );
              }
              
              // try open all serial ports
              for(int i=0;i<portList.size();i++)
              {
                  serial.setPortName(portList[i].portName() );
                  text = "serial port  ";
                  text += portList[i].portName() ;
                  if(serial.open(QIODevice::ReadWrite))
                  {
                      text += " open OK " ;
                  }
                  else
                  {
                      text += " ! FAILED to open";
                  }
              
                  BTUL->FLOWCHART(
                              text ,
                              Q_FUNC_INFO,
                              ui->listWidget_3,
                              __LINE__
                              );
              }
              return;
          
          J Offline
          J Offline
          JonB
          wrote on 21 Apr 2022, 20:00 last edited by JonB
          #4

          @AnneRanch
          When they fail to open try outputting https://doc.qt.io/qt-5/qserialport.html#error-prop, serial.error().

          This property holds the error status of the serial port

          The I/O device status returns an error code. For example, if open() returns false, or a read/write operation returns -1, this property can be used to figure out the reason why the operation failed.

          Maybe it's reporting QSerialPort::PermissionError or others from https://doc.qt.io/qt-5/qserialport.html#SerialPortError-enum. https://doc.qt.io/qt-5/qiodevice.html#errorString, serial.errorString() may have the text.

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Anonymous_Banned275
            wrote on 22 Apr 2022, 01:03 last edited by
            #5

            Perfect group participation solution - getting "Permission denied " error.
            THANKS

            1 Reply Last reply
            0

            1/5

            21 Apr 2022, 19:41

            • Login

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