According to this, I'm using the system() (QProcess) function from inside my program to call the gpio program.
It works. But I've noticed I need to run my app two times, in fact it only works at the second time. It seems the call to gpio must be done in another process, as pointed here.
Should this problem be approached with QProcess::setupChildProcess()?
I extended QProcess overwriting setupChildProcess and then just instanciated SandboxProcess in the constructor of my app. Unfortunately, this didn't worked.
class SandboxProcess : public QProcess
{
protected:
void setupChildProcess();
};
void SandboxProcess::setupChildProcess()
{
QString program = "/usr/local/bin/gpio";
QStringList arguments;
arguments << "export" << QString::number(4) << "out";
start(program, arguments);
}
I guess QProcess::setupChildProcess() doesn't help because it's own process starts after the main app proccess. So the main app still fells like the export command was not executed.
At this point I see two options:
To make a ManagerApp, which call gpio (to do the exports) and then call (another) MyApp which will actually access the exported devices.
Use the gpio app directly and listen to their stdout via signal/slot, using QProcess.
Related
I am using pthread in my program. For creation using pthread_create(). Right after creation I am using pthread_setname_np() to set the created thread's name.
I am observing that the name I set takes a small time to reflect, initially the thread inherits the program name.
Any suggestions how I can set the thread name at the time I create the thread using pthread_create()? I researched a bit in the available pthread_attr() but did not find a function that helps.
A quick way to reproduce what I am observing, is as follows:
void * thread_loop_func(void *arg) {
// some code goes here
pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
// Output to console the thread_name here
// some more code
}
int main() {
// some code
pthread_t test_thread;
pthread_create(&test_thread, &attr, thread_loop_func, &arg);
pthread_setname_np(test_thread, "THREAD-FOO");
// some more code, rest of pthread_join etc follows.
return 0;
}
Output:
<program_name>
<program_name>
THREAD-FOO
THREAD-FOO
....
I am looking for the first console output to reflect THREAD-FOO.
how I can set the thread name at the time I create the thread using pthread_create()?
That is not possible. Instead you can use a barrier or mutex to synchronize the child thread until it's ready to be run. Or you can set the thread name from inside the thread (if any other threads are not using it's name).
Do not to use pthread_setname_np. This is a nonstandard GNU extension. The _np suffix literally means "non-portable". Write portable code and instead use your own place where you store your thread names.
Instead of pthread_setname_np(3) you can use prctl(2) with PR_SET_NAME. The only limitation with this function is that you can only set the name of the calling process/thread. But since your example is doing exactly that, there should be no problem with this solution AND it's a portable standard API.
Have a simple Qt app. Gui thread, creates Dev thread it creates (in its run()) Read thread. Dev and Read threads are my classes inherited from QThread. The Read thread should read data from COM port continuously. An approximate view of Read run is following.
read::run()
{
sp2->clear();
while (DO_EXEC)
{
if (DO_WRITE)
{
// write data to port
}
usleep(500);
ba = sp2->bytesAvailable();
if (ba > 0)
{
int a = sp2->read(&BUF[BUF_END], ba);
// process data
emit sgnl(sendeddata);
}
}
}
To start it I emit signal in GUI that is passed to Dev at it is passed to the following read slot:
read::slot_readStart()
{
// some stuff
if (doStart && !isRunning())
{
sp2 = new QSerialPort(this);
sp2->setPortName("COM3");
sp2->setBaudRate(256000);
sp2->setDataBits(QSerialPort::Data8);
sp2->setStopBits(QSerialPort::OneStop);
sp2->setParity(QSerialPort::NoParity);
sp2->setFlowControl(QSerialPort::NoFlowControl);
sp2->setReadBufferSize(5000);
bool isOpen = sp2->open(QIODevice::ReadWrite);
DO_EXEC = true;
start();
}
}
This works. But, if I place creating and setup and opening serial port to run method, then the port is open, but the bytesAvailable() are always zero? Why it is possible?
Thank you in adcance.
I agree with Orest Hera, in that you are using a "non recommended" way of implementing threads.
You are using inheritance for your thread object.
It is important to understand how QThreads work. The general procedure to using the QThreads is:
Make Object to go into thread, assign no parent
Make thread
Move object into thead using obj->moveToThread(thread)
Connect a signal to a slot in the object that will instatiate the object members (if required)
Start the thread: thread->start()
For example:
MyObj *myObj = new MyObj(0); // 0 = no parent if your object inherits QObject
QThread* thread = new QThread;
myObj->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), myObj, SLOT(run()));
thread->start();
So your object can still have its "run()" function, but it won't be overloading anything.
Also your run() function does not need to be a "forever" loop, it is simply an initialization function (create the serial port or whatever). Then you add other slots for other events, e.g. you can connect the QSerialPort::readyRead() to your "incoming data slot" handler to handle any data received from the serial port.... and so on.
I think this will solve your issues. It is difficult to tell exactly why your serial port does not work in your overloaded "Run()" function because I can't see how you are calling /creating the thread (i.e. the rest of your code) or where DO_EXEC is initialized etc... There is probably some ordering of events or thread ownership issue here.
Note: I am not saying you can't inherit thread class, but if you do that it is so that you create your own custom thread class (to do thread stuff), but not some other class to create a hybrid of thread utility and other stuff. There is a fair amount of information relating to this here (on SO) and on the qt forum if you are interested in the why/how etc... :)
I need to develop a GUI program which will be run some external bash script. This script are working about 30-40 minutes and I want to see system output in my application in real time.
How can I provide this? Should I use QTextStream?
Please give me some examples.
If you launch the script via QProcess, you can get the output by connecting to the readyRead signal. Then it's just a matter of calling any of the read functions to get the data and then displaying it on any type of widget you want, such as a QTextEdit which has an append function for adding text.
Something like this: -
// Assuming QTextEdit textEdit has been created and this is in a class
// with a slot called updateText()
QProcess* proc = new QProcess;
connect(proc, SIGNAL(readyRead()), this, SLOT(updateText()));
proc->start("pathToScript");
...
// updateText in a class that stored a pointer to the QProcess, proc
void ClassName::updateText()
{
QString appendText(proc->readAll());
textEdit.append(appendText);
}
Now, every time the script produces text, your updateText function is called and you are adding it to the QTextEdit object.
I'm standing in front of a small (maybe not) problem. I have one function which parses XML file (very big xml ~1Gb) so it takes many time (5-6 mins to finish the func). I don't want to use it in GUI-thread because of known issues (mainwindow freezes and nothing happened, so user thinks everything goes wrong). I've tried to solve this problem by using
QtConcurrent::run
But one more problem appeared: if user press X (close button in top right corner) main GUI-thread goes down, but child-thread which was generated my QtConcurrent::run continue his work and I can kill him only by task manager.
I've decided to use QThread instead of QtConcurrent::run6 but I don't understand how can I run MainWindow class function:
void MainWindow::parseXML()
I've tried to create smth like this:
class pThread : public QThread
{
Q_OBJECT
private:
void run();
};
void pThread::run(){
MainWindow::parseXML();
}
But when I'm trying to compile it error appears:
cannot call member function 'void MainWindow::parseXML()' without object
Moreover, I don't know if it possible to update GUI-thread through this method (parseXML function changes statusBar)
What should I do?
The recommended ways to work with threads in Qt is not to inherit from QThread class, see the documentation here and you should be able to do it after that.
And yes it is possible to update the mainwindow from the thread, just code the signals and slots for that functionality, into mainwindow class code a slot that updates the progress and into the class that does the work (the xml parsing you need - there is no reason that functionality should be into the mainwindow class anyway) you code the signal that emit the progress and connect it with mainwindow's slot with Qt::QueuedConnection (note that the default auto-connection will become queued if the objects are in separate threads).
Another option is to use start a QRunnable with QThreadPool. you may want to check documentation. Be ware to wait the spawned threads with QThreadPool::waitForDone().
I am writing a simple char device driver. The function which we pass to module_init() is called at the time of module installation.
When we insert the module using insmod command the function passes to module_init() is gets called.
Is there any other method to call this module_init() function.
If you are talking about using something else than insmod, then no: insmod is the only way I know to initialize your module.
Otherwise, this module_init thing is a macro and isn't really a function call (you cannot call a function from global scope in C). It expands to some predefined "module constructor" that calls your initializing function, depending on if you're compiling as a dynamic module or as an object built into the kernel. Its role is to avoid having to #ifdef a lot when developing a module and making the development process easier (see this).
So if, for some reason (but I discourage you doing this), you want to call your initializing function from your module code, then just call it directly. For example:
static void some_other_function(void) {
// ...
initialize();
// ...
}
static int initialize(void) {
// your initialization code
}
module_init(initialize);
Edit: removed __init following Eugene's comment.
However, I recommend only the module_init expansion calls your initialization function and that other common code be in a separate function:
static void some_other_function(void) {
// ...
something_that_might_get_called_afterwards_also();
// ...
}
static int __init initialize(void) {
// your initialization code (done only once)
something_that_might_get_called_afterwards_also();
// some other one-time code
}
module_init(initialize);