wangsonglei007
驱动牛犊
驱动牛犊
  • 注册日期2002-11-24
  • 最后登录2003-02-22
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1016回复:1

WDM之新手SOS!!!SOS!!!

楼主#
更多 发布于:2002-12-18 17:15
本人在WIN98下按照DriverWizard生成一个基于ISA的WDM,选择基于ISA总线并选择了一个I/O端口进行读写,其余均默认设置,并生成测试程序,生成的INF文件改I/O端口为0310,想对此端口写操作。添加设备并重启机器后,查看系统属性设备资源正确,运行测试程序,程序提示“Device found,Handle Open”但对端口写1个数据时,却不能写入并提示“0 BYTE WRITEN ON THE DEVICE(1 ATTEMPT)”
以下为测试程序代码(由DRIVER WIZARD 自动生成)
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>


#include \"..\\CANISADeviceinterface.h\" // Has class GUID definition

// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);

typedef void VOIDFUNC();

// Prototypes
void Usage(void);

void CloseIfOpen(void);
void doRead(int i);
void doWrite(int i);

// Global data



// Handle to device opened in driver.
//
HANDLE hDevice = INVALID_HANDLE_VALUE;

// Class GUID used to open device
//
GUID ClassGuid = CANISADevice_CLASS_GUID;


////////////////////////////////////////////////////////////////////////
// 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;
DWORD Error;

printf(\"Test application Test_CANISA starting...\\n\");

hDevice = OpenByInterface( &ClassGuid, 0, &Error);
if (hDevice == INVALID_HANDLE_VALUE)
{
printf(\"ERROR opening device: (%0x) returned from CreateFile\\n\", GetLastError());
Exit(1);
}
else
{
printf(\"Device found, handle open.\\n\");
}

// 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\':
doRead(val);
nArgIncrement = 2;
break;

case \'w\':
case \'W\':
doWrite(val);
nArgIncrement = 2;
break;

case \'i\':
case \'I\':
  printf(\"No IO Control Codes defined\\n\");
  Exit(1);

case \'?\':
case \'h\':
default:
Usage();
}
nArgIndex += nArgIncrement;
}

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;
DWORD   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);
}
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;
DWORD 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<n; i++, j=(j + 1)%26)
{
buf = 1;//此处为本人所改欲往该端口写“1”
}

// Write data to driver
printf(\"Writing to device - \");
WriteFile(hDevice, buf, n, &nWritten, NULL);
printf(\"%d bytes written to device (%d attempted).\\n\", nWritten, n);

// Print what was written
i = 0;
while(i < n)
{
j = min((i+26),n);
for(; i < j; i++)
{
printf(\"%d, \", buf);
}
printf(\"\\n\");
}

free(buf);
}


////////////////////////////////////////////////////////////////////////
// Usage
//
// Print a usage message describing arguments to this program
//
void Usage(void)
{
printf(\"Usage: Test_CANISA [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\");
    printf(\"         (no IO Control Codes are defined)\\n\");
printf(\"Example:\\n\");
printf(\"    Test_CANISA r 32 w 32\\n\");
printf(\"        read 32 bytes, then write 32 bytes\\n\");

Exit(1);
}

本人怀疑是否Writrfile()有问题,但查了MSDN未发现问题,求各位大侠给予指点!!!多谢!!!

[编辑 -  12/18/02 by  wangsonglei007]
legend_my
驱动牛犊
驱动牛犊
  • 注册日期2002-08-24
  • 最后登录2003-10-23
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2002-12-26 16:22
帖子太长,无法读懂!
游客

返回顶部