gpt4 book ai didi

linux - Qt 5 使用 QFile 和 QDataStream 编写 Linux SysFs 条目

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:55 26 4
gpt4 key购买 nike

我可以 echo 100 >/sys/class/soft_pwm/pwm-35/duty_cycle 以 root 身份从命令行运行,但我无法让我的 Qt 5 代码在运行时执行相同的操作根。下面是代码——我做错了什么?谢谢。

/**
* Set the PWM on the Red / Green LEDs
* @param percent
*/
void LedController::setLedPwm(int percent) {

// Cap the percent
if ( percent > 90 ) {
percent = 90;
}
if ( percent < 25 ) {
percent = 25;
}

// Scale to usable percent
percent = abs(percent - 100);

QFile file1("/sys/class/soft_pwm/pwm-35/duty_cycle");
if (!file1.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "LedController: Cannot set LED PWM for Red/Green LEDs [ 35 ]...";
return;
}
QDataStream in1(&file1);
QString toWrite1 = QString("%1\n").arg(percent);
qDebug() << "LedController: PWM Led [ 35 ]" << toWrite1;
in1 << toWrite1;
file1.close();

QFile file2("/sys/class/soft_pwm/pwm-39/duty_cycle");
if (!file2.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "LedController: Cannot set LED PWM for Red/Green LEDs [ 39 ]...";
return;
}
QDataStream in2(&file2);
QString toWrite2 = QString("%1\n").arg(percent);
qDebug() << "LedController: PWM Led [ 35 ]" << toWrite2;
in2 << toWrite1;
file2.close();

}

最佳答案

QDataStream 用于二进制数据存储。要写入文本值,请使用 QTextStream。

关于linux - Qt 5 使用 QFile 和 QDataStream 编写 Linux SysFs 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34779975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com