⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbfxtest.cpp

📁 基于vc++6.0环境的cypress USB 驱动源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

            for (UCHAR i = 0; i < 8; i++) {

                sevenSegment = 1 << i;

                if (!DeviceIoControl(deviceHandle,
                                     IOCTL_OSRUSBFX2_SET_7_SEGMENT_DISPLAY,
                                     &sevenSegment,             // Ptr to InBuffer
                                     sizeof(UCHAR),            // Length of InBuffer
                                     NULL,                 // Ptr to OutBuffer
                                     0,         // Length of OutBuffer
                                     &bytesRet,                     // BytesReturned
                                     0)) {                       // Ptr to Overlapped structure

                    code = GetLastError();

                    printf("DeviceIoControl failed with error 0x%x\n", code);

                    return(code);
                }

                printf("This is %d\n", i+1);
                Sleep(500);

            }

            printf("7 Segment mask:  0x%x\n", sevenSegment);
            break;

        case 0:
        default:

            CloseHandle(deviceHandle);

            //
            // zero is get out!
            //
            return(0);

        }
    }   
}


///////////////////////////////////////////////////////////////////////////////
//
//  FindUSBFX2LKDevice
//
//    This routine tries to return a handle to a USB FX2 Learning Kit device.
//     If multiple devices are found, a handle to the first available
//     one found is returned;
//
//  INPUTS:
//
//      None
//
//  OUTPUTS:
//
//      Handle - Upon successful return, this is filled in 
//                with a valid, open handle to a USB FX2 device.
//                HANDLE must be closed by the caller. 
//
//  RETURNS:
//
//      TRUE if a device has been found and Handle contains
//       a valid HANDLE. FALSE otherwise.
//
//
//  NOTES:
//
///////////////////////////////////////////////////////////////////////////////
BOOLEAN 
FindUSBFX2LKDevice(
    PHANDLE Handle
    ) {

    HDEVINFO                         devInfo;
    SP_DEVICE_INTERFACE_DATA         devInterfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA devInterfaceDetailData = NULL;
    ULONG                            devIndex;
    ULONG                            requiredSize;
    ULONG                            code = ERROR_SUCCESS;
    PCHAR                            devicePath = NULL;


    //
    // Open a handle to the device using the 
    //  device interface that the driver registers
    //
    //

    //
    // Get the device information set for all of the
    //  devices of our class (the GUID we defined
    //  in nothingioctl.h and registered in the driver
    //  with DfwDeviceCreateDeviceInterface) that are present in the 
    //  system
    //
    devInfo = SetupDiGetClassDevs(&GUID_OSR_USBFX2LK_INTERFACE,
                                  NULL,
                                  NULL,
                                  DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    if (devInfo == INVALID_HANDLE_VALUE) {

        printf("SetupDiGetClassDevs failed with error 0x%x\n", GetLastError());

        return FALSE;

    }

    //
    // Now get information about each device installed...
    //
    
    //
    // This needs to be set before calling
    //  SetupDiEnumDeviceInterfaces
    //
    devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    //
    // Start with the first device...
    //
    devIndex = 0;

    while (SetupDiEnumDeviceInterfaces(devInfo,
                                       NULL,
                                       &GUID_OSR_USBFX2LK_INTERFACE,
                                       devIndex++, 
                                       &devInterfaceData)) {

        //
        // If you actually had a reason to keep
        //  track of all the devices in the system
        //  you obviously wouldn't want to just
        //  throw these away. Since we're just 
        //  running through to print them out
        //  and picking whatever the last one
        //  is we'll alloc and free these
        //  as we go...
        //
        if (devInterfaceDetailData != NULL) {

            free(devInterfaceDetailData);

            devInterfaceDetailData = NULL;

        }

        //
        // The entire point of this exercise is
        //  to get a string that we can hand to 
        //  CreateFile to get a handle to the device,
        //  so we need to call SetupDiGetDeviceInterfaceDetail
        //  (which will give us the string we need)
        //

        //
        // First call it with a NULL output buffer to
        //  get the number of bytes needed...
        //
        if (!SetupDiGetDeviceInterfaceDetail(devInfo,
                                             &devInterfaceData,
                                             NULL,
                                             0,
                                             &requiredSize,
                                             NULL)) {

            code = GetLastError();

            //
            // We're expecting ERROR_INSUFFICIENT_BUFFER.
            //  If we get anything else there's something
            //  wrong...
            //
            if (code != ERROR_INSUFFICIENT_BUFFER) {

                printf("SetupDiGetDeviceInterfaceDetail failed with error 0x%x\n", code);

                //
                // Clean up the mess...
                //
                SetupDiDestroyDeviceInfoList(devInfo);
                
                return FALSE;

            }

        }

        //
        // Allocated a PSP_DEVICE_INTERFACE_DETAIL_DATA...
        //
        devInterfaceDetailData = 
                (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(requiredSize);

        if (!devInterfaceDetailData) {

            printf("Unable to allocate resources...Exiting\n");

            //
            // Clean up the mess...
            //
            SetupDiDestroyDeviceInfoList(devInfo);

            return FALSE;

        }

        //
        // This needs to be set before calling
        //  SetupDiGetDeviceInterfaceDetail. You
        //  would *think* that you should be setting
        //  cbSize to requiredSize, but that's not the 
        //  case. 
        //
        devInterfaceDetailData->cbSize = 
                    sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);


        if (!SetupDiGetDeviceInterfaceDetail(devInfo,
                                             &devInterfaceData,
                                             devInterfaceDetailData,
                                             requiredSize,
                                             &requiredSize,
                                             NULL)) {

            printf("SetupDiGetDeviceInterfaceDetail failed with error 0x%x\n", GetLastError());

            //
            // Clean up the mess...
            //
            SetupDiDestroyDeviceInfoList(devInfo);

            free(devInterfaceDetailData);

            return FALSE;

        }

        //
        // Got one!
        //
        printf("Device found! %s\n", devInterfaceDetailData->DevicePath);

        //
        // We don't really care which one we get, 
        //  just try to open each one and quit when we succeeed...
        //
        *Handle = CreateFile(devInterfaceDetailData->DevicePath,  // Name of the NT "device" to open
                             GENERIC_READ|GENERIC_WRITE,  // Access rights requested
                             0,                           // Share access - NONE
                             0,                           // Security attributes - not used!
                             OPEN_EXISTING,               // Device must exist to open it.
                             0,                           // Open for overlapped I/O
                             0);                          // extended attributes - not used!

        if (*Handle == INVALID_HANDLE_VALUE) {

            printf("CreateFile failed with error 0x%x\n", GetLastError());

        } else {

            SetupDiDestroyDeviceInfoList(devInfo);
        
            free(devInterfaceDetailData);

            return TRUE;

        }

    }

    //
    // We're here if we ran our of devices to enumarate or if
    //  something went wrong with our initial call to 
    //  SetupDiEnumDeviceInterfaces
    //

    if (code != ERROR_NO_MORE_ITEMS) {

        //
        // Something tr
        //
        printf("SetupDiGetDeviceInterfaceDetail failed with error 0x%x\n", code);
        
        //
        // Clean up the mess...
        //
        return FALSE;

    }

    //
    // ERROR_NO_MORE_ITEMS means that there are either no
    //  devices to enumerate or all of the devices 
    //  refused our open request.
    //
    SetupDiDestroyDeviceInfoList (devInfo);
    
    printf("Unable to find or open any OSR USB FX2 devices!\n");

    return FALSE;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -