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. I have an Error in my terminal :device not open- but usually it works...C++

I have an Error in my terminal :device not open- but usually it works...C++

Scheduled Pinned Locked Moved Solved General and Desktop
terminalwritec++
16 Posts 2 Posters 1.1k 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.
  • R Offline
    R Offline
    RuWex
    wrote on last edited by
    #1

    Hi:)
    I have an intersting problem
    Im doing a terminal application,
    and the terminal work well.
    my problem is when Im reading from file and send the letters to the terminal,
    then there is that error(in the debug, the program doesnt failed):
    QIODevice::write (QSerialPort): device not open
    my code:
    mainWindow.cpp:

    void MainWindow::writeData(const QByteArray &data)
    {
        m_serial->write(data);
    }
    

    sendCommand.cpp:

    void SendCommands::StartToSendCommand( QString fileName)/
    {
            QFile file(fileName);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                return;
    
    
            QTextStream in(&file);
            QString line;
            while (!in.atEnd()) {
    
                line = in.readLine();
    
                for(int i=0; i< line.length(); ++i)
                {
    
                    QString letter= (QString)line[i];
                    MainWindow *mainWind= new MainWindow();
                    mainWind->WriteData(letter.toLatin1());
    
                }
                QApplication::processEvents();
    
    
            }
    
    
    }
    

    but as I said the function usually works except when I run it on the StartToSendCommand function

    JonBJ 1 Reply Last reply
    0
    • R RuWex

      @JonB
      thank, you right I understand my mistake..:(
      Im new to C++ so I dont still Master the material....
      Do you have any Idea how to get the instance that already open?

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

      @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

      Do you have any Idea how to get the instance that already open?

      Like I said, do not try to pass or access the MainWindow instance you have created to MainSendCommand (or any other class). A good rule of thumb is do not allow yourself to go #include "mainwindow.h" in any file other than mainwindow.cpp, then you can never access it from elsewhere.

      Assuming you create the MainSendCommand instance from your MainWindow (right?). So pass the serial port instance instead. Like maybe:

      // Somewhere in `MainWindow`
      mainSendCommand = new MainSendCommand(m_serial);
      
      MainSendCommand::MainSendCommand(QSerialPort *serial)
      {
          sendCommand= new SendCommands(serial);
      }
      
      SendCommands::SendCommands(QSerialPort *serial)
      {
          this->m_serial = serial;
      }
      
      void SendCommands::StartToSendCommand( QString fileName)
      {
          m_serial->write();
      }
      
      R 1 Reply Last reply
      2
      • R RuWex

        Hi:)
        I have an intersting problem
        Im doing a terminal application,
        and the terminal work well.
        my problem is when Im reading from file and send the letters to the terminal,
        then there is that error(in the debug, the program doesnt failed):
        QIODevice::write (QSerialPort): device not open
        my code:
        mainWindow.cpp:

        void MainWindow::writeData(const QByteArray &data)
        {
            m_serial->write(data);
        }
        

        sendCommand.cpp:

        void SendCommands::StartToSendCommand( QString fileName)/
        {
                QFile file(fileName);
                if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                    return;
        
        
                QTextStream in(&file);
                QString line;
                while (!in.atEnd()) {
        
                    line = in.readLine();
        
                    for(int i=0; i< line.length(); ++i)
                    {
        
                        QString letter= (QString)line[i];
                        MainWindow *mainWind= new MainWindow();
                        mainWind->WriteData(letter.toLatin1());
        
                    }
                    QApplication::processEvents();
        
        
                }
        
        
        }
        

        but as I said the function usually works except when I run it on the StartToSendCommand function

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

        @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

        MainWindow *mainWind= new MainWindow();

        What do you think is going to happen if you create a brand new MainWindow each time round the loop for each character?? It's totally the wrong concept, and MainWindow::m_serial will not be open.

        Your SendCommands class should not even be attempting to access any MainWindow class or object. Redesign your architecture. There are many ways to achieve this, perhaps pass m_serial as a parameter to SendCommands::StartToSendCommand() so you can call write() on it.

        R 1 Reply Last reply
        3
        • JonBJ JonB

          @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

          MainWindow *mainWind= new MainWindow();

          What do you think is going to happen if you create a brand new MainWindow each time round the loop for each character?? It's totally the wrong concept, and MainWindow::m_serial will not be open.

          Your SendCommands class should not even be attempting to access any MainWindow class or object. Redesign your architecture. There are many ways to achieve this, perhaps pass m_serial as a parameter to SendCommands::StartToSendCommand() so you can call write() on it.

          R Offline
          R Offline
          RuWex
          wrote on last edited by
          #3

          @JonB
          so what should I do?

          JonBJ 1 Reply Last reply
          0
          • R RuWex

            @JonB
            so what should I do?

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

            @RuWex
            Mostly understand why creating a new MainWindow cannot be the right thing to do.

            I have given you one simple suggested approach. Other ways might include (a) sending a signal from StartToSendCommand() with the character(s) to forward to the serial port or (b) move the serial port handling out of MainWindow into wherever suitable to be shared.

            R 1 Reply Last reply
            3
            • JonBJ JonB

              @RuWex
              Mostly understand why creating a new MainWindow cannot be the right thing to do.

              I have given you one simple suggested approach. Other ways might include (a) sending a signal from StartToSendCommand() with the character(s) to forward to the serial port or (b) move the serial port handling out of MainWindow into wherever suitable to be shared.

              R Offline
              R Offline
              RuWex
              wrote on last edited by
              #5

              @JonB
              The truth is that I already did the idea you brought up and it caused the same problem:
              sendCommand

              void SendCommands::StartToSendCommand( QString fileName)//Ruth
              {
              
                      QFile file(fileName);
                      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                          return;
              
                      QTextStream in(&file);
                      QString line;
                      while (!in.atEnd()) {
                          line = in.readLine();
                          for(int i=0; i< line.length(); ++i)
                          {
              
                              QString letter= (QString)line[i];
              
                              QSerialPort *serial=mainSend->mainwind->SendCommand();
                              serial->write(letter.toLatin1());
                          }
                          QApplication::processEvents();
              
              
                      }
              
                  }
              
              
              

              MainWindow:

              QSerialPort* MainWindow:: SendCommand()
              {
                  return m_serial;
              }
              
              JonBJ 1 Reply Last reply
              0
              • R RuWex

                @JonB
                The truth is that I already did the idea you brought up and it caused the same problem:
                sendCommand

                void SendCommands::StartToSendCommand( QString fileName)//Ruth
                {
                
                        QFile file(fileName);
                        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                            return;
                
                        QTextStream in(&file);
                        QString line;
                        while (!in.atEnd()) {
                            line = in.readLine();
                            for(int i=0; i< line.length(); ++i)
                            {
                
                                QString letter= (QString)line[i];
                
                                QSerialPort *serial=mainSend->mainwind->SendCommand();
                                serial->write(letter.toLatin1());
                            }
                            QApplication::processEvents();
                
                
                        }
                
                    }
                
                
                

                MainWindow:

                QSerialPort* MainWindow:: SendCommand()
                {
                    return m_serial;
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @RuWex

                QSerialPort *serial=mainSend->mainwind->SendCommand();

                This does not correspond to any "idea you [I] brought up".
                I do not know how you initialised mainSend->mainwind.
                Since the error says "QIODevice::write (QSerialPort): device not open" presumably that tells you that whatever you are doing the QSerialPort *serial used in serial->write() is not open.

                R 1 Reply Last reply
                2
                • JonBJ JonB

                  @RuWex

                  QSerialPort *serial=mainSend->mainwind->SendCommand();

                  This does not correspond to any "idea you [I] brought up".
                  I do not know how you initialised mainSend->mainwind.
                  Since the error says "QIODevice::write (QSerialPort): device not open" presumably that tells you that whatever you are doing the QSerialPort *serial used in serial->write() is not open.

                  R Offline
                  R Offline
                  RuWex
                  wrote on last edited by
                  #7

                  @JonB said in I have an Error in my terminal :device not open- but usually it works...C++:

                  This does not correspond to any "idea you [I] brought up".
                  I do not know how you initialised mainSend->mainwind.
                  Since the error says "QIODevice::write (QSerialPort): device not open" presumably that tells you that whatever you are doing the QSerialPort *serial used in serial->write() is not open.

                  I forgot to upload another piece of code:

                  MainSendCommand:

                  MainSendCommand::MainSendCommand()
                  {
                      sendCommand= new SendCommands();
                      mainwind= new MainWindow();
                  
                  
                  }
                  

                  I did it because I didnt want to do using mainWindow in Send Command file...
                  so what can be the problem?
                  I dont understand:(

                  JonBJ 1 Reply Last reply
                  0
                  • R RuWex

                    @JonB said in I have an Error in my terminal :device not open- but usually it works...C++:

                    This does not correspond to any "idea you [I] brought up".
                    I do not know how you initialised mainSend->mainwind.
                    Since the error says "QIODevice::write (QSerialPort): device not open" presumably that tells you that whatever you are doing the QSerialPort *serial used in serial->write() is not open.

                    I forgot to upload another piece of code:

                    MainSendCommand:

                    MainSendCommand::MainSendCommand()
                    {
                        sendCommand= new SendCommands();
                        mainwind= new MainWindow();
                    
                    
                    }
                    

                    I did it because I didnt want to do using mainWindow in Send Command file...
                    so what can be the problem?
                    I dont understand:(

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

                    @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                    so what can be the problem?
                    I dont understand:(

                    @JonB said in I have an Error in my terminal :device not open- but usually it works...C++:

                    @RuWex
                    Mostly understand why creating a new MainWindow cannot be the right thing to do.

                    I assume the mainwind= new MainWindow(); you show in MainSendCommand::MainSendCommand() is not the only MainWindow your application creates, and is not the MainWindow widget your application is actually showing. Until you understand the basics of C++ and what separate instances are you won't get very far.

                    I already suggested one of simpler alternatives is to pass m_serial (once opened!) to SendCommands or StartToSendCommand(). You have no need to be trying to access MainWindow from StartToSendCommand().

                    R 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                      so what can be the problem?
                      I dont understand:(

                      @JonB said in I have an Error in my terminal :device not open- but usually it works...C++:

                      @RuWex
                      Mostly understand why creating a new MainWindow cannot be the right thing to do.

                      I assume the mainwind= new MainWindow(); you show in MainSendCommand::MainSendCommand() is not the only MainWindow your application creates, and is not the MainWindow widget your application is actually showing. Until you understand the basics of C++ and what separate instances are you won't get very far.

                      I already suggested one of simpler alternatives is to pass m_serial (once opened!) to SendCommands or StartToSendCommand(). You have no need to be trying to access MainWindow from StartToSendCommand().

                      R Offline
                      R Offline
                      RuWex
                      wrote on last edited by RuWex
                      #9

                      @JonB
                      thank, you right I understand my mistake..:(
                      Im new to C++ so I dont still Master the material....
                      Do you have any Idea how to get the instance that already open?

                      JonBJ 1 Reply Last reply
                      0
                      • R RuWex

                        @JonB
                        thank, you right I understand my mistake..:(
                        Im new to C++ so I dont still Master the material....
                        Do you have any Idea how to get the instance that already open?

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

                        @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                        Do you have any Idea how to get the instance that already open?

                        Like I said, do not try to pass or access the MainWindow instance you have created to MainSendCommand (or any other class). A good rule of thumb is do not allow yourself to go #include "mainwindow.h" in any file other than mainwindow.cpp, then you can never access it from elsewhere.

                        Assuming you create the MainSendCommand instance from your MainWindow (right?). So pass the serial port instance instead. Like maybe:

                        // Somewhere in `MainWindow`
                        mainSendCommand = new MainSendCommand(m_serial);
                        
                        MainSendCommand::MainSendCommand(QSerialPort *serial)
                        {
                            sendCommand= new SendCommands(serial);
                        }
                        
                        SendCommands::SendCommands(QSerialPort *serial)
                        {
                            this->m_serial = serial;
                        }
                        
                        void SendCommands::StartToSendCommand( QString fileName)
                        {
                            m_serial->write();
                        }
                        
                        R 1 Reply Last reply
                        2
                        • JonBJ JonB

                          @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                          Do you have any Idea how to get the instance that already open?

                          Like I said, do not try to pass or access the MainWindow instance you have created to MainSendCommand (or any other class). A good rule of thumb is do not allow yourself to go #include "mainwindow.h" in any file other than mainwindow.cpp, then you can never access it from elsewhere.

                          Assuming you create the MainSendCommand instance from your MainWindow (right?). So pass the serial port instance instead. Like maybe:

                          // Somewhere in `MainWindow`
                          mainSendCommand = new MainSendCommand(m_serial);
                          
                          MainSendCommand::MainSendCommand(QSerialPort *serial)
                          {
                              sendCommand= new SendCommands(serial);
                          }
                          
                          SendCommands::SendCommands(QSerialPort *serial)
                          {
                              this->m_serial = serial;
                          }
                          
                          void SendCommands::StartToSendCommand( QString fileName)
                          {
                              m_serial->write();
                          }
                          
                          R Offline
                          R Offline
                          RuWex
                          wrote on last edited by
                          #11

                          @JonB
                          Wow I really appreciate it :)
                          But I didn't make an appearance of the class in mainSenfCommand, maybe I'll try to think of another idea in the style you suggested.
                          I have no words really thank you!!!

                          JonBJ 1 Reply Last reply
                          0
                          • R RuWex

                            @JonB
                            Wow I really appreciate it :)
                            But I didn't make an appearance of the class in mainSenfCommand, maybe I'll try to think of another idea in the style you suggested.
                            I have no words really thank you!!!

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

                            @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                            But I didn't make an appearance of the class in mainSenfCommand

                            I take that to mean "code does not create any instance of MainSendCommand"? Or do you create it somewhere? Else what is the point of having it? What does the MainSendCommand class do/add to your design? Maybe you don't need it and MainWindow should just create a SendCommands instance directly? This now all comes down to your overall program architecture.

                            R 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                              But I didn't make an appearance of the class in mainSenfCommand

                              I take that to mean "code does not create any instance of MainSendCommand"? Or do you create it somewhere? Else what is the point of having it? What does the MainSendCommand class do/add to your design? Maybe you don't need it and MainWindow should just create a SendCommands instance directly? This now all comes down to your overall program architecture.

                              R Offline
                              R Offline
                              RuWex
                              wrote on last edited by
                              #13

                              @JonB
                              I did it because I had a circular problem
                              sendCommand need function to mainwindow and mainwindow need function from send command
                              so I did more class that would solve the problem

                              JonBJ 1 Reply Last reply
                              0
                              • R RuWex

                                @JonB
                                I did it because I had a circular problem
                                sendCommand need function to mainwindow and mainwindow need function from send command
                                so I did more class that would solve the problem

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

                                @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                                sendCommand need function to mainwindow

                                Now that you know nothing should need or hold any reference to MainWindow maybe you can get rid of this MainSendCommand class and simplify.

                                R 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @RuWex said in I have an Error in my terminal :device not open- but usually it works...C++:

                                  sendCommand need function to mainwindow

                                  Now that you know nothing should need or hold any reference to MainWindow maybe you can get rid of this MainSendCommand class and simplify.

                                  R Offline
                                  R Offline
                                  RuWex
                                  wrote on last edited by RuWex
                                  #15

                                  @JonB
                                  yes, I think you right I have just now wanted to write it to you:)
                                  thank!!!

                                  R 1 Reply Last reply
                                  0
                                  • R RuWex

                                    @JonB
                                    yes, I think you right I have just now wanted to write it to you:)
                                    thank!!!

                                    R Offline
                                    R Offline
                                    RuWex
                                    wrote on last edited by
                                    #16

                                    @JonB
                                    I changed it as you said and it works well!!!

                                    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