zoukankan      html  css  js  c++  java
  • C# 实现Epson热敏打印机打印 Pos机用

    自己琢磨的,看了打印机的指令自己写的。共享出来,免得大家又去看烦人的指令

        public class EpsopnPrinter
        {

            //打印文字(端口号 字符)
            public static string PrintString(int Port, string val)
            {
                System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort();
                sp.PortName = "COM" + Port.ToString();
                try
                {
                    sp.Open();
                }
                catch
                {
                    return "端口被占用";
                }
                List<byte> data = new List<byte>();
                string[] lines = val.Split('\n');
                for (int i = 0; i < lines.Length; i++)
                {
                    byte[] content = System.Text.Encoding.Default.GetBytes(lines[i].Replace("\r", ""));
                    byte[] wapbt ={ 0x0a };
                    data.AddRange(content);
                    data.AddRange(wapbt);
                }
                byte[] cutbt={0x1d,0x56,0x42,0x11};
                data.AddRange(cutbt);
                byte[] databt = data.ToArray();
                sp.Write(databt, 0, databt.Length);
                sp.Close();
                return null;
            }

    //测试打印机是否接在这个端口
            public static bool PrintTest(int Port)
            {
                System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort();
                sp.PortName = "COM" + Port.ToString();
                sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
                try
                {
                    sp.Open();
                }
                catch
                {
                    return false;
                }
                Recived = false;
                byte[] testbt={0x1D,0x49,0x01};
                sp.Write(testbt, 0, testbt.Length);
                System.Threading.Thread.Sleep(100);
                sp.Close();
                return Recived;
            }

            static bool Recived = false;
            static void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                Recived = true;
            }
        }

  • 相关阅读:
    PAT甲级1056Mice and Rice
    李宏毅机器学习课程笔记-5.3神经网络中的反向传播算法
    李宏毅机器学习课程笔记-5.2神经网络为什么要是深度的
    PAT甲级1014Waiting in Line
    python批量处理邮件:poplib和email快速上手教程
    李宏毅机器学习课程笔记-5.1深度学习之引言
    李宏毅机器学习课程笔记-4.2分类模型之概率生成模型
    李宏毅机器学习课程笔记-4.1分类简介及其与回归的区别
    类自动加载封装类
    var与let循环中经典问题
  • 原文地址:https://www.cnblogs.com/wubin264/p/1683130.html
Copyright © 2011-2022 走看看