zoukankan      html  css  js  c++  java
  • C#Lpt端口打印类的操作浅析

    C#LPT端口打印类的操作是什么呢?首先让我们看看什么是LPT端口(打印机专用)?LPT端口是一种增强了的双向并行传输接口,在USB接口出现以前是扫描仪,打印机最常用的接口。最高传输速度为1.5Mbps,设备容易安装及使用,但是速度比较慢,下面是C#LPT端口打印类的操作具体实例:

    /// <summary>  
        /// LPTControl 的摘要说明,C#LPT端口打印类的操作  
        /// </summary>  
        public class LPTControls
        {
            public LPTControls() { }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct OVERLAPPED
            {
                int Internal;
                int InternalHigh;
                int Offset;
                int OffSetHigh;
                int hEvent;
            }
    
            [DllImport("kernel32.dll")]
            private static extern int CreateFile(
             string lpFileName,
             uint dwDesiredAccess,
             int dwShareMode,
             int lpSecurityAttributes,
             int dwCreationDisposition,
             int dwFlagsAndAttributes,
             int hTemplateFile
             );
    
            [DllImport("kernel32.dll")]
            private static extern bool WriteFile(
             int hFile,
             byte[] lpBuffer,
             int nNumberOfBytesToWrite,
             ref int lpNumberOfBytesWritten,
             ref OVERLAPPED lpOverlapped
             );
    
            [DllImport("kernel32.dll")]
            private static extern bool CloseHandle(
             int hObject
             );
    
            //C#LPT端口打印类的操作  
            private int iHandle;
            public bool Open()
            {
                iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
                if (iHandle != -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            public bool Write(String Mystring)
            {
                if (iHandle != -1)
                {
                    int i = 0;
                    OVERLAPPED x = new OVERLAPPED();
                    byte[] mybyte =
                    System.Text.Encoding.Default.GetBytes(Mystring);
                    return WriteFile(
                    iHandle, mybyte, mybyte.Length, ref i, ref x);
                }
                else
                {
                    throw new Exception("端口未打开!");
                }
            }
    
            public bool Close()
            {
                return CloseHandle(iHandle);
            }
        }
  • 相关阅读:
    kafka学习-卡不卡的简单使用
    mongoDB-mongo compass使用学习-最像关系型数据库的非关系型文档型数据库
    postman使用-跑男常用功能
    jmeter数据驱动csv+批量导出数据到csv文件
    jmeter-数据库查询与调用-mysql
    arch linux mysql using
    Win10安装 oracle11g 出现INS-13001环境不满足最低要求解决方法
    oracle11g数据库安装
    安装PLSQLDeveloper
    oracle基本学习
  • 原文地址:https://www.cnblogs.com/rinack/p/4837993.html
Copyright © 2011-2022 走看看