WinIO 并口打印机通讯(得到打印机状态 针对爱普生532)

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PrintPaper
{
    public class PrintPaper
    {
        public static bool getStatus()
        {
            try
            {
                WinIO wio = new WinIO();
                byte b = wio[0x379];
                wio.close();
                return ((b & 32) > 0)||((b&8)==0) ;//打印机无纸或错误的返回值 根据打印机不同而改变,从打印机厂家得到。
            }
            catch (Exception ex) {
                return true;
            }
        }

        #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加载失败异常处理
                   throw new Exception("加载失败");
                }
            }

            public void close()
            {//析构函数(自动运行)
                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
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值