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
-
@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 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
MainSendCommand
instance 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++:
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, andMainWindow::m_serial
will not be open.Your
SendCommands
class should not even be attempting to access anyMainWindow
class or object. Redesign your architecture. There are many ways to achieve this, perhaps passm_serial
as a parameter toSendCommands::StartToSendCommand()
so you can callwrite()
on it. -
@RuWex
Mostly understand why creating a newMainWindow
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 ofMainWindow
into 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; }
-
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 *serial
used 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:( -
@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 newMainWindow
cannot be the right thing to do.I assume the
mainwind= new MainWindow();
you show inMainSendCommand::MainSendCommand()
is not the onlyMainWindow
your application creates, and is not theMainWindow
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!) toSendCommands
orStartToSendCommand()
. You have no need to be trying to accessMainWindow
fromStartToSendCommand()
. -
@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 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
MainSendCommand
instance 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++:
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 theMainSendCommand
class do/add to your design? Maybe you don't need it andMainWindow
should just create aSendCommands
instance 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++:
sendCommand need function to mainwindow
Now that you know nothing should need or hold any reference to
MainWindow
maybe you can get rid of thisMainSendCommand
class and simplify.