zoukankan      html  css  js  c++  java
  • C#LPT端口连接热敏打印机发送指令

    class LptControl
    {
        private string LptStr = "lpt1";
        public LptControl(string l_LPT_Str)
        {
    
            LptStr = l_LPT_Str;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct OVERLAPPED
        {
            int Internal;
            int InternalHigh;
            int Offset;
            int OffSetHigh;
            int hEvent;
        }
    
    
        //调用DLL.  
        [DllImport("kernel32.dll")]
        private static extern IntPtr 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);
        private int iHandle;
    
    
        /// <summary>  
        /// 打开端口  
        /// </summary>  
        /// <returns></returns>  
        public bool Open()
        {
            iHandle = CreateFile(LptStr, 0x40000000, 0, 0, 3, 0, 0).ToInt32();
            // iHandle = CreateFile(LptStr, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);  
    
            if (iHandle != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        /// <summary>  
        /// 打印字符串,通过调用该方法可以打印需要的字符串  
        /// </summary>  
        /// <param name="Mystring"></param>  
        /// <returns></returns>  
        public bool Write(String Mystring)
        {
            //如果端口为打开,则提示,打开,则打印  
            if (iHandle != -1)
            {
                OVERLAPPED x = new OVERLAPPED();
                int i = 0;
                //byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);  
                byte[] mybyte = Encoding.GetEncoding("GB2312").GetBytes(Mystring);
                bool b = WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
                return b;
            }
            else
            {
                throw new Exception("不能连接到打印机!");
            }
        }
        /// <summary>  
        /// 打印命令,通过参数,可以打印小票打印机的一些命令,比如换行,行间距,打印位图等。  
        /// </summary>  
        /// <param name="mybyte"></param>  
        /// <returns></returns>  
        public bool Write(byte[] mybyte)
        {
            //如果端口为打开,则提示,打开,则打印  
            if (iHandle != -1)
            {
                OVERLAPPED x = new OVERLAPPED();
                int i = 0;
                return WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
            }
            else
            {
                throw new Exception("不能连接到打印机!");
            }
        }
    
        /// <summary>  
        /// 关闭端口  
        /// </summary>  
        /// <returns></returns>  
        public bool Close()
        {
            return CloseHandle(iHandle);
        }
    
    }
                 Byte[] data1 = new byte[] { 0x1D, 0x56, 0x30 };//切纸指令
                 Byte[] data2 = new byte[] { 0x1B, 0x70, 0x00, 0x3C, 0xFF };//弹钱箱指令(什么鬼收银机中可以弹钱箱,我也不知道)
                 Byte[] data3 = new byte[] { 0x1D, 0x21, 0x24, 0x24 };//字体大小36
                 Byte[] data4 = new byte[] { 0x1D, 0x21, 0x0C, 0x0C };//字体大小12
                 Byte[] data5 = new byte[] { 0x1B, 0x45, 0x01 };//字体加粗开始
                 Byte[] data6 = new byte[] { 0x1B, 0x45, 0x00 };//字体加粗结束
    
                 LptControl lc = new LptControl("lpt1");//端口
                 lc.Open();
                 lc.Write(data3);//打印指令byte数组
    
                 lc.Write("文字文字");//打印文字
                 lc.Write("
    
    
    
    ");
                 lc.Close();

    转 : https://www.cnblogs.com/rinack/p/4837993.html

  • 相关阅读:
    python 异常类型大全
    HDU6532 Chessboard (最大费用流)
    P2764 最小路径覆盖问题 (最小点覆盖=顶点数-最大匹配)
    P3355 骑士共存问题 (最小割)
    P1251 餐巾计划 (网络流)
    P2765 魔术球问题 (网络流)
    P3381 [模板] 最小费用最大流
    P3376 [模板] 网络最大流
    P3384 [模板] 树链剖分
    BZOJ1009: [HNOI2008]GT考试 (矩阵快速幂 + DP)
  • 原文地址:https://www.cnblogs.com/fps2tao/p/14756026.html
Copyright © 2011-2022 走看看