My gpio does not change with my code but changes occured with this code.



  • Hi.
    I have a matrix relay with a nanopi fire (like rasperry pi). I connected this relay to my device and want to change gpio value.(my device has multi gpio pin. first I should create gpio folder with export command and then change direction folder "out" and with changing value the relay should turn on and off.)
    this is my code:

    bool GPIOClass::ChangeValue(QString IO, QString port)
    {
        QString gpio;
        QString comand;
    //    comand=" echo ";
    //    comand.append(QIO);
    //    comand.append(" > ");
    //    gpio=config->GpioFromPort(port);
    
    //    if(gpio.length()==0)
    //        return false;
    //    //IO should be 1 or 0
    
        QString src=IdenGPIOSrc(gpio);//src is /sys/class/gpio/gpio95
        src.append("/value");
    
    //    comand.append(src);
    //    qDebug()<<port<<gpio<<"with src:"
    //           <<src <<"with command:"<<comand;
    //    system(comand.toStdString().c_str());
    
        ///===========================================================
        QFile file(src);
        if ( file.open(QIODevice::WriteOnly) )
        {
            //clear the content
    //        file.resize(0);
            file.seek(0);
            file.write(QByteArray::number(IO.toInt()),1);
    
        }
    
        if(GetValue(port)==IO.toInt())
        {
            return true;
        }
        else
            return false;
    }
    

    the relay is not changed, but when I run the executable sample the relay is turn on and off:

    int main(int argc, char ** argv) 
    {
        int pin = GPIO_PIN(7); 
        int i, value, board;
        int ret = -1;
     
        if ((board = boardInit()) < 0) {
            printf("Fail to init board\n");
            return -1;
        }
        if (board == BOARD_NANOPI_T2)
            pin = GPIO_PIN(15);
     
        if (argc == 2)
            pin = GPIO_PIN(atoi(argv[1]));
        if ((ret = exportGPIOPin(pin)) == -1) {   
            printf("exportGPIOPin(%d) failed\n", pin);
        }
        if ((ret = setGPIODirection(pin, GPIO_OUT)) == -1) {
            printf("setGPIODirection(%d) failed\n", pin);
        }
        for (i = 0; i < STATUS_CHANGE_TIMES; i++) {
            if (i % 2) {
                value = GPIO_HIGH;
            } else {
                value = GPIO_LOW;
            }
            if ((ret = setGPIOValue(pin, value)) > 0) {
                printf("%d: GPIO_PIN(%d) value is %d\n", i+1, pin, value);
            } else {
                printf("setGPIOValue(%d) failed\n", pin);
            }
            sleep(1);
        }
        unexportGPIOPin(pin);
        return 0;
    }
    

    in that code also the value in that address changed like my code (i test it ) and the relay is working.
    where is my code problem?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.