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 - OpenError [solved]
Forum Updated to NodeBB v4.3 + New Features

QSerialPort - OpenError [solved]

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 688 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.
  • D Offline
    D Offline
    don_kon
    wrote on last edited by don_kon
    #1

    Hello everyone,

    I'm trying to use the QSerialPort to send data to a FTDI and read the data that comes back. I'm fairly new in coding, so I tried to get all the information from the examples and other posts, but nothing really helped. So my problem is that I cannot open the port and the error code that I get is number 3 OpenError what means that the port is already open?? I made sure that no other program uses the port e.g. Terminal. But still I can't open it.

    meinlayout::meinlayout(QWidget *parent)
        : QMainWindow(parent)
    {
        serial = new QSerialPort(this);
        //info = new QSerialPortInfo();
    
        foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
        {
        qDebug() << "Available Ports: " << info.portName();
        }
    
        serial->setPortName("COM3");
    
        if(serial->portName()!= "COM3")
        {
            qDebug() << tr("error set name %1").arg(serial->error()) << serial->portName();
            return;
        }
        serial->setBaudRate(QSerialPort::Baud115200);
    
        if(serial->baudRate()!= 115200)
        {
            qDebug() << tr("error set Baud %1").arg(serial->error());
            return;
        }
    
        serial->setDataBits(QSerialPort::Data8);
    
        if(serial->dataBits()!= 8)
        {
            qDebug() << tr("error set bits %1").arg(serial->error());
            return;
        }
    
        serial->setParity(QSerialPort::NoParity);
    
        if(serial->parity()!= 0)
        {
            qDebug() << tr("error set parity %1").arg(serial->error());
            return;
        }
    
        serial->setFlowControl(QSerialPort::NoFlowControl);
    
        if(serial->flowControl()!= 0)
        {
            qDebug() << tr("error set flow %1").arg(serial->error());
            return;
        }
    
        serial->setStopBits(QSerialPort::OneStop);
    
        if(serial->stopBits()!= 1)
        {
            qDebug() << tr("error set stop %1").arg(serial->error());
            return;
        }
    
        if(serial->isOpen())
        {
            qDebug() << "Port already opened!";
            serial->close();
        }
    
    
       serial->open(QIODevice::ReadWrite);
    
    
        if(!serial->open(QIODevice::ReadWrite))
        {
            qDebug() << tr("error open Port %1").arg(serial->error());
            return;
        }
    
    }
    
    meinlayout::~meinlayout()
    {
        serial->close();
    }
    
    

    so the output I get is:

    Available Ports: "COM3"
    "error open Port 3"

    any suggestions what I'm doing wrong?

    jsulmJ 1 Reply Last reply
    0
    • D don_kon

      Hello everyone,

      I'm trying to use the QSerialPort to send data to a FTDI and read the data that comes back. I'm fairly new in coding, so I tried to get all the information from the examples and other posts, but nothing really helped. So my problem is that I cannot open the port and the error code that I get is number 3 OpenError what means that the port is already open?? I made sure that no other program uses the port e.g. Terminal. But still I can't open it.

      meinlayout::meinlayout(QWidget *parent)
          : QMainWindow(parent)
      {
          serial = new QSerialPort(this);
          //info = new QSerialPortInfo();
      
          foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
          {
          qDebug() << "Available Ports: " << info.portName();
          }
      
          serial->setPortName("COM3");
      
          if(serial->portName()!= "COM3")
          {
              qDebug() << tr("error set name %1").arg(serial->error()) << serial->portName();
              return;
          }
          serial->setBaudRate(QSerialPort::Baud115200);
      
          if(serial->baudRate()!= 115200)
          {
              qDebug() << tr("error set Baud %1").arg(serial->error());
              return;
          }
      
          serial->setDataBits(QSerialPort::Data8);
      
          if(serial->dataBits()!= 8)
          {
              qDebug() << tr("error set bits %1").arg(serial->error());
              return;
          }
      
          serial->setParity(QSerialPort::NoParity);
      
          if(serial->parity()!= 0)
          {
              qDebug() << tr("error set parity %1").arg(serial->error());
              return;
          }
      
          serial->setFlowControl(QSerialPort::NoFlowControl);
      
          if(serial->flowControl()!= 0)
          {
              qDebug() << tr("error set flow %1").arg(serial->error());
              return;
          }
      
          serial->setStopBits(QSerialPort::OneStop);
      
          if(serial->stopBits()!= 1)
          {
              qDebug() << tr("error set stop %1").arg(serial->error());
              return;
          }
      
          if(serial->isOpen())
          {
              qDebug() << "Port already opened!";
              serial->close();
          }
      
      
         serial->open(QIODevice::ReadWrite);
      
      
          if(!serial->open(QIODevice::ReadWrite))
          {
              qDebug() << tr("error open Port %1").arg(serial->error());
              return;
          }
      
      }
      
      meinlayout::~meinlayout()
      {
          serial->close();
      }
      
      

      so the output I get is:

      Available Ports: "COM3"
      "error open Port 3"

      any suggestions what I'm doing wrong?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @don_kon said in QSerialPort - OpenError:

      serial->open(QIODevice::ReadWrite);

      if(!serial->open(QIODevice::ReadWrite)
      

      Why do you open it twice?!

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • jsulmJ jsulm

        @don_kon said in QSerialPort - OpenError:

        serial->open(QIODevice::ReadWrite);

        if(!serial->open(QIODevice::ReadWrite)
        

        Why do you open it twice?!

        D Offline
        D Offline
        don_kon
        wrote on last edited by
        #3

        @jsulm Thank you for your help, after I annotated the first open() the program worked.
        I didn't know the

        if(!serial->open(QIODevice::ReadWrite))
        

        will try to open the port again.

        jsulmJ 1 Reply Last reply
        0
        • D don_kon

          @jsulm Thank you for your help, after I annotated the first open() the program worked.
          I didn't know the

          if(!serial->open(QIODevice::ReadWrite))
          

          will try to open the port again.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @don_kon said in QSerialPort - OpenError [solved]:

          will try to open the port again

          I don't think it will try to open it again, but if it is already open it will return false.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          0
          • jsulmJ jsulm

            @don_kon said in QSerialPort - OpenError [solved]:

            will try to open the port again

            I don't think it will try to open it again, but if it is already open it will return false.

            D Offline
            D Offline
            don_kon
            wrote on last edited by
            #5

            @jsulm Thanks!

            1 Reply Last reply
            0

            • Login

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