none
DeviceIoControl failed in Windows 7 RRS feed

  • Question

  • Hi,

    I have write one application to open an interface called i2c in my embedded pc via MSDN SetupDiXXX functions in windows 7. By using SetupDiXXX function i am getting the interface PATH availabe in REGEDIT in windows 7 by using SetupDiGetDeviceInterfaceDetail function in pInterfaceDetailData->DevicePath. When i open this path using CreateFile api, it is successfully open it. To communicate with device i am using DeviceIoControl but every time it returns 0 value meaning DeviceIoControl is failed. Below is my code,

    i2c_Handle = CreateFile(pInterfaceDetailData->DevicePath,
                                        DWORD(S_GENERIC_READ | S_GENERIC_WRITE),
                                        DWORD(FILE_SHARE_READ | FILE_SHARE_WRITE),
                                        LPSECURITY_ATTRIBUTES(NULL),
                                        DWORD(S_OPEN_EXISTING),
                                        DWORD(FILE_FLAG_OVERLAPPED),
                                        (HANDLE)NULL);

    if(i2c_Handle == INVALID_HANDLE_VALUE)
                {
                    std::cout << " Port Open Error : " << GetLastError() <<endl;
                }
                else
                {
                    std :: cout << "Creatfile is successfull..... = "<< i2c_Handle << endl; // i2c_Handle value is 000000C4
                }

        BOOL status;
        I2C_SINGLE_TRANSMISSION writeTransmission;
        UCHAR writeBuf[2] = {0x48,0x00};
        UCHAR readBuf[BUF_SIZE] = {};
        UINT16 slaveAdr = 0x48;
        DWORD bytesReturned;

        writeTransmission.Address = slaveAdr;
        writeTransmission.AddressMode = AddressMode7Bit;
        writeTransmission.BusSpeed = I2C_BUS_SPEED_400KHZ;
        writeTransmission.DataLength = sizeof(writeBuf);
        writeTransmission.pBuffer = writeBuf;

        status = DeviceIoControl(i2c_Handle,
                                 (DWORD)IOCTL_I2C_EXECUTE_WRITE,
                                 &writeTransmission,
                                 sizeof(writeTransmission),
                                 NULL,
                                 0,
                                 NULL,
                                 (LPOVERLAPPED)&Overlapped);

        std::cout << "Status : "<< status << endl << "Get-Last-Error-Code : "<< GetLastError() << endl; // Here the return value of DeviceIoControl is 0 as Status = 0 and Get-Last-Error-Code : 997 means the operation is still pending and not completed.

    but after calling the GetOverlappedResult, it also returns failed.

         if(status || (GetLastError() == ERROR_IO_PENDING))
        {        
            status = GetOverlappedResult(i2c_Handle,
                                         (LPOVERLAPPED)&Overlapped1,
                                         &bytesReturned,
                                         TRUE);        
            if(status)
            {
                std::cout << "Write Data : "<< writeBuf[0] << endl;
                std::cout << endl;
            }
            else
            {
                std::cout << "GetOverlappedResult....1 Failed - Write. "<< GetLastError() << endl; // here GetLastError = 31 means the attached device is not functioning but my device is working fine with other devices.
                std::cout << endl;
            }

    }

    So can any one suggest how can we make this code working?

    Thank You in advanced.


    • Edited by Dhaval08 Thursday, July 16, 2015 9:59 AM
    Thursday, July 16, 2015 9:37 AM