I have an Error in my terminal :device not open- but usually it works...C++
-
@JonB
The truth is that I already did the idea you brought up and it caused the same problem:
sendCommandvoid 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; }QSerialPort *serial=mainSend->mainwind->SendCommand();This does not correspond to any "idea you [I] brought up".
I do not know how you initialisedmainSend->mainwind.
Since the error says "QIODevice::write (QSerialPort): device not open" presumably that tells you that whatever you are doing theQSerialPort *serialused inserial->write()is not open. -
QSerialPort *serial=mainSend->mainwind->SendCommand();This does not correspond to any "idea you [I] brought up".
I do not know how you initialisedmainSend->mainwind.
Since the error says "QIODevice::write (QSerialPort): device not open" presumably that tells you that whatever you are doing theQSerialPort *serialused inserial->write()is not open.@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:( -
@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:(@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 newMainWindowcannot be the right thing to do.I assume the
mainwind= new MainWindow();you show inMainSendCommand::MainSendCommand()is not the onlyMainWindowyour application creates, and is not theMainWindowwidget 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!) toSendCommandsorStartToSendCommand(). You have no need to be trying to accessMainWindowfromStartToSendCommand(). -
@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 newMainWindowcannot be the right thing to do.I assume the
mainwind= new MainWindow();you show inMainSendCommand::MainSendCommand()is not the onlyMainWindowyour application creates, and is not theMainWindowwidget 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!) toSendCommandsorStartToSendCommand(). You have no need to be trying to accessMainWindowfromStartToSendCommand(). -
@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?@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
MainWindowinstance you have created toMainSendCommand(or any other class). A good rule of thumb is do not allow yourself to go#include "mainwindow.h"in any file other thanmainwindow.cpp, then you can never access it from elsewhere.Assuming you create the
MainSendCommandinstance from yourMainWindow(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(); } -
@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
MainWindowinstance you have created toMainSendCommand(or any other class). A good rule of thumb is do not allow yourself to go#include "mainwindow.h"in any file other thanmainwindow.cpp, then you can never access it from elsewhere.Assuming you create the
MainSendCommandinstance from yourMainWindow(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(); } -
@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!!!@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 theMainSendCommandclass do/add to your design? Maybe you don't need it andMainWindowshould just create aSendCommandsinstance directly? This now all comes down to your overall program architecture. -
@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 theMainSendCommandclass do/add to your design? Maybe you don't need it andMainWindowshould just create aSendCommandsinstance directly? This now all comes down to your overall program architecture. -
@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@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
MainWindowmaybe you can get rid of thisMainSendCommandclass and simplify. -
@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
MainWindowmaybe you can get rid of thisMainSendCommandclass and simplify.