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++
QtWS25 Last Chance

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.
  • J JonB
    9 Nov 2022, 08:30

    @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 9 Nov 2022, 08:38 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;
    }
    
    J 1 Reply Last reply 9 Nov 2022, 08:45
    0
    • R RuWex
      9 Nov 2022, 08:38

      @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;
      }
      
      J Offline
      J Offline
      JonB
      wrote on 9 Nov 2022, 08:45 last edited by JonB 11 Sept 2022, 08:45
      #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 9 Nov 2022, 08:49
      2
      • J JonB
        9 Nov 2022, 08:45

        @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 9 Nov 2022, 08:49 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:(

        J 1 Reply Last reply 9 Nov 2022, 08:54
        0
        • R RuWex
          9 Nov 2022, 08:49

          @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:(

          J Offline
          J Offline
          JonB
          wrote on 9 Nov 2022, 08:54 last edited by JonB 11 Sept 2022, 08:56
          #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 9 Nov 2022, 09:10
          2
          • J JonB
            9 Nov 2022, 08:54

            @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 9 Nov 2022, 09:10 last edited by RuWex 11 Sept 2022, 09:10
            #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?

            J 1 Reply Last reply 9 Nov 2022, 09:21
            0
            • R RuWex
              9 Nov 2022, 09:10

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

              J Offline
              J Offline
              JonB
              wrote on 9 Nov 2022, 09:21 last edited by JonB 11 Sept 2022, 09:24
              #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 9 Nov 2022, 09:27
              2
              • J JonB
                9 Nov 2022, 09:21

                @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 9 Nov 2022, 09:27 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!!!

                J 1 Reply Last reply 9 Nov 2022, 09:35
                0
                • R RuWex
                  9 Nov 2022, 09:27

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 9 Nov 2022, 09:35 last edited by JonB 11 Sept 2022, 09:35
                  #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 9 Nov 2022, 09:37
                  0
                  • J JonB
                    9 Nov 2022, 09:35

                    @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 9 Nov 2022, 09:37 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

                    J 1 Reply Last reply 9 Nov 2022, 09:39
                    0
                    • R RuWex
                      9 Nov 2022, 09:37

                      @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

                      J Offline
                      J Offline
                      JonB
                      wrote on 9 Nov 2022, 09:39 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 9 Nov 2022, 09:40
                      1
                      • J JonB
                        9 Nov 2022, 09:39

                        @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 9 Nov 2022, 09:40 last edited by RuWex 11 Sept 2022, 09:41
                        #15

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

                        R 1 Reply Last reply 9 Nov 2022, 10:58
                        0
                        • R RuWex
                          9 Nov 2022, 09:40

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

                          R Offline
                          R Offline
                          RuWex
                          wrote on 9 Nov 2022, 10:58 last edited by
                          #16

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

                          1 Reply Last reply
                          0

                          14/16

                          9 Nov 2022, 09:39

                          • Login

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