21,615
社区成员




[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern int DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
byte[] lpInBuffer,
int nInBufferSize,
byte[] lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("kernel32.dll")]
private static extern IntPtr CreateFile(
string lpFileName,
// 要打开的串口名称
uint dwDesiredAccess,
// 指定串口的访问方式,一般设置为可读可写方式
int dwShareMode,
// 指定串口的共享模式,串口不能共享,所以设置为0
int lpSecurityAttributes,
// 设置串口的安全属性,WIN9X下不支持,应设为NULL
int dwCreationDisposition,
// 对于串口通信,创建方式只能为OPEN_EXISTING
int dwFlagsAndAttributes,
// 指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED( 重叠I/O操作 ),指定串口以异步方式通信
int hTemplateFile
// 对于串口通信必须设置为NULL
);
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
public Form1()
{
InitializeComponent();
}
private void GPIO测试_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr gpiodriver = CreateFile("GIO20:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
、、就是这一句出错
if (gpiodriver == (IntPtr)(-1))
{
MessageBox.Show("打开GPIO设备失败!");
}
else
{
int bytesReturned = 0;
DeviceIoControl(gpiodriver, 0x01, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
}
}