usb wirtefile失敗

hw37du 2011-11-26 03:37:22
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;

namespace usbwritefile
{
class Program

{
int HidHandle = -1;
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const int OPEN_EXISTING = 3;
//獲取設備文件
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int CreateFile(
string lpFileName, // file name
uint dwDesiredAccess, // access mode
uint dwShareMode, // share mode
uint lpSecurityAttributes, // SD
uint dwCreationDisposition, // how to create
uint dwFlagsAndAttributes, // file attributes
uint hTemplateFile // handle to template file
);
[DllImport("Kernel32.dll")]
private static extern bool ReadFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToRead,
ref uint lpNumberOfBytesRead,
IntPtr lpOverlapped
);
[DllImport("Kernel32.dll", SetLastError = true)]
private static extern bool WriteFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
ref uint lpNumberOfBytesWritten,
IntPtr lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
IntPtr hObject
);
//--------------------------------------------------------------------------------
IntPtr hFile;
private const Int32 INVALID_HANDLE_VALUE = -1;
private const int USB_WRITENUM = 8;
private const int USB_READNUM = 8;
private byte[] m_rd_data = new byte[USB_READNUM];
public byte[] rd_data
{
get { return m_rd_data; }
set { m_rd_data = value; }
}
private byte[] m_wr_data = new byte[USB_WRITENUM];
public byte[] wr_data
{
get { return m_wr_data; }
set { m_wr_data = value; }
}
public int OnInitUSB(string DeviceName)
{
HidHandle = CreateFile(
DeviceName,
GENERIC_READ,// | GENERIC_WRITE,//讀寫,或者一起
FILE_SHARE_READ,// | FILE_SHARE_WRITE,//共享讀寫,或者一起
0,
OPEN_EXISTING,
0,
0);
if (HidHandle == -1)
{
return 0;
}
else
{
return 1;
}
}
public bool USBDataRead()
{
uint read = 0;
return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero);
}
public bool USBDataWrite()
{
uint bytesWritten = 0;
byte[] buffer = new byte[] { 0x02, 0x02, 0x00 };
bool status = WriteFile((IntPtr)HidHandle, buffer, 8, ref bytesWritten, IntPtr.Zero);
if (!status)
{
int errorCode = Marshal.GetLastWin32Error();
string errorMessage = new Win32Exception(errorCode).Message;
Console.WriteLine(errorCode + ":" + errorMessage);
MessageBox.Show(errorCode + ":" + errorMessage);
return false;
}
else
{
Console.WriteLine(bytesWritten.ToString());
return true;
}
}
public void CloseConnection()
{
if (hFile.ToInt32() != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = IntPtr.Zero;
}
}

static void Main(string[] args)
{
Program p = new Program();
p.OnInitUSB("\\\\?\\hid#vid_046d&pid_0a29&mi_03&col02#7&ca16861&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}");
p.USBDataWrite();
Console.ReadKey();


}
}
}

大家好.上面是我写的一个关于给usb设备发送数据的程序,但是现在出现了问题,createfile可以成功,可以获取到句柄,但是到writefile发送数据的时候,总是出现参数无效
请问大家该怎么解决?
谢谢!
...全文
76 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hw37du 2011-11-27
  • 打赏
  • 举报
回复
求高手啊 。。
hw37du 2011-11-26
  • 打赏
  • 举报
回复
怎么没有人回答啊。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧