zoukankan      html  css  js  c++  java
  • C#后台调用LPT1端口实现小票机打印方法。

        public class POSPrinter
        {
            const int OPEN_EXISTING = 3;
    
            string prnPort = "LPT1";
            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            private static extern IntPtr CreateFile(string lpFileName,
            int dwDesiredAccess,
            int dwShareMode,
            int lpSecurityAttributes,
            int dwCreationDisposition,
            int dwFlagsAndAttributes,
            int hTemplateFile);
            public POSPrinter()
            {
            
            }
            public POSPrinter(string prnPort)
            {
                this.prnPort = prnPort;//打印机端口
            }
            public string PrintLine(string str)
            {
                IntPtr iHandle = CreateFile(prnPort, 0x50000000, 0, 0, OPEN_EXISTING, 0, 0);
                if (iHandle.ToInt32() == -1)
                {
                    Console.WriteLine(iHandle.ToString());
                    return "没有连接打印机或者打印机端口不是LPT1";
                }
                else
                {
                    Console.WriteLine(iHandle.ToString());
                    FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
                    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
                    sw.WriteLine("           小票单");
                    sw.WriteLine();
                    sw.WriteLine(str);
                    sw.WriteLine("打印内容");
                    sw.WriteLine("---------------------------");
    
                    sw.Close();
                    fs.Close();
                    return "打印成功!";
                }
            }
        }


    直接调用PrintLine();方法进行打印具体需要的参数和打印格式大家自行调整。

  • 相关阅读:
    python获取DBLP数据集
    GNUPLOT 画多组柱状图 以及 折线图 以及各种问题的解决方案
    Leetcode 1:two sum
    测试面试之如何测试一个杯子
    C++小总结
    统计‘1’的个数
    C语言小总结
    剑指offer面试题1---赋值运算符函数
    黑盒测试与白盒测试
    软件测试的原则
  • 原文地址:https://www.cnblogs.com/liluping860122/p/3099054.html
Copyright © 2011-2022 走看看