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;

       //主要是在从COM端口中接收数据时触发
            static void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                Recived = true;
            }
        }

  • 相关阅读:
    PHP实现git部署的方法教程
    windows下php7.1安装redis扩展以及redis测试使用全过程
    win7下php7.1运行getenv('REMOTE_ADDR')fastcgi停止运行
    CGI与FastCGI
    Laravel 单设备登录
    Laravel 登录后清空COOKIE 方法
    PHP进阶与redis锁限制并发访问功能示例
    微信开放平台开发——网页微信扫码登录(OAuth2.0)
    laravel 项目本地版本为5.5,线上mysql 为5.7.21版本,执行严格模式
    mysql中bigint、int、mediumint、smallint与tinyint的取值范围
  • 原文地址:https://www.cnblogs.com/zahxz/p/2811537.html
Copyright © 2011-2022 走看看