查看: 7799|回复: 0

C#下采用索引对WinIO的封装类(WinIO.cs)

[复制链接]
发表于 2009-4-3 00:10:04 | 显示全部楼层 |阅读模式
关键词: WinIO , 封装 , 索引
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;//MessageBox需要
using System.Runtime.InteropServices;//加动态链接库需要
//菜农HotPower@126.com  2008.11.12 于雁塔村
namespace WindowsFormsApplication2
{
    #region C#下采用索引对WinIO的封装类(WinIO.cs)
    /*
       应用时应该在Form1.cs中加入:
       private WinIO WinIOPort = new WinIO();
            例: WinIOPort[0x378] = 0x55;
                 x = WinIOPort[0x379, 2];
    */
    public class WinIO
    {
        //动态链接库的引用
        [DllImport("winio.dll")]
        public static extern bool InitializeWinIo();
        [DllImport("winio.dll")]
        public static extern void ShutdownWinIo();
        [DllImport("winio.dll")]
        public static extern bool GetPortVal(IntPtr wPortAddr, out uint pdwPortVal, byte
bSize);
        [DllImport("winio.dll")]
        public static extern bool SetPortVal(IntPtr wPortAddr, IntPtr dwPortVal, byte
bSize);
        /*
        //以下是WinIO与并口无关的函数
                    [DllImport("winio.dll")]
                    public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize,
IntPtr PhysicalMemoryHandle);
                    [DllImport("winio.dll")]
                    public static extern bool UnmapPhysicalMemory(IntPtr
PhysicalMemoryHandle, byte pbLinAddr);
                    [DllImport("winio.dll")]
                    public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte
pdwPhysVal);
                    [DllImport("winio.dll")]
                    public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte
dwPhysVal);
                    [DllImport("user32.dll")]
                    public static extern int MapVirtualKey(uint Ucode, uint uMapType);
        */
        //属性字段
        private bool enable = false;//WinIO不可用
        private bool error = false;//读写出错字段
        public WinIO()
        {//构造函数(自动运行)
            try
            {
                enable = InitializeWinIo();//加载WinIO
            }
            catch (System.Exception error)
            {//WinIO加载失败异常处理
                MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK,
MessageBoxIcon.Error);
            }
        }
        ~WinIO()
        {//析构函数(自动运行)
            if (enable)
            {//成功加载WinIO
                ShutdownWinIo();//卸载WinIO
            }
        }
        public bool Enable
        {//Enable只读属性
            get
            {
                return enable;
            }
        }
        public bool Error
        {//Error只读属性
            get
            {
                return error;
            }
        }
        public byte this[uint index]//byte读写端口
        {//数组索引:
            get
            {//例如 x = WioIOPort[0x378];  等效:x = WioIOPort[0x378, sizeof(byte)];
                uint val;//WinIo要求dword
                error = GetPortVal((IntPtr)index, out val, sizeof(byte));
                return (byte)val;
            }
            set
            {//例如 WioIOPort[0x378] = 0xff; 等效:WioIOPort[0x378, sizeof(byte)] = 0xff;
                error = SetPortVal((IntPtr)index, (IntPtr)value, sizeof(byte));
            }
        }
        public uint this[uint index, byte size]//可用byte,word,dword读写端口
        {//数组索引,size=1/2/4=sizeof(byte/UInt16/UInt32)
            get
            {//例如 x = WioIOPort[0x378, sizeof(UInt16)];
                uint val;//WinIo要求dword
                error = GetPortVal((IntPtr)index, out val, size);
                return val;
            }
            set
            {//例如 WioIOPort[0x378, sizeof(UInt32)] = 0x12345678;
                error = SetPortVal((IntPtr)index, (IntPtr)value, size);
            }
        }
    }
    #endregion
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表