C# 热敏打印机 Socket 网络链接 打印 图片 (一)
2015-09-25 14:48
756 查看
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 = new byte[] { 0x1B, 0x33, 0x00 }; socket.Send(data, data.Length, 0); data[0] = (byte)'\x00'; data[1] = (byte)'\x00'; data[2] = (byte)'\x00'; // Clear to Zero. Color pixelColor; // ESC * m nL nH 点阵图 byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x00, 0x00, 0x00 }; escBmp[2] = (byte)'\x21'; //nL, nH escBmp[3] = (byte)(bmp.Width % 256); escBmp[4] = (byte)(bmp.Width / 256); // data for (int i = 0; i < (bmp.Height / 24) + 1; i++) { socket.Send(escBmp, escBmp.Length, 0); for (int j = 0; j < bmp.Width; j++) { for (int k = 0; k < 24; k++) { if (((i * 24) + k) < bmp.Height) // if within the BMP size { pixelColor = bmp.GetPixel(j, (i * 24) + k); if (pixelColor.R == 0) { data[k / 8] += (byte)(128 >> (k % 8)); } } } socket.Send(data, 3, 0); data[0] = (byte)'\x00'; data[1] = (byte)'\x00'; data[2] = (byte)'\x00'; // Clear to Zero. } byte_send = Encoding.GetEncoding("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(); } } }
相关文章推荐
- tcprstat分析服务的响应速度
- DataSnap如何监控Tcp/IP客户端的连接情况
- 第1章 网络基础知识
- boost asio网络编程
- C# 使用IP端口网络打印图片
- python--httplib模块使用
- python案例学习——http服务器
- 移动端网络游戏服务器架构
- loopj.android.http.AsyncHttpClient 400
- Linux下必须知道的11个网络命令
- 2015(第五届)中国未来网络发展与创新论坛 征文通知
- 2015(第五届)中国未来网络发展与创新论坛 征文通知
- Apache2 httpd.conf 配置详解
- 新建JSP报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- Broadcast监听网络状态,ping判断网络是否可用
- Windows 7系统笔记本创建wifi热点供手机上网的方法
- Grid ++ MIME 类型配置 载入报表数据,检查此URL及其数据,错误提示 网络服务器响应不成功
- 未能从'http://192.168.7.174/ddd/abc?abc.grf'a=00234载入报表数据,检查此URL及响应数据,错误提示:网络服务器
- 15北京网络赛 E题
- 网络基础知识学习