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

📄 test.cpp

📁 利用DriverWorks实现PCI9054IO读写操作。包括如何使用DW开发向导生成程序框架
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
void Usage(void)
{
	printf("Usage: Test [r n] [w n] [i n]\n");
	printf("       r initiates a read of specified number of bytes\n");
	printf("       w initiates a write of specified number of bytes\n");
	printf("       i initiates an IO Control Code message with specified index value\n");
	ShowIoctlValues();
	printf("Example:\n");
	printf("    Test r 32 w 32\n");
	printf("        read 32 bytes, then write 32 bytes\n");

	Exit(1);
}



#define	IOCTL_INBUF_SIZE	128         //写入128个数据
#define	IOCTL_OUTBUF_SIZE	128         //读出128个数据

//=== Parameterized IOCTL Example ===
//void Test_IOCTL_PARAMETERIZED(int nVal, ULONG dwVal)
//{
//  Function body same as other IOCTL handlers, with command line
//	parameters 'nVal' and 'dwVal' available as input.
//}

////////////////////////////////////////////////////////////////////////
// Test_PCI9054_IOCTL_800_ReadBase0
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
void Test_PCI9054_IOCTL_800_ReadBase0(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	bufOutput[1];	// Output from device
	ULONG	nOutput;						// Count written to bufOutput

	// Call device IO Control interface (PCI9054_IOCTL_800_ReadBase0) in driver
	printf("\n----------- ready for reading Base0 -----------");
	if (!DeviceIoControl(hDevice,
						 PCI9054_IOCTL_800_ReadBase0,
						 NULL,
						 0,
						 bufOutput,
						 sizeof(bufOutput),
						 &nOutput,
						 NULL)
	   )
	{
		printf("\nERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
    printf("\n------>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*sizeof(USHORT),     // 字节数,为数组bufInput的大小
						 bufOutput,
						 IOCTL_OUTBUF_SIZE*sizeof(USHORT),    // 字节数
						 &nOutput,
						 NULL)
	   )
	{
		printf("\nERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}

	//显示读取的数据
    printf("\n------>>>>>> data read <<<<<<------");
	for(ULONG i=0;i<nOutput/sizeof(USHORT);i++)
	{
		printf("\n      read data[%d] = %x",i,bufOutput[i]);
	}
	printf("\n\n");
}

////////////////////////////////////////////////////////////////////////
// Test_PCI9054_IOCTL_803_WriteBase2
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
void Test_PCI9054_IOCTL_803_WriteBase2(void)
{	
	ULONG	nOutput;						// Count written to bufOutput

	
	// 传给驱动程序要写入的参数和数据
	// 数组的第一个元素为写入的偏移地址,第二个元素为数据的个数,其它元素为真正要写入的数据
	USHORT	bufInput[IOCTL_INBUF_SIZE+2];	//IOCTL_INBUF_SIZE的定义在前面,为128
	USHORT  offset;       // 写入的偏移地址
	USHORT  num;          // 写入的初始数据,以此来产生一个数组
	
	printf("\n----------- ready for writing to Base2 -----------");
	
	//获取数据要写入的偏移地址,由用户指定
	printf("\nPlease input the offset of the write operation(Hex):");
	scanf("%x",&offset);

	bufInput[0]=offset;   //将用户指定的写入的偏移地址赋值给bufInput数组的第一个元素

	bufInput[1]=IOCTL_INBUF_SIZE;  //把要写入的数据个数赋值给bufInput数组的第二个元素

	//由用户指定一个初始数据,然后通过该它产生IOCTL_INBUF_SIZE个数据
	printf("\nPlease input the initial data to write(Hex):");
	scanf("%x",&num);
	for(ULONG j=0;j<IOCTL_INBUF_SIZE;j++)
	{
		bufInput[2+j]=num;
		num+=1;
	}

	//显示生成的数组
	printf("\n        the offset = %x",bufInput[0]);
	printf("\nthe number of data = %d",bufInput[1]);
	for(ULONG i=0;i<IOCTL_INBUF_SIZE;i++)
	{
		printf("\n     write data[%d] = %x (Hex)",i,bufInput[i+2]);
	}

	// 调用DeviceIoControl()函数
	if (!DeviceIoControl(hDevice,
						 PCI9054_IOCTL_803_WriteBase2,
						 bufInput,
						 (IOCTL_INBUF_SIZE+2)*sizeof(USHORT),  //字节数
						 NULL,
						 0,
						 &nOutput,
						 NULL)
	   )
	{
		printf("\nERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}

	printf("\n\n");
}

////////////////////////////////////////////////////////////////////////
// Test_PCI9054_IOCTL_804_ReadBase3
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
void Test_PCI9054_IOCTL_804_ReadBase3(void)
{
	ULONG	bufOutput[IOCTL_OUTBUF_SIZE];	// 传出读取的数据缓冲区
	ULONG	nOutput;						// 实际读取的数据个数

    ULONG   bufInput[2];	 	            // 传入读取的参数
	ULONG   offset;                         // 要读取的偏移地址

	printf("\n----------- ready for reading from Base3 -----------");

	//获取读取的偏移地址:
	printf("\nPlease input the offset of read operation(Hex):");
	scanf("%x",&offset);

//	//获取读取的数据个数:
//	printf("\nPlease input the number of data to read(Dec):");
//	scanf("%d",&number);

	bufInput[0]=offset;
	bufInput[1]=IOCTL_OUTBUF_SIZE;

	// Call device IO Control interface (PCI9054_IOCTL_804_ReadBase3) in driver
	if (!DeviceIoControl(hDevice,
						 PCI9054_IOCTL_804_ReadBase3,
						 bufInput,
                         2*4,     // 字节
						 bufOutput,
						 IOCTL_OUTBUF_SIZE*4,//sizeof(bufOutput),
						 &nOutput,
						 NULL)
	   )
	{
		printf("\nERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
    printf("\n------>>>>>> data read <<<<<<------");
	for(ULONG i=0;i<nOutput;i++)
	{
		printf("\nread data[%d]=%x",i,bufOutput[i]);
	}
	printf("\n\n");

}

////////////////////////////////////////////////////////////////////////
// Test_PCI9054_IOCTL_805_WriteBase3
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
void Test_PCI9054_IOCTL_805_WriteBase3(void)
{
	ULONG	nOutput;						// Count written to bufOutput

	// 传给驱动程序要写入的参数和数据
	// 数组的第一个元素为写入的偏移地址,第二个元素为数据的个数,其它元素为写入的数据
	ULONG	bufInput[IOCTL_INBUF_SIZE+2];	
	ULONG   offset;       // 写入的偏移地址
	ULONG   num;          // 写入的初始数据,以此来产生一个数组
	
	printf("\n----------- ready for writing to Base3 -----------");
	
	//获取写入的偏移地址
	printf("\nPlease input the offset of the write operation(Hex):");
	scanf("%x",&offset);

//	//获取写入的数据个数
//	printf("Please input the number of data to write(Dec):");
//	scanf("%d",&num);

	bufInput[0]=offset;
	bufInput[1]=IOCTL_INBUF_SIZE;

	//获取写入的数据
	printf("\nPlease input the initial data to write(Hex):");
	scanf("%x",&num);
	for(ULONG j=0;j<IOCTL_INBUF_SIZE;j++)
	{
		bufInput[2+j]=num;
		num+=0xfffff;
	}

	//显示生成的数组
	for(ULONG i=0;i<IOCTL_INBUF_SIZE+2;i++)
	{
		printf("\nwrite data[%d]=%x",i,bufInput[i]);
	}

	// Call device IO Control interface in driver
	if (!DeviceIoControl(hDevice,
						 PCI9054_IOCTL_805_WriteBase3,
						 bufInput,
						 (IOCTL_INBUF_SIZE+2)*4,//sizeof(bufInput),
						 NULL,//bufOutput,
						 0,//sizeof(bufOutput),
						 &nOutput,
						 NULL)
	   )
	{
		printf("\nERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
/*
	printf("\n---------- data have been written to Base2 ----------");
	for(ULONG j=0;j<nOutput;j++)
	{
		printf("\nwritten data[%d]=%x",j,bufOutput[j]);
	}
*/
	printf("\n\n");
}


////////////////////////////////////////////////////////////////////////
// ShowIoctlValues
//
//		Print list of IO Control Code values for usage display
//
void ShowIoctlValues(void)
{
	int i;

	for (i=0; i<N_IOCODES; i++)
	{
		if (i==0)
			printf( "         IO control code index\n");
		printf( "           %d is code %s [%x]\n", i, IOnames[i], IOcodes[i]);

//=== Parameterized IOCTL Example ===
//		if (IOcodes[i] == IOCTL_PARAMETERIZED)
//		{
//			printf( "               and has two arguments: <arg1 desc.> <arg1 desc.>\n");
//			printf( "               Example: i %d <IOCTL index> <ex. arg1> <ex. arg2>\n", i);
//		}
	}
}

⌨️ 快捷键说明

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