I have an Error in my terminal :device not open- but usually it works...C++
-
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
-
@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(); } -
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
@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
MainWindoweach time round the loop for each character?? It's totally the wrong concept, andMainWindow::m_serialwill not be open.Your
SendCommandsclass should not even be attempting to access anyMainWindowclass or object. Redesign your architecture. There are many ways to achieve this, perhaps passm_serialas a parameter toSendCommands::StartToSendCommand()so you can callwrite()on it. -
@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
MainWindoweach time round the loop for each character?? It's totally the wrong concept, andMainWindow::m_serialwill not be open.Your
SendCommandsclass should not even be attempting to access anyMainWindowclass or object. Redesign your architecture. There are many ways to achieve this, perhaps passm_serialas a parameter toSendCommands::StartToSendCommand()so you can callwrite()on it. -
@RuWex
Mostly understand why creating a newMainWindowcannot 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 ofMainWindowinto wherever suitable to be shared. -
@RuWex
Mostly understand why creating a newMainWindowcannot 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 ofMainWindowinto wherever suitable to be shared.@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; } -
@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.