電子工程網(wǎng)

標題: C#下采用索引對WinIO的封裝類(lèi)(WinIO.cs) [打印本頁(yè)]

作者: hotpower    時(shí)間: 2009-4-3 00:10
標題: C#下采用索引對WinIO的封裝類(lèi)(WinIO.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;//MessageBox需要
using System.Runtime.InteropServices;//加動(dòng)態(tài)鏈接庫需要
//菜農HotPower@126.com  2008.11.12 于雁塔村
namespace WindowsFormsApplication2
{
    #region C#下采用索引對WinIO的封裝類(lèi)(WinIO.cs)
    /*
       應用時(shí)應該在Form1.cs中加入:
       private WinIO WinIOPort = new WinIO();
            例: WinIOPort[0x378] = 0x55;
                 x = WinIOPort[0x379, 2];
    */
    public class WinIO
    {
        //動(dòng)態(tài)鏈接庫的引用
        [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與并口無(wú)關(guān)的函數
                    [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;//讀寫(xiě)出錯字段
        public WinIO()
        {//構造函數(自動(dòng)運行)
            try
            {
                enable = InitializeWinIo();//加載WinIO
            }
            catch (System.Exception error)
            {//WinIO加載失敗異常處理
                MessageBox.Show(error.Message, "系統提示", MessageBoxButtons.OK,
MessageBoxIcon.Error);
            }
        }
        ~WinIO()
        {//析構函數(自動(dòng)運行)
            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讀寫(xiě)端口
        {//數組索引:
            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讀寫(xiě)端口
        {//數組索引,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
}




歡迎光臨 電子工程網(wǎng) (http://selenalain.com/) Powered by Discuz! X3.4
午夜高清国产拍精品福利|亚洲色精品88色婷婷七月丁香|91久久精品无码一区|99久久国语露脸精品|动漫卡通亚洲综合专区48页