首页 > 代码库 > C# 热敏打印机 Socket 网络链接 打印 图片 (一)

C# 热敏打印机 Socket 网络链接 打印 图片 (一)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace POSPrinter
{
    ///   <summary> 
    ///   POSPrinter的摘要说明。
    ///   此类处理网络打印,使用了IP端口.
    ///   </summary> 
    public class NetPOSPrinter 
    {
        string ipPort = "127.0.0.1";

        public NetPOSPrinter()
        {
        }

        public NetPOSPrinter(string IpPort)
        {
            this.ipPort = IpPort;//打印机端口 
        }

        ///   <summary> 
        ///   输出文字到打印机 
        ///   </summary> 
        ///   <param   name= "str "> 要打印的内容 </param> 
        public void PrintLine(string str)
        {
            //建立连接
            IPAddress ipa = IPAddress.Parse(ipPort);
            IPEndPoint ipe = new IPEndPoint(ipa, 9100);//9100为小票打印机指定端口
            Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            soc.Connect(ipe);

            //string str= "hello,123456789,大家好! ";

            byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
            soc.Send(b);
            soc.Close();
        }


        public void PrintPic(Bitmap bmp)
        {
            //把ip和端口转化为IPEndPoint实例
            IPEndPoint ip_endpoint = new IPEndPoint(IPAddress.Parse(ipPort), 9100);

            //创建一个Socket
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


            //连接到服务器
            socket.Connect(ip_endpoint);
            //应对同步Connect超时过长的办法,猜测应该是先用异步方式建立以个连接然后,
            //确认连接是否可用,然后报错或者关闭后,重新建立一个同步连接                    

            //socket.SendTimeout = 1000;

            //初始化打印机,并打印

            Byte[] byte_send = Encoding.GetEncoding("gb18030").GetBytes("\x1b\x40");

            //发送测试信息
            socket.Send(byte_send, byte_send.Length, 0);


            byte[] data = http://www.mamicode.com/new byte[] { 0x1B, 0x33, 0x00 };"gb18030").GetBytes("\n");

                //发送测试信息
                socket.Send(byte_send, byte_send.Length, 0);
            } // data

            byte_send = Encoding.GetEncoding("gb18030").GetBytes("\n");

            //发送测试信息
            socket.Send(byte_send, byte_send.Length, 0);
            socket.Close();
        }


        ///   <summary> 
        ///   打开钱箱 
        ///   </summary> 
        public void OpenCashBox()
        {
            IPAddress ipa = IPAddress.Parse(ipPort);
            IPEndPoint ipe = new IPEndPoint(ipa, 9100);//9100为小票打印机指定端口
            Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            soc.Connect(ipe);
            char[] c = { Convert.ToChar(27), ‘p‘, Convert.ToChar(0), Convert.ToChar(60), Convert.ToChar(255) };
            byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(c);
            soc.Send(b);
            soc.Close();
        }


    }
}

  转自:http://www.cnblogs.com/rinack/p/4838211.html

C# 热敏打印机 Socket 网络链接 打印 图片 (一)