www.pudn.com > pci9054.rar > Test.cpp
// Test.cpp // // Generated by DriverWizard version DriverStudio 2.6.0 (Build 336) // // This console application demonstrates how to open a handle // to a device in your driver, and communicate with the driver // using Read, Write, and DeviceIoControl calls, as appropriate. // // This test program attempts to open the device with link name // PCI9054Device, which corresponds to Unit 0 of the // PCI9054Device device class in the driver. // // You can build this application using the command line compiler with // the following command: // // cl Test.cpp // #include#include #include #include #include "..\PCI9054ioctl.h" typedef void VOIDFUNC(); // Prototypes void Usage(void); void ShowIoctlValues(void); // TODO: // You can redefine the IOCTL handler prototypes as needed, adding // appropriate parameters that can be collected from the command line. // To do this you must modify the command line parsing logic. An // example of this is shown in comments throughout the test application. // //=== Parameterized IOCTL Example === // void Test_IOCTL_PARAMETERIZED(int nVal, ULONG dwVal); void Test_PCI9054_IOCTL_800_ReadBase0(void); void Test_PCI9054_IOCTL_801_WriteBase0(void); void Test_PCI9054_IOCTL_802_ReadBase2(void); void Test_PCI9054_IOCTL_803_WriteBase2(void); void Test_PCI9054_IOCTL_804_ReadBase3(void); void Test_PCI9054_IOCTL_805_WriteBase3(void); void CloseIfOpen(void); void doRead(int i); void doWrite(int i); // Global data #define N_IOCODES 6 // Names of IOCTL codes // char *IOnames[N_IOCODES+1] = { //=== Parameterized IOCTL Example === // "IOCTL_PARAMETERIZED", "PCI9054_IOCTL_800_ReadBase0", "PCI9054_IOCTL_801_WriteBase0", "PCI9054_IOCTL_802_ReadBase2", "PCI9054_IOCTL_803_WriteBase2", "PCI9054_IOCTL_804_ReadBase3", "PCI9054_IOCTL_805_WriteBase3", "" }; // IOCTL codes // int IOcodes[N_IOCODES+1] = { //=== Parameterized IOCTL Example === // IOCTL_PARAMETERIZED, PCI9054_IOCTL_800_ReadBase0, PCI9054_IOCTL_801_WriteBase0, PCI9054_IOCTL_802_ReadBase2, PCI9054_IOCTL_803_WriteBase2, PCI9054_IOCTL_804_ReadBase3, PCI9054_IOCTL_805_WriteBase3, 0 }; // Handle to device opened in driver. // HANDLE hDevice = INVALID_HANDLE_VALUE; // Name used to open device // char *sLinkName = "\\\\.\\PCI9054Device0"; //////////////////////////////////////////////////////////////////////// // OpenByName // // Open a handle to the requested device // HANDLE OpenByName(void) { // Create a handle to the driver return CreateFile(sLinkName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); } //////////////////////////////////////////////////////////////////////// // Exit // // Print a message and exit // void Exit(int res) { printf("Exiting...\n\n"); CloseIfOpen(); exit(res); } //////////////////////////////////////////////////////////////////////// // Main entry point // // int __cdecl main(int argc, char *argv[]) { int nArgIndex; // Walk through command line arguments int nArgIncrement = 0; int val; //=== Parameterized IOCTL Example === // int nVal; // ULONG dwVal; // printf("Test application Test starting...\n"); hDevice = OpenByName(); if (hDevice == INVALID_HANDLE_VALUE) { printf("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError()); Exit(1); } else { printf("Device found, handle open."); } // Parse the command line if (argc < 2) Usage(); nArgIndex = 1; while (nArgIndex < argc) { // Parse ahead to determine numeric value of argument if (nArgIndex+1 >= argc) Usage(); if (!isdigit(argv[nArgIndex+1][0])) Usage(); val = atoi(argv[nArgIndex+1]); switch (argv[nArgIndex][0]) { case 'r': case 'R': printf("Driver does not have a read handler\n"); nArgIncrement = 2; break; case 'w': case 'W': printf("Driver does not have a write handler\n"); nArgIncrement = 2; break; case 'i': case 'I': if (val >= N_IOCODES) { printf("IO control code index must be less than %d\n", N_IOCODES); ShowIoctlValues(); Exit(1); } switch (IOcodes[val]) { //=== Parameterized IOCTL Example === // case IOCTL_PARAMETERIZED: // if (nArgIndex+3 >= argc) Usage(); // nVal = atoi(argv[nArgIndex+2]); // dwVal = strtoul(argv[nArgIndex+3], NULL, 0); // Test_IOCTL_PARAMETERIZED(nVal, dwVal); // nArgIncrement = 4; // break; case PCI9054_IOCTL_800_ReadBase0: Test_PCI9054_IOCTL_800_ReadBase0(); nArgIncrement = 2; break; case PCI9054_IOCTL_801_WriteBase0: Test_PCI9054_IOCTL_801_WriteBase0(); nArgIncrement = 2; break; case PCI9054_IOCTL_802_ReadBase2: Test_PCI9054_IOCTL_802_ReadBase2(); nArgIncrement = 2; break; case PCI9054_IOCTL_803_WriteBase2: Test_PCI9054_IOCTL_803_WriteBase2(); nArgIncrement = 2; break; case PCI9054_IOCTL_804_ReadBase3: Test_PCI9054_IOCTL_804_ReadBase3(); nArgIncrement = 2; break; case PCI9054_IOCTL_805_WriteBase3: Test_PCI9054_IOCTL_805_WriteBase3(); nArgIncrement = 2; break; default: printf("IO control code not valid\n"); Exit(1); } break; case '?': case 'h': default: Usage(); } nArgIndex += nArgIncrement; } CloseIfOpen(); return 0; } //////////////////////////////////////////////////////////////////////// // CloseIfOpen // // Close the device if we previously opened a handle to it. // void CloseIfOpen(void) { if (hDevice != INVALID_HANDLE_VALUE) { // Close the handle to the driver if (!CloseHandle(hDevice)) { printf("ERROR: CloseHandle returns %0x.\n", GetLastError()); } hDevice = INVALID_HANDLE_VALUE; } } //////////////////////////////////////////////////////////////////////// // doRead // // Read 'n' bytes of data from the device // // Note: This simple test app reads data from the device and displays the // data as characters. This behavior can be modified as appropriate // for your device. // void doRead(int n) { char *buf; ULONG nRead; int i; int j; buf = (char *) malloc(n); if (buf == NULL) { printf("Failed to allocate buffer for read"); Exit(1); } // Read data from driver printf("Reading from device - "); ReadFile(hDevice, buf, n, &nRead, NULL); printf("%d bytes read from device (%d requested).\n", nRead, n); // Print what was read i = 0; while(i < n) { j = min((i+26),n); for(; i < j; i++) { printf("%c, ", buf[i]); } printf("\n"); } free(buf); } //////////////////////////////////////////////////////////////////////// // doWrite // // Write 'n' bytes of data to the device // // Note: This simple test app writes sequential characters to the // device. This behavior can be modified as appropriate // for your device. // void doWrite(int n) { char *buf; ULONG nWritten; int i; int j; buf = (char *) malloc(n); if (buf == NULL) { printf("Failed to allocate buffer for write"); Exit(1); } // start with the mod26 letter of the number of bytes to write j = (n % 26); // load buffer with dummy data (abcdefg...) for (i=0; i data read : %x",bufOutput[0]); } //////////////////////////////////////////////////////////////////////// // Test_PCI9054_IOCTL_801_WriteBase0 // // Test one Io Control Code // // TODO: // Pass appropriate arguments to your device and check // the return value // void Test_PCI9054_IOCTL_801_WriteBase0(void) { // Note that Input and Output are named from the point of view // of the DEVICE: // bufInput supplies data to the device // bufOutput is written by the device to return data to this application ULONG bufInput[IOCTL_INBUF_SIZE]; // Input to device ULONG nOutput; // Count written to bufOutput bufInput[0]=0Xfffff000; // Call device IO Control interface (PCI9054_IOCTL_800_ReadBase0) in driver printf("\n----------- ready for writing to Base0 -----------"); printf("\nthe data for writing is: %x",bufInput[0]); if (!DeviceIoControl(hDevice, PCI9054_IOCTL_801_WriteBase0, bufInput, sizeof(bufInput), NULL,//bufOutput, 0,//IOCTL_OUTBUF_SIZE, &nOutput, NULL) ) { printf("\nERROR: DeviceIoControl returns %0x.", GetLastError()); Exit(1); } } //////////////////////////////////////////////////////////////////////// // Test_PCI9054_IOCTL_802_ReadBase2 // // Test one Io Control Code // // TODO: // Pass appropriate arguments to your device and check // the return value // void Test_PCI9054_IOCTL_802_ReadBase2(void) { USHORT bufOutput[IOCTL_OUTBUF_SIZE]; // 缓冲区,用于传出读取的数据,IOCTL_OUTBUF_SIZE在前面定义,为128 ULONG nOutput; // 实际读取的数据字节数 USHORT bufInput[2]; // 传入读取的参数,其中第一个元素为指定的读取偏移地址, // 第二个元素为指定的读取的数据个数 USHORT offset; // 定义的变量,用于存放要读取的偏移地址 printf("\n----------- ready for reading from Base2 -----------"); //由用户指定读取的偏移地址: printf("\nPlease input the offset of read operation(Hex):"); scanf("%x",&offset); //把获取的读取偏移地址赋值给bufInput第一个元素 bufInput[0]=offset; //把要读取的数据个数赋值给bufInput第二个元素 bufInput[1]=IOCTL_OUTBUF_SIZE; // 调用DeviceIoControl()函数 if (!DeviceIoControl(hDevice, PCI9054_IOCTL_802_ReadBase2, bufInput, 2*2, // 字节数,为数组bufInput的大小 bufOutput, IOCTL_OUTBUF_SIZE*2, // 字节数 &nOutput, NULL) ) { printf("\nERROR: DeviceIoControl returns %0x.", GetLastError()); Exit(1); } //显示读取的数据 printf("\n------>>>>>> data read <<<<<<------"); for(ULONG i=0;i >>>>> data read <<<<<<------"); for(ULONG i=0;i \n"); // printf( " Example: i %d \n", i); // } } }