How we can run Linux command from qt code?
-
@jsulm thanks;
I passed the RX_LO_frequency to the device via bashscript using QProcess when I connect the device with a signal generator from the external at this time my device sense according to the value I passed in RX_LO_frequency. Now I want to pass the same value on TX_LO_frequency using QProcess this is the main reason I am using QProcess.
thanks -
@Wasee said in How we can run Linux command from qt code?:
kindly give me any suitable example QFile device read
What example?
These device files are files, so you read/write them like any other files... -
@Wasee
To answer your original question: the code you showed in your first example should do exactly what you say you want it to do --- provided you have first typed that command into abash
shell to verify it works correctly. So how do you know "it is not executing my command"? There is no need to run itstartDetached()
instead of plainstart()
. You could then capture any error and anything written to stdout/stderr ot check for anything unexpected. [Note: your literal string has\:
in it. That is not right for C++. Either\\:
or just simple:
. Maybe that is why it had problems.]However, as @jsulm said this command simple writes a string to a file. So no need for a
QProcess
, what is wrong with:QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency"); if (f.open(QIODevice::WriteOnly)) { qint64 count = f.write("24250000"); Q_ASSERT(count > 0); f.close(); }
-
@Wasee
To answer your original question: the code you showed in your first example should do exactly what you say you want it to do --- provided you have first typed that command into abash
shell to verify it works correctly. So how do you know "it is not executing my command"? There is no need to run itstartDetached()
instead of plainstart()
. You could then capture any error and anything written to stdout/stderr ot check for anything unexpected. [Note: your literal string has\:
in it. That is not right for C++. Either\\:
or just simple:
. Maybe that is why it had problems.]However, as @jsulm said this command simple writes a string to a file. So no need for a
QProcess
, what is wrong with:QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency"); if (f.open(QIODevice::WriteOnly)) { qint64 count = f.write("24250000"); Q_ASSERT(count > 0); f.close(); }
-
@JonB Hi;
I coded as you suggested above but still not changing TX_LO_frequency value. why?
thanks -
@JonB Hi;
I coded as you suggested above but still not changing TX_LO_frequency value. why?
thanksThis post is deleted! -
@JonB Hi;
I coded as you suggested above but still not changing TX_LO_frequency value. why?
thanks@Wasee said in How we can run Linux command from qt code?:
but still not changing TX_LO_frequency value. why?
Did you even at least check the return result of the
open()
for yourself? For all I know you do not have permission to write to that file. This is part of what doing development involves....Also I would not write a string but a number:
That's fine, but it's not what the OP's
echo 24250000 > out_altvoltage1_TX_LO_frequency
would do (treat anything as a number), which I was trying to copy. In fact I think Linuxecho
will append a newline, so the OP may need to do that too. -
@JonB said in How we can run Linux command from qt code?:
echo 24250000 > out_altvoltage1_TX_LO_frequency this command run in terminal and change the value on device. But not changing from qt code.@Wasee said in How we can run Linux command from qt code?:
change the value on device
Without sudo?
Please add error handling to your code: print error message if f.open(...) fails.
-
@Wasee said in How we can run Linux command from qt code?:
change the value on device
Without sudo?
Please add error handling to your code: print error message if f.open(...) fails.
-
@jsulm Hi;
Yes without sudo in terminal its changing value. No error of device open its not returning.
thanks -
@Wasee
To answer your original question: the code you showed in your first example should do exactly what you say you want it to do --- provided you have first typed that command into abash
shell to verify it works correctly. So how do you know "it is not executing my command"? There is no need to run itstartDetached()
instead of plainstart()
. You could then capture any error and anything written to stdout/stderr ot check for anything unexpected. [Note: your literal string has\:
in it. That is not right for C++. Either\\:
or just simple:
. Maybe that is why it had problems.]However, as @jsulm said this command simple writes a string to a file. So no need for a
QProcess
, what is wrong with:QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency"); if (f.open(QIODevice::WriteOnly)) { qint64 count = f.write("24250000"); Q_ASSERT(count > 0); f.close(); }
@JonB said in How we can run Linux command from qt code?:
QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
if (f.open(QIODevice::WriteOnly))
{
qint64 count = f.write("24250000");
Q_ASSERT(count > 0);
f.close();
}how I can pass variable instead of 24250000.
-
@JonB Thanks for your appreciated help!
Your suggestion of QFile is working fine now.QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency"); if (f.open(QIODevice::WriteOnly)) { qint64 count = f.write("2425000000"); Q_ASSERT(count > 0); f.close(); }
The value 2425000000 is coming from a variable which can be change any time. I need to pass this value as a variable instead of constant 2425000000.
thanks -
@JonB Thanks for your appreciated help!
Your suggestion of QFile is working fine now.QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency"); if (f.open(QIODevice::WriteOnly)) { qint64 count = f.write("2425000000"); Q_ASSERT(count > 0); f.close(); }
The value 2425000000 is coming from a variable which can be change any time. I need to pass this value as a variable instead of constant 2425000000.
thanks@Wasee said in How we can run Linux command from qt code?:
The value 2425000000 is coming from a variable which can be change any time. I need to pass this value as a variable instead of constant 2425000000.
thanksAre you joking???
Do you really don't have any idea how to do this simple stuff?
It is so complicated to create a string and write it to a file?
What did you try to do? -
@JonB Thanks for your appreciated help!
Your suggestion of QFile is working fine now.QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency"); if (f.open(QIODevice::WriteOnly)) { qint64 count = f.write("2425000000"); Q_ASSERT(count > 0); f.close(); }
The value 2425000000 is coming from a variable which can be change any time. I need to pass this value as a variable instead of constant 2425000000.
thanks@Wasee
You must convert your (integer) variable to a string.QIODevice::write()
accepts either aconst char *
or aconst QByteArray &
for the string to write. You can use eitherQString::number()
orQByteArray::number()
to convert a number to a string/byte array to pass towrite()
, as you please.