软件接口程序
虽然Win95 OSR2.1 支持USB,但微软推荐在win98或win2000上使用USB。软件部分由WDM驱动程序和应用程序组成。对于USB客户驱动程序,主要是与 USBDI打交道,下面的代码完成了对于IRP_MJ_READ和IRP_MJ_WRITE的响应。
/////////////////////////////////////////////////////////////////////////////
// U***gatherRead:
//
// Description:
// Handle IRP_MJ_READ requests
//
// Arguments:
// Pointer to our FDO
// Pointer to the IRP
// IrpStack-》Parameters.Read.xxx has read parameters
// MdlAddress (direct I/O)
//
// Return Value:
// This function returns STATUS_XXX
NTSTATUS U***gatherRead( IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PUSBgather_DEVICE_EXTENSION dx = (PUSBgather_DEVICE_EXTENSION)fdo-》DeviceExtension;
if( dx-》IODisabled)
return CompleteIrp( Irp, STATUS_DEVICE_NOT_CONNECTED, 0);
if (!LockDevice(dx))
return CompleteIrp( Irp, STATUS_DELETE_PENDING, 0);
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status = STATUS_SUCCESS;
ULONG BytesTxd = 0;
// 得到参数
LONGLONG FilePointer = IrpStack-》Parameters.Read.ByteOffset.QuadPart;
ULONG ReadLen = IrpStack-》Parameters.Read.Length;
// 检查文件指针
if( FilePointer0)
status = STATUS_INVALID_PARAMETER;
else
{
status = U***DoInterruptTransfer( dx, Irp-》AssociatedIrp.SystemBuffer, ReadLen);
BytesTxd = ReadLen;
}
// 完成 IRP
CompleteIrp(Irp,status,BytesTxd);
UnlockDevice(dx);
return status;
}
/////////////////////////////////////////////////////////////////////////////
// U***gatherWrite:
//
// Description:
// Handle IRP_MJ_WRITE requests
//
// Arguments:
// Pointer to our FDO
// Pointer to the IRP
// IrpStack-》Parameters.Write.xxx has write parameters
// User buffer at: AssociatedIrp.SystemBuffer (buffered I/O)
// MdlAddress (direct I/O)
//
// Return Value:
// This function returns STATUS_XXX
NTSTATUS U***gatherWrite( IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PUSBgather_DEVICE_EXTENSION dx = (PUSBgather_DEVICE_EXTENSION)fdo-》DeviceExtension;
if( dx-》IODisabled)
return CompleteIrp( Irp, STATUS_DEVICE_NOT_CONNECTED, 0);
if (!LockDevice(dx))
return CompleteIrp( Irp, STATUS_DELETE_PENDING, 0);
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status = STATUS_SUCCESS;
ULONG BytesTxd = 0;
// 得到参数
LONGLONG FilePointer = IrpStack-》Parameters.Write.ByteOffset.QuadPart;
ULONG WriteLen = IrpStack-》Parameters.Write.Length;
if( FilePointer0 || WriteLen1)
status = STATUS_INVALID_PARAMETER;
else
{
// 仅写一个字节
BytesTxd = 1;
PUCHAR pData = (PUCHAR)Irp-》AssociatedIrp.SystemBuffer;
U***SendOutputReport( dx, *pData);
}
// 完成 IRP
CompleteIrp(Irp,status,BytesTxd);
UnlockDevice(dx);
return status;
}
应用程序采用标准的文件操作方法。使用CreateFile API打开文件。使用WriteFile API发出开始命令,启动ADC,使用ReadFile读回采样值。
软件接口程序
虽然Win95 OSR2.1 支持USB,但微软推荐在win98或win2000上使用USB。软件部分由WDM驱动程序和应用程序组成。对于USB客户驱动程序,主要是与 USBDI打交道,下面的代码完成了对于IRP_MJ_READ和IRP_MJ_WRITE的响应。
/////////////////////////////////////////////////////////////////////////////
// U***gatherRead:
//
// Description:
// Handle IRP_MJ_READ requests
//
// Arguments:
// Pointer to our FDO
// Pointer to the IRP
// IrpStack-》Parameters.Read.xxx has read parameters
// MdlAddress (direct I/O)
//
// Return Value:
// This function returns STATUS_XXX
NTSTATUS U***gatherRead( IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PUSBgather_DEVICE_EXTENSION dx = (PUSBgather_DEVICE_EXTENSION)fdo-》DeviceExtension;
if( dx-》IODisabled)
return CompleteIrp( Irp, STATUS_DEVICE_NOT_CONNECTED, 0);
if (!LockDevice(dx))
return CompleteIrp( Irp, STATUS_DELETE_PENDING, 0);
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status = STATUS_SUCCESS;
ULONG BytesTxd = 0;
// 得到参数
LONGLONG FilePointer = IrpStack-》Parameters.Read.ByteOffset.QuadPart;
ULONG ReadLen = IrpStack-》Parameters.Read.Length;
// 检查文件指针
if( FilePointer0)
status = STATUS_INVALID_PARAMETER;
else
{
status = U***DoInterruptTransfer( dx, Irp-》AssociatedIrp.SystemBuffer, ReadLen);
BytesTxd = ReadLen;
}
// 完成 IRP
CompleteIrp(Irp,status,BytesTxd);
UnlockDevice(dx);
return status;
}
/////////////////////////////////////////////////////////////////////////////
// U***gatherWrite:
//
// Description:
// Handle IRP_MJ_WRITE requests
//
// Arguments:
// Pointer to our FDO
// Pointer to the IRP
// IrpStack-》Parameters.Write.xxx has write parameters
// User buffer at: AssociatedIrp.SystemBuffer (buffered I/O)
// MdlAddress (direct I/O)
//
// Return Value:
// This function returns STATUS_XXX
NTSTATUS U***gatherWrite( IN PDEVICE_OBJECT fdo,
IN PIRP Irp)
{
PUSBgather_DEVICE_EXTENSION dx = (PUSBgather_DEVICE_EXTENSION)fdo-》DeviceExtension;
if( dx-》IODisabled)
return CompleteIrp( Irp, STATUS_DEVICE_NOT_CONNECTED, 0);
if (!LockDevice(dx))
return CompleteIrp( Irp, STATUS_DELETE_PENDING, 0);
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status = STATUS_SUCCESS;
ULONG BytesTxd = 0;
// 得到参数
LONGLONG FilePointer = IrpStack-》Parameters.Write.ByteOffset.QuadPart;
ULONG WriteLen = IrpStack-》Parameters.Write.Length;
if( FilePointer0 || WriteLen1)
status = STATUS_INVALID_PARAMETER;
else
{
// 仅写一个字节
BytesTxd = 1;
PUCHAR pData = (PUCHAR)Irp-》AssociatedIrp.SystemBuffer;
U***SendOutputReport( dx, *pData);
}
// 完成 IRP
CompleteIrp(Irp,status,BytesTxd);
UnlockDevice(dx);
return status;
}
应用程序采用标准的文件操作方法。使用CreateFile API打开文件。使用WriteFile API发出开始命令,启动ADC,使用ReadFile读回采样值。
举报