天嵌 ARM开发社区

 找回密码
 注册
查看: 3975|回复: 5

C#写SPI通信应用程序问题

[复制链接]
Afeila 发表于 2013-12-5 19:34:31 | 显示全部楼层 |阅读模式
前几天刚要了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驱动成功!");   
         
            }
        }
    }
}
Jangel 发表于 2013-12-6 09:25:23 | 显示全部楼层
目前TQ210的WinCE SPI未提供
 楼主| Afeila 发表于 2013-12-6 09:59:42 | 显示全部楼层
Jangel 发表于 2013-12-6 09:25
目前TQ210的WinCE SPI未提供

谢谢版主一大早给我回复!

如果想在WINCE 下实现 SPI通信还有其它的办法吗?

再次麻烦版主了,谢谢
Jangel 发表于 2013-12-6 12:05:01 | 显示全部楼层
Afeila 发表于 2013-12-6 09:59
谢谢版主一大早给我回复!

如果想在WINCE 下实现 SPI通信还有其它的办法吗?

不用谢,要先完善spi驱动部分
alison 发表于 2013-12-6 14:56:04 | 显示全部楼层
哪方面需要完善呢?能不能给个思路啊
alison 发表于 2013-12-6 16:42:59 | 显示全部楼层
Jangel 发表于 2013-12-6 09:25
目前TQ210的WinCE SPI未提供

有没有人用TQ210 wince6+SPI成功过啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-6-17 13:12 , Processed in 1.046875 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表