前几天刚要了TQ210的板子,想写个SPI通信应用程序,但是连SPI的驱动都打不开,也是显示"打开SPI驱动失败!”信息,不知是什么问题!
附上代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; 
namespace SpiTest
{
public partial class MainForm : Form
{
public const int GENERIC_ALL = 0x10000000;
public const int GENERIC_EXECUTE = 0x20000000;
public const uint GENERIC_READ = 0x80000000;
public const int GENERIC_WRITE = 0x40000000;
public const int FILE_SHARE_READ = 1;
public const int FILE_SHARE_WRITE = 2;
public const int OPEN_EXISTING = 3;
public IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
public struct OVERLAPPED
{
public UIntPtr Internal;
public UIntPtr InternalHigh;
public uint Offset;
public uint OffsetHigh;
public IntPtr EventHandle;
}
//对设备执行指定的操作,访问设备驱动程序
[DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
private static extern int DeviceIoControl
(
IntPtr hDevice,//设备句柄
int dwIoControlCode,//设备控制选项,带有 FSCTL_ 前缀的常数
IntPtr lpInBuffer,//设备控制选项,具体取决于dwIoControlCode参数
uint nInBufferSize,//输入缓冲区的长度
IntPtr lpOutBuffer,//设备控制选项,具体取决于dwIoControlCode参数
uint nOutBufferSize,//输出缓冲区的长度
ref int lpBytesReturned,//实际装载到输出缓冲区的字节数量
IntPtr lpOverlapped//重叠指针
);
//对设备执行指定的操作,关闭设备驱动程序
[DllImport("coredll.dll", EntryPoint = "CloseHandle")]
private static extern int CloseHandle
(
IntPtr hDevice//设备句柄
);
//创建或打开对象,并返回一个可被对象访问的句柄
[DllImport("coredll.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
);
[DllImport("coredll.dll")]
private static extern bool ReadFile(
int hFile, // 通信设备句柄 handle to file
byte[] lpBuffer, // 数据缓冲区 data buffer
int nNumberOfBytesToRead, // 多少字节等待读取 number of bytes to read
ref int lpNumberOfBytesRead, // 读取多少字节 number of bytes read
ref OVERLAPPED lpOverlapped // 溢出缓冲区 overlapped buffer
);
[DllImport("coredll.dll")]
private static extern bool WriteFile(
int hFile, // 通信设备句柄 handle to file
byte[] lpBuffer, // 数据缓冲区 data buffer
int nNumberOfBytesToWrite, // 多少字节等待写入 number of bytes to write
ref int lpNumberOfBytesWritten, // 已经写入多少字节 number of bytes written
ref OVERLAPPED lpOverlapped // 溢出缓冲区 overlapped buffer
);
private IntPtr hFile;
public MainForm()
{
InitializeComponent();
}
private void btOpen_Click(object sender, EventArgs e)
{
hFile = CreateFile("SPI1:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox.Show("打开SPI驱动失败!");//
return;
}
else
{
MessageBox.Show("打开SPI驱动成功!");
}
}
}
} |