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;
        }
    }

转载于:https://www.cnblogs.com/zahxz/archive/2012/12/10/2811537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值